February 4th, 2013, 03:35 PM

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
February 4th, 2013, 05:51 PM

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
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.factorial
<builtin 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...88728663983613570273121541638995145894561079999213500076101248781500642979091198044736325729107144579768273776743716439557581557253878204647391176582851167312182026188795156620056856503340092247479478684738621107994804323593105039052556442336528920420940314
999 402790454129684136968124369...58756417455613570273121541638995145894561079999213500076101248781500642979091198044736325729107144579768273776743716439557581557253878204647391176582851167312182026188795156620056856503340092247479478684738621107994804323593105039052556442336528920420940314
1000 402790050531223457680670558292...86509889455613570273121541638995145894561079999213500076101248781500642979091198044736325729107144579768273776743716439557581557253878204647391176582851167312182026188795156620056856503340092247479478684738621107994804323593105039052556442336528920420940314
spaces amid these last few numbers are a forum artifact.
Last edited by b49P23TIvg; February 4th, 2013 at 05:54 PM.
[code]
Code tags[/code] are essential for python code and Makefiles!
February 5th, 2013, 01:09 PM

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: 2155807062973525294082617280308786356700898434469931032502371601179120011055473750789148656432818290093666154615739794923853029528972717288228370095719154975954692625128833698580515126584020852442411882819246541016990988007565261208879490584433158479186439975551415213949890908678604990005556779257749349473334997043255989602160778364015250238241900215268474438865449301901871659068494172487120867925305629300937224625391333050935360609602339513957978166961355114833860855148276479349334120865735602751414820515310085831617256249830345937278877078409355879253388368045181606099925992777569196164327851729038486942961238131892211577632461879137144817904459295250470462496495568202407584187968923150458473695148511540924159897272823449790622496489846305232216587382956911270838839265245788488340257928003354818618571043961248617036655233642960100028530256219433690393379160212868923087867833788928795947347202821514848523003573442645006571598198571019155302488536659625420737913273978344153611624449360984538230244151437584833719646391121080060507187678070970358835191643879062250913016864613784066077725987871486863612847537860636587518702983810144557650014940399940157896979940935114953309163811737207902042095125528533292653622179639356859115482528418494293874724115631925132058573620803275030561085738248057020276229793453820426295521640308049511542417549614892574090710757501726491026217189958335233300466841651814533376031318344323161328203269792425411780992018450912632445866941175192463052196180271026448597722461125193753990753948519623569457016672298731382483386372669598087649343225873254718214092086239372838632058478114971796212873854929696527425249372973371271380578933790754146304527684230245653237400923771384917932109959260869655038201095104290927642570209203958201574125369144863553330200474705568789183569806107030916148358700598542573003450439544656927445873306639004704359322952346994146290306556120373342933790733231008057471648187952488902459904948876361169159662872294171259754063779498199975329276590983125060001169767365678358045783042026192144142179810263704762102779958680422030405068084389173653453415549941099366532908416706628554989950232429912065117118006947801384158359099790555406753162006777312785120385400491709454967026177448176090056203043595067174802697728803900313407012383986857586489038983308526558443171181213625626614419249108031199616900264024948729882955284588224003834245288199539249446166748404068614668633541330917275243291960391466318021210908218921148301614627171719129978894986260705742186808255987330258593656749720362745579506052137733643388530121153045760862360436736000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Total sum of Fibonacci is: 573147844013817084100
February 5th, 2013, 02:41 PM

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
4027904541296841369681243691790592110662303408143338755347324901373899714535302578347295241534700398426757855305712604968169738979178400674667312310904279804410393283696170463209888223218063312979344591705436611985001205850357950858332862384205197120735838...
Last edited by b49P23TIvg; February 5th, 2013 at 02:55 PM.
[code]
Code tags[/code] are essential for python code and Makefiles!
February 5th, 2013, 05:53 PM

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.
February 7th, 2013, 12:43 PM

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.
February 7th, 2013, 12:51 PM

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
[code]
Code tags[/code] are essential for python code and Makefiles!
February 7th, 2013, 01:23 PM

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.