Author Topic: Factorial Output  (Read 8827 times)

0 Members and 2 Guests are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Factorial Output
« on: June 16, 2011, 05:38:03 pm »
I was trying to make a C Program that returns the factorial of a given number without using any libraries (besides stdio.h).

Code: [Select]
#include <stdio.h>

int main();
int factorial(int n);

int main() {
   
    int n;
   
    scanf("%d",&n);
   
    int i;
    for (i=0;i<n;i++)
    {
        int x;
        scanf("%d",&x);
        printf("%i\n",factorial(x));
    }
   
    return 0;
}

int factorial(int n)
{
    if (n==0)
    {
        return 1;
    }
    int sum = 1;
    int i;
    for (i=n;i>0;i--)
    {
        sum = sum*i;
    }
    return sum;
}

That's my code, the first line of input is supposed to be the number of inputs. Then for each input I output its factorial.

It works fine for me. However, this won't accept it. Any ideas?
« Last Edit: June 16, 2011, 05:40:22 pm by Scout »

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Factorial Output
« Reply #1 on: June 16, 2011, 05:45:33 pm »
You should replace
Code: [Select]
printf("%i",factorial(x));with
Code: [Select]
printf("%i\n",factorial(x));
You forgot the newlines.

EDIT: Ninja'd?
« Last Edit: June 16, 2011, 05:47:14 pm by juju2143 »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Factorial Output
« Reply #2 on: June 16, 2011, 06:06:04 pm »
The numbers are getting way too big. You'd have to use some sort of arbitrary-precision math or something (considering it can go up to 100!)
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline DrDnar

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 546
  • Rating: +97/-1
    • View Profile
Re: Factorial Output
« Reply #3 on: June 16, 2011, 11:24:24 pm »
Try using doubles. Integers will definitely not go up to the factorial of 100, which has 157 digits. You'll lose precision in the lower digits, but that may not be not important.
"No tools will make a man a skilled workman, or master of defense, nor be of any use to him who has not learned how to handle them, and has never bestowed any attention upon them. . . . Yes, [] the tools which would teach men their own use would be beyond price."—Plato's The Republic, circa 380 BC

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Factorial Output
« Reply #4 on: June 17, 2011, 12:06:34 am »
Doubles will definitively do the job. 32-bit integers will only hold up to 12!, while 64-bit will hold up to 20!. Doubles will hold up to 170!.

Note that 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000.
« Last Edit: June 17, 2011, 12:09:37 am by juju2143 »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Factorial Output
« Reply #5 on: June 17, 2011, 12:25:28 am »
Ah, doubles will hold numbers as large as 170! but will you get all of the decimal precision?  I think not, if I remember correctly the double in C only allows for around 16 decimal digits

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Factorial Output
« Reply #6 on: June 17, 2011, 12:28:51 am »
52 bits of precision's not enough indeed. You might want to try binary-coded decimal (BCD) and figure out how to multiply stuff in BCD.

I did some BCD in Axe today to hold up to a lot of digits.
« Last Edit: June 17, 2011, 12:30:01 am by juju2143 »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Factorial Output
« Reply #7 on: June 17, 2011, 12:33:23 am »
Or it might be even easier to just store individual digits in an array (even though that wastes a little bit of space).
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Factorial Output
« Reply #8 on: June 17, 2011, 12:35:40 am »
Yeah, same principle as BCD. You have to store each digit separately and figure out multiplication with that.
« Last Edit: June 17, 2011, 12:36:22 am by juju2143 »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline DrDnar

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 546
  • Rating: +97/-1
    • View Profile
Re: Factorial Output
« Reply #9 on: June 17, 2011, 12:38:38 am »
That may or may not be faster depending on CPU design, memory bandwidth, caching, integer size, et cetera. If you have high-latency memory, BCD may well make things much faster, as the code for screwing with the individual nibbles could be faster than the penalty for a cache miss.

The Z80 has instructions for helping with BCD a little, namely the DAA instruction. You might consider making an Axiom of a generic open source BCD library.
« Last Edit: June 17, 2011, 12:39:00 am by DrDnar »
"No tools will make a man a skilled workman, or master of defense, nor be of any use to him who has not learned how to handle them, and has never bestowed any attention upon them. . . . Yes, [] the tools which would teach men their own use would be beyond price."—Plato's The Republic, circa 380 BC

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Factorial Output
« Reply #10 on: June 17, 2011, 12:39:45 am »
That may or may not be faster depending on CPU design, memory bandwidth, caching, integer size, et cetera. If you have high-latency memory, BCD may well make things much faster, as the code for screwing with the individual nibbles could be faster than the penalty for a cache miss.

The Z80 has instructions for helping with BCD a little, namely the DAA instruction. You might consider making an Axiom of a generic open source BCD library.
I'm talking about Scout's C program ;)
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline DrDnar

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 546
  • Rating: +97/-1
    • View Profile
Re: Factorial Output
« Reply #11 on: June 17, 2011, 12:41:24 am »
Yeah, I first addressed, that, and the I addressed the BCD in Axe. I'm assuming that Scout is coding for a PC, and I thought that it might be worth pointing out that there are performance considerations in which you choose.
« Last Edit: June 17, 2011, 12:42:08 am by DrDnar »
"No tools will make a man a skilled workman, or master of defense, nor be of any use to him who has not learned how to handle them, and has never bestowed any attention upon them. . . . Yes, [] the tools which would teach men their own use would be beyond price."—Plato's The Republic, circa 380 BC

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Factorial Output
« Reply #12 on: June 17, 2011, 12:41:25 am »
Ah, doubles will hold numbers as large as 170! but will you get all of the decimal precision?  I think not, if I remember correctly the double in C only allows for around 16 decimal digits

If it's an integer data type, then yes. A double floating point number will not have the precision, though. As for BCD...

Anyone who uses BCD for any reason other than ease of use should be shot :P
You get all the precision of a floating point number with a little more than half the space efficiency.

If you need to operate on large numbers without math.h, then I recommend using arrays to store your numbers. 100! takes up 66 bytes in integer format, so a custom routine that malloc's an array and then applies the operations to that array will be able to represent massive numbers with no loss of precision. You'll also likely want to improve that algorithm with numbers like 100! :P
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Factorial Output
« Reply #13 on: June 17, 2011, 12:43:52 am »
Ah, doubles will hold numbers as large as 170! but will you get all of the decimal precision?  I think not, if I remember correctly the double in C only allows for around 16 decimal digits

If it's an integer data type, then yes. A double floating point number will not have the precision, though. As for BCD...

Anyone who uses BCD for any reason other than ease of use should be shot :P
You get all the precision of a floating point number with a little more than half the space efficiency.

If you need to operate on large numbers without math.h, then I recommend using arrays to store your numbers. 100! takes up 66 bytes in integer format, so a custom routine that malloc's an array and then applies the operations to that array will be able to represent massive numbers with no loss of precision. You'll also likely want to improve that algorithm with numbers like 100! :P
I recommend storing in decimal because he's going to have to output the values in human-readable format. I assume he won't want to make a routine to extract decimal digits from a large binary number.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Factorial Output
« Reply #14 on: June 17, 2011, 12:46:05 am »
Well yeah. You have to store each decimal digit separately and allow for a seemingly infinite number of digits. That's how BCD works, kinda.
« Last Edit: June 17, 2011, 12:46:38 am by juju2143 »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.