Understanding Fortran Factorials in Infinite Sums

  • Context: Fortran 
  • Thread starter Thread starter Goatsenator
  • Start date Start date
  • Tags Tags
    Factorials Fortran
Click For Summary

Discussion Overview

The discussion revolves around implementing a factorial expression within an infinite sum as described by the binomial theorem in Fortran. Participants explore how to correctly compute terms in the sum and the logic behind updating the factorial variable in the code.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about how to evaluate a factorial expression in the context of an infinite sum.
  • Another participant outlines the terms being summed for various values of k, illustrating how the factorial values are derived step-by-step.
  • A participant questions the correctness of the formula used to update the factorial variable, noting discrepancies between expected and actual results.
  • Further discussion reveals that the program outputs a different result than anticipated, prompting a reevaluation of the factorial update logic.
  • Participants discuss the calculation of factorial values and how they relate to the loop variable k, with some proposing alternative formulations for the factorial update.
  • A later reply clarifies that the code calculates the factorial for k=2 when it should be for k=3, leading to a realization about the loop's operation.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correct formulation for updating the factorial variable, and there are differing interpretations of how the code should function. The discussion remains unresolved regarding the exact implementation details.

Contextual Notes

There are limitations in the discussion regarding the assumptions made about the factorial calculations and the definitions of variables used in the code. Some mathematical steps remain unresolved, particularly concerning the factorial update logic.

Goatsenator
Messages
18
Reaction score
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
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".
 
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.
 
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:
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.
 
Code:
	i=n
	fact=1 
	sum=1
	DO k= 1,n
		f = f*i/k*x
		i = i-1
		s = s + f
	END DO
 
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!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
23K
  • · Replies 26 ·
Replies
26
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K