Author Topic: Factorial Output  (Read 8939 times)

0 Members and 1 Guest are viewing this topic.

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 #15 on: June 17, 2011, 12:58:22 am »
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.

It's not terribly difficult to convert to decimal, but yeah.

Example psuedo-code for Hex array->Dec:
Code: [Select]
int arraysize;
char *array;
char *output;
int j = 0;
For(i,i<arraysize,i++) {
stream = *array[i];

if stream>0x0A {
*array[j] = 0x31;
j = j+1;
stream = stream % 10;
}
*array[j] = stream + 0x30;
j = j+1;
}
∂²Ψ    -(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 #16 on: June 17, 2011, 01:02:38 am »
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.

It's not terribly difficult to convert to decimal, but yeah.

Example psuedo-code for Hex array->Dec:
Code: [Select]
int arraysize;
char *array;
char *output;
int j = 0;
For(i,i<arraysize,i++) {
stream = *array[i];

if stream>0x0A {
*array[j] = 0x31;
j = j+1;
stream = stream % 10;
}
*array[j] = stream + 0x30;
j = j+1;
}
That's assuming each byte holds a value from 0-99 (or 0x00 to 0x63 if you prefer). Binary numbers are in base 2, not base 100.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

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 #17 on: June 17, 2011, 01:26:46 am »
Actually, it assumes a value from 0x0 to 0xF, which just proves that BCD is better  :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 #18 on: June 17, 2011, 01:29:11 am »
Oh yeah, cause when you display 0xFFFF, it turns out as 15151515! Nice to know :D
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Factorial Output
« Reply #19 on: June 17, 2011, 03:00:26 am »
I see everyone, I tried this:

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

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

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

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

But it returns "inf" for all numbers :/

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 #20 on: June 17, 2011, 03:09:23 am »
Well, you're not declaring x to be anything but null, so..
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Factorial Output
« Reply #21 on: June 17, 2011, 09:16:15 am »
Well, you're not declaring x to be anything but null, so..

Oh I see, but I also tried this and get the same output:

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

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

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

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

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 #22 on: June 17, 2011, 10:17:09 am »
He means you are not initializing X with a value, you are just going int X or double X, which creates the variable, but it holds no value.

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 #23 on: June 17, 2011, 11:23:15 am »
And both of you seem to be missing the scanf() with argument of &x.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Factorial Output
« Reply #24 on: June 17, 2011, 11:27:17 am »
He means you are not initializing X with a value, you are just going int X or double X, which creates the variable, but it holds no value.

Code: [Select]
double x;
scanf("%lf",&x);

I get it through input.
« Last Edit: June 17, 2011, 11:27:30 am by Scout »

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Factorial Output
« Reply #25 on: June 18, 2011, 04:09:36 pm »
Bump anybody?

Offline Belberith

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 13
  • Rating: +2/-0
    • View Profile
Re: Factorial Output
« Reply #26 on: July 11, 2011, 02:46:57 pm »
Try this? It's just a different method of calculating the factorial.

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

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

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

double factorial(double n)
{
    if ((n == 1) || (n == 0))
    {
        return(1);
    }
    else
    {
        return(n * factorial(n - 1));
    }
}
« Last Edit: July 11, 2011, 02:47:39 pm by Belberith »