Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 4th, 2013, 03:35 PM
 keshk
Contributing User

Join Date: Nov 2012
Posts: 49
Time spent in forums: 19 h 5 m 33 sec
Reputation Power: 2
Print out Cumulative Sum

Trying to print out cumulative sum of fibonacci(first 100) and cumulative factorial numbers(first 1000) sum. Work out as follows. I don't know if the workout/ the output is correct ( or how to check if correct).
Please advice if it is right or maybe a better way of doing it. Thank you.

Question 1:
Calculate a cumulative sum of the first 100 Fibonacci numbers.
Question 2:
Calculate a cumulative sum of the first 1000 factorials.

Code:
```#Fibonacci
fibo_Number = 100

a, b = 0, 1

fibo_Sum = a + b

for x in range(3, fibo_Number + 1):
a, b = b, a + b
fibo_Sum += b

print "Fibonacci cumulative sum for first 100 is: ", fibo_Sum

#Factorial
fact_Number = 1000

f = 1
fact_Sum = f

for i in range(2, fact_Number + 1):
f += i
fact_Sum += f

print "\nFactorial cumulative sum for first 1000 is: ", fact_Sum
```

#2
February 4th, 2013, 05:51 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,163
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 25 m 16 sec
Reputation Power: 455
Note that although the python math library was intended to map the functions in c language math.h they inserted a factorial function.
Code:
```\$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
>>> import math
>>> math.factorial
<built-in function factorial>
>>> ```

Code:
```   NB. Fibonacci cumulative sums

F =: ({: , +/)^:(<101) 0 1x  NB. F contains many Fibonacci numbers.  Which do you consider the first?

(i.@:# ,. +/\) {."1 F  NB. index and cumulative sum.
0                     0
1                     1
2                     2
3                     4
4                     7
5                    12
6                    20
...
97 218922995834555169025
98 354224848179261915074
99 573147844013817084100
100 927372692193078999175```
Code:
```  NB. factorial 8 is the product 8*7*6*5*4*3*2*1 and that factorial 0 is 1.
NB. Again, which is "first"?
(,. !) i. 7  NB. index and factorial
0   1
1   1
2   2
3   6
4  24
5 120
6 720
NB. none of my software appreciates all the digits I'm displaying.
10^.+/!i.1001x  NB. The answer has something like 2600 base 10 digits, depending on what you consider #1
2567.61

(,,.&LF ":(8&{. , _4&{.)(,. +/\@:!) i. 1001x)1!:2<'/tmp/firstsAndLastsCumulativeSumOfFactorial'  NB. written to a file, and extracted therefrom.  Many digits deleted.
0    1
1    2
2    4
3   10
4   34
5  154
6  874
7 5914
998       403194052590363424421935...8872866398361357027312154163899514589456107999921350007610124878150064297  9091198044736325729107144579768273776743716439557581557253878204647391176582851167312182026188795156  620056856503340092247479478684738621107994804323593105039052556442336528920420940314
999    402790454129684136968124369...5875641745561357027312154163899514589456107999921350007610124878150064  2979091198044736325729107144579768273776743716439557581557253878204647391176582851167312182026188795  156620056856503340092247479478684738621107994804323593105039052556442336528920420940314
1000 402790050531223457680670558292...8650988945561357027312154163899514589456107999921350007610124878150  0642979091198044736325729107144579768273776743716439557581557253878204647391176582851167312182026188  795156620056856503340092247479478684738621107994804323593105039052556442336528920420940314```
spaces amid these last few numbers are a forum artifact.
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : February 4th, 2013 at 05:54 PM.

#3
February 5th, 2013, 01:09 PM
 keshk
Contributing User

Join Date: Nov 2012
Posts: 49
Time spent in forums: 19 h 5 m 33 sec
Reputation Power: 2
Still absorbing your codes and trying to understand it. I am getting the following as results. As you mentioned, way too much of digits being displayed and confusing as to whether it is correct or right.

Code:
```Factorial sum: 2155807062973525294082617280308786356700898434469931032502371601179120011055473750789148656432818290  0936661546157397949238530295289727172882283700957191549759546926251288336985805151265840208524424118  8281924654101699098800756526120887949058443315847918643997555141521394989090867860499000555677925774  9349473334997043255989602160778364015250238241900215268474438865449301901871659068494172487120867925  3056293009372246253913330509353606096023395139579781669613551148338608551482764793493341208657356027  5141482051531008583161725624983034593727887707840935587925338836804518160609992599277756919616432785  1729038486942961238131892211577632461879137144817904459295250470462496495568202407584187968923150458  4736951485115409241598972728234497906224964898463052322165873829569112708388392652457884883402579280  0335481861857104396124861703665523364296010002853025621943369039337916021286892308786783378892879594  7347202821514848523003573442645006571598198571019155302488536659625420737913273978344153611624449360  9845382302441514375848337196463911210800605071876780709703588351916438790622509130168646137840660777  2598787148686361284753786063658751870298381014455765001494039994015789697994093511495330916381173720  7902042095125528533292653622179639356859115482528418494293874724115631925132058573620803275030561085  7382480570202762297934538204262955216403080495115424175496148925740907107575017264910262171899583352  3330046684165181453337603131834432316132820326979242541178099201845091263244586694117519246305219618  0271026448597722461125193753990753948519623569457016672298731382483386372669598087649343225873254718  2140920862393728386320584781149717962128738549296965274252493729733712713805789337907541463045276842  3024565323740092377138491793210995926086965503820109510429092764257020920395820157412536914486355333  0200474705568789183569806107030916148358700598542573003450439544656927445873306639004704359322952346  9941462903065561203733429337907332310080574716481879524889024599049488763611691596628722941712597540  6377949819997532927659098312506000116976736567835804578304202619214414217981026370476210277995868042  2030405068084389173653453415549941099366532908416706628554989950232429912065117118006947801384158359  0997905554067531620067773127851203854004917094549670261774481760900562030435950671748026977288039003  1340701238398685758648903898330852655844317118121362562661441924910803119961690026402494872988295528  4588224003834245288199539249446166748404068614668633541330917275243291960391466318021210908218921148  3016146271717191299788949862607057421868082559873302585936567497203627455795060521377336433885301211  5304576086236043673600000000000000000000000000000000000000000000000000000000000000000000000000000000  0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000  000000000000000000000000000000000000000000000000000000000000000000000
Total sum of Fibonacci is:  573147844013817084100```

#4
February 5th, 2013, 02:41 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,163
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 25 m 16 sec
Reputation Power: 455
Our factorial results disagree. I verified my result using python. Pretty sure I've got the correct answer.
You've got, in my opinion, roughly 311 digits too many.
Code:
```>>> def factorial(x):
...  if 0 == x: return 1
...  if x < 3: return x
...  p = 1
...  for i in range(2,x+1): p *= i
...  return p
...
>>> factorial(6)
720
>>> print(sum(factorial(i)for i in range(1000))) # or to 1001, depending on how the professor counts
# line omitted for sanity.  It's the same as I reported before.
>>> print(sum(factorial(i)for i in range(4))) # 1 1 2 6
10```
And here's a little help with the j. The interactive j prompt is a three space indentation.
Code:
```   NB. =: is global assignment, "copula"

sum =: +/          NB. meaning "insert +"

factorial =: !

integers =: i.

extended =: x:

integers 8      NB. demonstrate integers
0 1 2 3 4 5 6 7

sum factorial extended integers 1000
4027904541296841369681243691790592110662303408143338755347324901373899714535302578347295241534700398  4267578553057126049681697389791784006746673123109042798044103932836961704632098882232180633129793445  91705436611985001205850357950858332862384205197120735838...
```

Last edited by b49P23TIvg : February 5th, 2013 at 02:55 PM.

#5
February 5th, 2013, 05:53 PM
 dwblas
Contributing User

Join Date: May 2009
Posts: 412
Time spent in forums: 5 Days 5 h 29 m 16 sec
Reputation Power: 32
To test, add a simple print statement which will give a running total.
Code:
```for x in range(3, fibo_Number + 1):
a, b = b, a + b
fibo_sum += b
print x, a, b, fibo_sum ```

Last edited by dwblas : February 5th, 2013 at 05:55 PM.

#6
February 7th, 2013, 12:43 PM
 keshk
Contributing User

Join Date: Nov 2012
Posts: 49
Time spent in forums: 19 h 5 m 33 sec
Reputation Power: 2
Hi,

Been trying some days now but still not able to really come to a proper conclusion. I haven't really deviated much from my previous workings thus embarrassingly got nothing really new to show.

In case the answer is clearly stated above, sorry but I'm really blank here. As mentioned, I am trying to work out simple program to print out cumulative factorial and fibonacci numbers.

To keep the numbers small, cumulative fibonacci for first 10 numbers is 143 or (1 +1 +2 +3 +5 +8 +13 +21 +34 +55). I am trying to program out an equation for this outcome.

Same for factorial. Apologises for repeating on this but trying to get clarity.Any advice is appreciated. Thank you.

#7
February 7th, 2013, 12:51 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,163
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 25 m 16 sec
Reputation Power: 455
Our solutions for sum of Fibonacci numbers (first 100) agree.

The factorials are the trouble.
It's not hard. Easy, actually
Code:
```>>> import math
>>> sum(math.factorial(n) for n in range(10))
409114```

#8
February 7th, 2013, 01:23 PM
 keshk
Contributing User

Join Date: Nov 2012
Posts: 49
Time spent in forums: 19 h 5 m 33 sec
Reputation Power: 2
Thanks so much. Think the factorial question was getting to me and all the while thinking fibonacci workout was wrong too.

I'm introduced to another useful built in function again. Makes the whole complicated feeling regards to factorial numbers better now. Tnks.

Last edited by keshk : February 7th, 2013 at 01:31 PM.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Print out Cumulative Sum