Sum of integers (tam sayıların toplamı)

Haluk

Özel Üye
Katılım
7 Temmuz 2004
Mesajlar
12,406
Excel Vers. ve Dili
64 Bit 2010 - İngilizce
+
Google Sheets
+
JScript
Altın Üyelik Bitiş Tarihi
Herkese merhaba;

Forumun yeni bölümüne ilk soruyu ben yazayım ... :mrgreen:

Soru, 1' den başlayarak herhangibir sayıya kadar olan tam sayıların toplamını bulunması ile ilgili.

Diyelim ki, sayımız 1.000.000.000 olsun.

1 + 2 + 3 + 4 + 5 + .............. + 999.999.999 + 1.000.000.000 = ?



******************************************************

Hi all;

Let me be the first one, posting on this new part of the board ... :mrgreen:

The question is, finding the sum of the integer numbers starting from 1, to the number under concern.

So, let's assume the number is 1.000.000.000

1 + 2 + 3 + 4 + 5 + .............. + 999.999.999 + 1.000.000.000 = ?
 
Katılım
14 Şubat 2006
Mesajlar
3,426
Excel Vers. ve Dili
(Excel 2016 - İngilizce)
Altın Üyelik Bitiş Tarihi
30-11-2022
Kod:
Sub Total()
Endnumber = InputBox("Entry Number End.", "Number", 1000)
If Not IsNumeric(Endnumber) Then Exit Sub
For i = 1 To Endnumber
Totalnumber = Totalnumber + i
Next i
MsgBox Totalnumber
End Sub
 

Levent Menteşoğlu

Administrator
Yönetici
Admin
Katılım
13 Ekim 2004
Mesajlar
16,057
Excel Vers. ve Dili
Excel 2010-32 bit-Türkçe
Excel 365 -32 bit-Türkçe
Simple solution by using Gauss method

1 + 2 + 3 + 4 + 5 + .............. + 999.999.999 + 1.000.000.000

1.000.000.000+999.999.999.....................+2+1

1.000.000.001 x 1.000.000.000/2=5E+17
 

Ali

Uzman
Katılım
21 Temmuz 2005
Mesajlar
7,895
Excel Vers. ve Dili
İş:Excel 2016-Türkçe
If you want to use Excel standart function.Can you try this formula.

I assumed A500 cell (your last cell) . You must write this formula into B1 cell

=(A500*(A500+1))/2
 

Haluk

Özel Üye
Katılım
7 Temmuz 2004
Mesajlar
12,406
Excel Vers. ve Dili
64 Bit 2010 - İngilizce
+
Google Sheets
+
JScript
Altın Üyelik Bitiş Tarihi
Hi ripek, leventm and fructose;

This is the problem ...

ripek's code seem to be correct and, leventm's Gauss theorem is also correct.

But the results are different... :mrgreen:

I've tried code similar to ripek's, and got the answer as :
500. 000. 000. 067. 109. 000

I also tried leventm's approach before, and got the result as :
500. 000. 000. 500. 000. 000

Also, I tried a similar routine in C language, but that code also yielded the same result in VBA.

So, this is the real question ..... why are they different ? .... :mrgreen:

I' ve some ideas but not sure ... :)
 

Ali

Uzman
Katılım
21 Temmuz 2005
Mesajlar
7,895
Excel Vers. ve Dili
İş:Excel 2016-Türkçe
Hi Haluk;

Hi ripek, leventm and fructose;


So, this is the real question ..... why are they different ? .... :mrgreen:

I' ve some ideas but not sure ... :)

Are you sure :mrgreen:

Did you try to separate Levent's code parenthesis.

I think,We must to pay attetion procedure priority

I tried ripek's code and Levent's code.

The results same.
 

Levent Menteşoğlu

Administrator
Yönetici
Admin
Katılım
13 Ekim 2004
Mesajlar
16,057
Excel Vers. ve Dili
Excel 2010-32 bit-Türkçe
Excel 365 -32 bit-Türkçe
I have also tried Ripek's code, The results are not same, Haluk's right
 
Katılım
14 Şubat 2006
Mesajlar
3,426
Excel Vers. ve Dili
(Excel 2016 - İngilizce)
Altın Üyelik Bitiş Tarihi
30-11-2022
.....I think...... :D
 

Merhum İdris SERDAR

Moderatör
Yönetici
Katılım
21 Ekim 2005
Mesajlar
17,094
Excel Vers. ve Dili
Excel, 365 - İngilizce
I am not sure. But may be the reason arises from digits' limit in excel.
Because, as far as I know the limit of digit in Excel is 16.
 
Son düzenleme:

Haluk

Özel Üye
Katılım
7 Temmuz 2004
Mesajlar
12,406
Excel Vers. ve Dili
64 Bit 2010 - İngilizce
+
Google Sheets
+
JScript
Altın Üyelik Bitiş Tarihi
I thought the same .... as yurttas and that's why I tried in C hoping to get a more reliable result.

But, this or that .... every program has some limits.

One of the things what I think is that; because Excel has some trouble in dealing with large numbers in a big for-next loop, I decided to divide the number (i.e. 1.000.000.000) in smaller parts and perform the calculation.

Something like this;

Kod:
DefDbl A-Z
'
Sub Test()
    i = 1000000000 / 5
    For x = 1 To i
        y = y + x
        z = z + i * 1 + x
        z2 = z2 + i * 2 + x
        z3 = z3 + i * 3 + x
        z4 = z4 + i * 4 + x
    Next
    [A7] = (y + z + z2 + z3 + z4)
End Sub
The above procedure gives the result as :
500.000.000.111.503.000

As per my last coding stated above, when I performed the procedure by 4 subtotals instead of 5, I' ve got the result as :
500.000.000.098.792.000

So, what I see from here is that .... by increasing the amount of subtotals, we can achieve more reliable results.

Having said this ..... if we are not using Excel to program a spaceship landing to moon, we can use it safely in our daily routines... as long as we are not dealing with large numbers....... :mrgreen:
 
Katılım
21 Ağustos 2004
Mesajlar
12
Excel Vers. ve Dili
Xl2000, 2003, 2007
this may be possible by version Excel 2100. :)

:) Yes, how true.
unfortunateley all software such as Exel that depend/use the IEEE
floating point standard will yield only up to 15 decimal places. Even with this
"accuracy", many numbers are represented as an approximation of their "true" value.
 

mehmett

Altın Üye
Katılım
18 Mayıs 2005
Mesajlar
2,571
Excel Vers. ve Dili
Excel 2010 Türkçe
Welcome to Excel.Web.Tr Mr Moala,

Nice to meet you.
 
Katılım
14 Şubat 2006
Mesajlar
3,426
Excel Vers. ve Dili
(Excel 2016 - İngilizce)
Altın Üyelik Bitiş Tarihi
30-11-2022
Welcome Mr Moala,
 

Haluk

Özel Üye
Katılım
7 Temmuz 2004
Mesajlar
12,406
Excel Vers. ve Dili
64 Bit 2010 - İngilizce
+
Google Sheets
+
JScript
Altın Üyelik Bitiş Tarihi
unfortunateley all software such as Exel that depend/use the IEEE
floating point standard will yield only up to 15 decimal places. Even with this
"accuracy", many numbers are represented as an approximation of their "true" value.
Yes, this is the exact definition of the problem.

Such as calculating, 170!

http://www.google.com/search?hl=en&q=170!&btnG=Google+Search

BTW, because of this problem (I guess) we can not calculate 171! with our microcomputers.

p.s. Ivan, Nice to see you on the board. :hey:
 

Levent Menteşoğlu

Administrator
Yönetici
Admin
Katılım
13 Ekim 2004
Mesajlar
16,057
Excel Vers. ve Dili
Excel 2010-32 bit-Türkçe
Excel 365 -32 bit-Türkçe
Mr Moala, welcome, nice to see you on our board.
 
Üst