# Help with noob pascal program

Discussion in 'OT Technology' started by Snowman, Feb 13, 2004.

1. ### SnowmanI know the Benzes.OT Supporter

Joined:
Sep 17, 2002
Messages:
3,052
0
Hey guys I'm trying to figure out how to do factorials in a program.

Here is what I have so far:

program fact;
var n0,n:word;
t,an,sum:real;
begin
write('number of terms n0 equals????');
sum:=0.0;
t:=n0;
for n:=n0 downto 1 do
begin
t:=t-1;
an:=n0*t;
sum:=sum+an;
end;
writeln('s+sum of 1/n^3 terms from n = 1 to n = ',n0);
writeln('s=....',sum);
end.

I'm trying to see where I'm going wrong. Any help would be much appreciated.

2. ### Joe_CoolNever trust a woman or a government.Moderator

Joined:
Jun 30, 2003
Messages:
299,422
598
Is it executing properly? Or coming up with the wrong number? I don't know pascal, but it looks to me like your algorithm is off:

Looks like you're adding up a sum of products, right? Well if that's the case, you're figuring your factorials wrong. It's not a sum - t! is the product t * t-1 * t-2 .... * 3 * 2.

You're using more variables than you need.

3. ### CompiledMonkeyNew Member

Joined:
Oct 26, 2001
Messages:
8,528
0
Location:
Richmond, VA
Factorials, eh? Sounds like you need recursion.

4. ### Joe_CoolNever trust a woman or a government.Moderator

Joined:
Jun 30, 2003
Messages:
299,422
598
does pascal allow recursive function calls? If so, the function can be 1 line long.

Joined:
Oct 26, 2001
Messages:
8,528