Understanding Fortran Factorials in Infinite Sums

In summary, the conversation discusses how to evaluate a complicated factorial expression using the infinite sum of binomial theorem. The code provided demonstrates how to input values for x and n, and uses a loop to calculate the sum. The conversation also addresses a discrepancy in the results and clarifies the correct calculation for the value of "fact". Ultimately, the participants come to a clearer understanding of the process.
  • #1
Goatsenator
20
0
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?
 
Technology news on Phys.org
  • #2
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".
 
  • #3
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.
 
  • #4
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 ).
 
Last edited:
  • #5
Goatsenator said:
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.
 
  • #6
Code:
	i=n
	fact=1 
	sum=1
	DO k= 1,n
		f = f*i/k*x
		i = i-1
		s = s + f
	END DO
 
  • #7
AlephZero said:
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!
 

1. What is a factorial in Fortran?

A factorial in Fortran is a mathematical operation that calculates the product of all positive integers less than or equal to a given number. In other words, it is the result of multiplying a number by all the numbers below it.

2. How is a factorial represented in Fortran code?

In Fortran, a factorial is represented using the exclamation mark (!) after the number. For example, 5! would be written as 5! in Fortran code.

3. What is the purpose of using factorials in infinite sums in Fortran?

Infinite sums involve adding an infinite number of terms together. In Fortran, factorials are used in these sums to help simplify the calculation and make it more efficient.

4. How does Fortran handle large factorials in infinite sums?

Fortran has a built-in function, called "factorial", that can handle large factorials in infinite sums. This function uses a special algorithm to accurately calculate the factorial of any given number, even if it is very large.

5. What are some common errors when working with factorials in Fortran?

Some common errors when working with factorials in Fortran include forgetting to include the exclamation mark after the number, using negative numbers (which are not defined in factorials), and exceeding the maximum value that Fortran can handle for factorials (typically 170!). It is important to carefully check the input and output of any factorial calculations to avoid these errors.

Similar threads

  • Programming and Computer Science
Replies
4
Views
498
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
22K
Replies
4
Views
1K
  • Programming and Computer Science
Replies
26
Views
3K
  • Programming and Computer Science
Replies
31
Views
2K
Replies
9
Views
940
Replies
4
Views
283
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
931
Back
Top