20090930, 23:34  #1 
2^{2}×2,273 Posts 
Easy Question
I'm a complete noob when it comes to programming in c, but you have to start somewhere. I ran into a question about factorials. Can you use "n!"? It didnt work for me, I instead went about it the long way,
# include <stdio.h> int main (void) { int n, n1 = 1, n2 = 2, n3 = 3, n4 = 4, n5 = 5, n6 = 6, n7 = 7, n8 = 8, n9 = 9, n10 = 10, nResult; printf("n n!\n\n"); printf( "%i %2i\n" , n1 , nResult = n1); printf( "%i %2i\n", n2, nResult = n1 * n2); printf( "%i %2i\n", n3, nResult = n1 * n2 * n3); printf( "%i %2i\n", n4, nResult = n1 * n2 * n3 * n4); printf( "%i %2i\n", n5, nResult = n1 * n2 * n3 *n4 * n5); and so on.... } Whats an easier way? 
20091001, 00:06  #2 
Account Deleted
"Tim Sorbera"
Aug 2006
San Antonio, TX USA
17×251 Posts 

20091001, 11:48  #3 
"Brian"
Jul 2007
The Netherlands
CC5_{16} Posts 
In case the methods in MiniGeek's link are too advanced for the moment, here a few comments which might help you improve your own program:
 It's neater and more generic to have a counting variable to which you add one each time instead of storing 1, 2, 3, ... in individual variables. Then instead of recalculating n1*n2*n3*... from the start in each step it would be more efficient to hold each intermediate result (1, 2, 6, 24, ...) in a variable and multiply that variable by the counting variable each time. So when you have reached 4!=24 you add one to the count and multiply 24 by the new count of 5 to get the next result. You will need to write a loop (hint: for, while or do).  Your integer variable nResult will soon overflow. (The examples in MiniGeek's link don't take this into account either!) You could test to see if overflow has occurred at each stage (hint: try dividing back and see if you get the previous answer) and stop when this happens. If your compiler supports the "long long" type you can continue the process a bit further than with ordinary integers. Last fiddled with by BrianE on 20091001 at 12:24 
20091001, 13:03  #4  
Nov 2003
2^{2}×5×373 Posts 
Quote:
You do realize that the multiplication of two integers can overflow? Do you know how to code recursive functions in any language (i.e. not specifically C?). If not, may I assume that you at least know how to code a simple loop? Do you have any understanding of multiprecision arithmetic? Aside from the issue of the choice of programming language, may I suggest that you read about coding arithmetic algorithms? Start with Knuth "The Art of Computer Programming", vol 2. There are many issues involved in coding n! that are separate from the choice of coding language. 

20091001, 16:33  #5 
Account Deleted
"Tim Sorbera"
Aug 2006
San Antonio, TX USA
4267_{10} Posts 
Here's an example in Java that uses BigInteger, an arbitraryprecision integer, and prints every factorial up to the size you specify:
Code:
import java.math.BigInteger; public class Main { public static void main(String[] args) { factorialWithBigIntNoDiv(100); } public static void factorialWithBigIntNoDiv(long n) { BigInteger fact = BigInteger.ONE; for (BigInteger i = fact; i.compareTo(BigInteger.valueOf(n)) <= 0; i = i.add(BigInteger.ONE)) { fact = fact.multiply(i); System.out.println(i + "! = " + fact); } } } Last fiddled with by MiniGeek on 20091001 at 16:34 
20091001, 17:09  #6  
Aug 2006
2·11·271 Posts 
Quote:
Knuth is excellent, and his "The Art of Computer Programming" is a treasure for the ages. But a beginning programmer should start from something simpler and work up to TAoCP. Its size and cost are likely to discourage! 

20091001, 17:18  #7  
Bamboozled!
"πΊππ·π·π"
May 2003
Down not across
5^{2}×421 Posts 
Quote:
TACP is an excellent text. Everyone except (many) novice should own a copy and study it carefully. My cop is wellthumbed, is falling apart from use, and I've learned a lot from it. That said, it's probably not the first port of call for most programmers. Second, definitely. Paul 

20091001, 20:24  #8  
Feb 2007
2^{4}·3^{3} Posts 
Quote:
Code:
int factorial(int n){ int f=1; while( n>1 ) f *= n; return(f); } main(){ int n; for( n=1; n<10; n++ ) printf( "%d ! = %d\n", n, factorial(n)); } 

20091001, 21:40  #9  
Nov 2003
1D24_{16} Posts 
Quote:
Do you have a better book? Rivest et al? Aho, Hopcroft, Ullman? Sedgwick? I was not suggesting a general text about coding, but rather one that discusses numerical algorithms and the (multiprecise) arithmetic involved in computing/coding n! 

20091001, 22:04  #10  
Aug 2006
2×11×271 Posts 
Quote:
If the OP was really looking for a book, I would sooner recommend Yan's Number Theory for Computing as its level is more appropriate. But I don't think any book is needed here, unless perhaps a text on introductory programming. 

20091002, 01:19  #11  
Nov 2003
2^{2}×5×373 Posts 
Quote:
he was a novice programmer; only that he was a novice with C. 

Thread Tools  
Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Manual Results Question ... an easy one.  petrw1  PrimeNet  7  20130814 13:54 
A probably easy question about PFGW and twins  PuzzlePeter  Twin Prime Search  0  20110625 07:41 
4 not so easy pieces?  Uncwilly  Puzzles  35  20061115 01:07 
Another easy one  fetofs  Puzzles  10  20061103 03:29 
Easy  MiniGeek  Puzzles  3  20061019 17:14 