Fortran Factorials


by Goatsenator
Tags: factorials, fortran
Goatsenator
Goatsenator is offline
#1
Feb9-12, 06:18 PM
P: 20
I don't understand at all how you tell the computer to evaluate a complicated factorial expression such as the one given in in the infinite sum of binomial theorem as

Ʃ [n! / k!(n-k)! ] * x^k

where n is the final value of the sum and k is where you are in the loop.

It's supposed to be

INTEGER :: k, n
REAL :: sum, fact, x

ASK INPUT (what are x and n?)

DO k = 0,n
sum = sum + fact*x**k
fact = fact * (n-k)/(k+1)
END DO

Is there a procedure to figure out what the term multiplied by the fact variable should be?
Phys.Org News Partner Science news on Phys.org
Internet co-creator Cerf debunks 'myth' that US runs it
Astronomical forensics uncover planetary disks in Hubble archive
Solar-powered two-seat Sunseeker airplane has progress report
AlephZero
AlephZero is offline
#2
Feb9-12, 07:32 PM
Engineering
Sci Advisor
HW Helper
Thanks
P: 6,388
Look at what the term are summing, for each value of k.

When k = 0, it is n! / (0! n!) so k0 = 1
When k = 1, it is n! / (1! (n-1)! so k1 = n = k0 * n / 1
When k = 2, it is n! / (2! (n-2)! so k2 = n(n-1) / 2! = k1 * (n-1) / 2
When k = 3, it is n! / (3! (n-3)! so k3 = n(n-1)(n-2) / 3! = k2 * (n-2) / 3
etc.
That is what the program is doing when it updates "fact".
Goatsenator
Goatsenator is offline
#3
Feb9-12, 09:35 PM
P: 20
Hmmm... then fact * (n-k)/(k+1) can't be right because it doesn't match the results of working out all the factorials like that. I checked it in command prompt and it said the sum with x = 2 and n = 2 is 5. This sum represents (1+x)^n which should be 9 in that case.

I thought fact * ( (n-k+1) / k ) would work but I'm not getting the right answer with that either.

Goatsenator
Goatsenator is offline
#4
Feb9-12, 09:50 PM
P: 20

Fortran Factorials


disregard that. i worked it out on paper and i got 9 but for some reason the program is outputting 5...

If the k0,k1,k2 etc values are the "fact", how is (n-2)/3 = (n-k)/(k+1) for k = 3? That's why I thought it should be ( (n-k+1) / k ).
AlephZero
AlephZero is offline
#5
Feb10-12, 07:33 AM
Engineering
Sci Advisor
HW Helper
Thanks
P: 6,388
Quote Quote by Goatsenator View Post
If the k0,k1,k2 etc values are the "fact", how is (n-2)/3 = (n-k)/(k+1) for k = 3? That's why I thought it should be ( (n-k+1) / k ).
The code calculates "k3" when k = 2, not when k = 3.

Each time through the loop, it uses the current value of fact, then calculates the next value.
Xitami
Xitami is offline
#6
Feb10-12, 10:30 AM
P: 129
	i=n
	fact=1 
	sum=1
	DO k= 1,n
		f = f*i/k*x
		i = i-1
		s = s + f
	END DO
Goatsenator
Goatsenator is offline
#7
Feb10-12, 10:38 AM
P: 20
Quote Quote by AlephZero View Post
The code calculates "k3" when k = 2, not when k = 3.

Each time through the loop, it uses the current value of fact, then calculates the next value.
OH! I just realized that! It's so simple now that I understand it. Thank you for the help!


Register to reply

Related Discussions
Fortran 77 help making an empty array (or blank list if they exist in fortran) Programming & Computer Science 5
FORTRAN 95: New to fortran, want to learn how to input a function Programming & Computer Science 1
Factorials Calculus & Beyond Homework 3
Accessing Fortran Modules within a Fortran library from Fortran Programming & Computer Science 0
sum of the first n natural numbers is n(n+1)/2 General Math 2