Help with noob pascal program

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

  1. Snowman

    Snowman I know the Benzes. OT Supporter

    Joined:
    Sep 17, 2002
    Messages:
    3,052
    Likes Received:
    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????');
    readln(n0);
    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);
    write('press enter...');readln;
    end.

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

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,422
    Likes Received:
    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. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Factorials, eh? Sounds like you need recursion. :big grin:
     
  4. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

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

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    If you can have functions, I would imagine you can use recursion. I know nothing about Pascal though. :dunno:
     

Share This Page