Factorials in C
I'm trying to make a program that will complete the factorial of an entered number.
I have partial success right now, but there's a peculiar instance taking place. So.. #include <stdio.h> int main() { .int a, i; .i = 1; .a = 10; .while(i < 10) ...{ ....a = a * i; // I was initially using a *= i, but the same issue was present. ....i = i + 1; // same issue ...} .printf("%d\n", a); } This code will do the factorial, but while(i < a) will result in a huge negative number. this prevents me from initially being able to scanf("%d", &a); to get the value for a, and having the rest of the code yield a!. A quick workaround I thought up was a = 10, so while(i < a) (whereas a has been assigned the value of 10) I still get the wrong answer. There's obviously something I'm missing here with the comparison of i and a. note: I initially tried building my own factorial function by int factorialtest(int x) { .int i; .for(i = 1; i < x; i++) ...x *= i; ...return x; } but the same huge negative number was present when printing the results from the main function int a, b; scanf("%d",&a); b = factorialtest(a); when a was 10, or any other number. |
Quote:
Quote:
You have designed a version of factorial that manipulates three quantities: 1) The entered number, which is the upper limit at which the counter should stop counting 2) The counter, which counts up from 1 to that upper limit 3) The product of all the counter values you have used so far, which will be the result at the end of the loop. You have just two variables for the three quantities, so you are confusing yourself about what each variable is doing at any given point in the code. To keep your existing basic design, you need three variables to hold the three quantities. When doing so, be careful of the initial value of the variable that holds the product of all the values you have multiplied so far. What do you think that should start out as before you have multiplied anything? If you want to use just two variables instead of three, the trick is to count backwards. Then the entered number and the counter can share one variable between them. Either way, the running product needs its own variable. I am intentionally giving you concepts instead of corrected code and I hope other programmers who might join this thread will respect the homework policy and do the same. You have made a start, but not enough of a start that directly correcting your code would be legitimate help. At this point correcting your code would be doing your homework for you. Also, if you post further code, please learn how to use code tags to make the forum software preserve the indenting. Your kludge to preserve indenting was a worthy effort since I assume you never heard of code tags. But the right way is with code tags. |
Thanks for the hint =)
Also, I have no formal training in anything regarding C, and only slightly more than that regarding math, so my terminology only goes so far. I'll update this post in a bit after I mess with it for another couple of hours. Codetag test = updated Code:
Also, as a increases to 20+, the result is quite interesting. In some instances, odd numbers yield a positive result, and even yields an negative result, in some cases, the inverse is true, and in other cases (30,31) 30 yields a larger number than 31, and for (32,33) the results are the same. Even more interesting is that 34 yields 0. |
What about tgamma(a+1)?
Kevin Barry |
Dogs,
The while loop isnt going for 10 loops like you think it is, its going many more! you set a=10 initially and you only want it to do the while comparision 10 times, but each time in the while loop, 'a' gets bigger and bigger far beyond ten, so 'i' will always be less than the forever increasing 'a' until 'a' overflows it's type and goes negative - terminating the while loop, and leaving 'a' negative. to solve this issue, make a copy of 'a' prior to going into the while loop - lets call it 'maxloop'. then the code may look like this. Code:
#include <stdio.h> i hope this helps. |
Quote:
@Tacorama - You posted that right after I updated the last post I made. That code works up to a = 12, but after that come interesting results that aren't the answer I'm looking for. Code:
#include <stdio.h> http://en.wikipedia.org/wiki/Factorial Quote:
|
Dogs,
I dunno how high you need to go, but if its larger than any of the data types you can use, then may you can make your own array of say ints or even ram with 'malloc', then write your own extended math functions to effectively regroup into higher value array positions, that way you could evaluate something like 413! |
Quote:
ETA - I need to go as far as I can, because I can. |
holla
check out for gmp (GNU multiple precision arithmetic library)
it may help |
try long
|
Quote:
If an approximate answer is OK, you can use double. |
All times are GMT -5. The time now is 02:46 AM. |