mersenneforum.org Easy Question
 Register FAQ Search Today's Posts Mark Forums Read

 2009-09-30, 23:34 #1 Unregistered   24·52·17 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 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?
 2009-10-01, 00:06 #2 Mini-Geek Account Deleted     "Tim Sorbera" Aug 2006 San Antonio, TX USA 17×251 Posts
2009-10-01, 13:03   #4
R.D. Silverman

Nov 2003

22·5·373 Posts

Quote:
 Originally Posted by Unregistered 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 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?
Let me start by asking: Are you a novice programmer in general?

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 multi-precision arithmetic?

Aside from the issue of the choice of programming language, may
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.

 2009-10-01, 16:33 #5 Mini-Geek Account Deleted     "Tim Sorbera" Aug 2006 San Antonio, TX USA 10000101010112 Posts Here's an example in Java that uses BigInteger, an arbitrary-precision 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 Mini-Geek on 2009-10-01 at 16:34
2009-10-01, 17:09   #6
CRGreathouse

Aug 2006

10111001001012 Posts

Quote:
 Originally Posted by R.D. Silverman Start with Knuth "The Art of Computer Programming", vol 2.
Allow me to disagree.

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!

2009-10-01, 17:18   #7
xilman
Bamboozled!

"πΊππ·π·π­"
May 2003
Down not across

1025210 Posts

Quote:
 Originally Posted by CRGreathouse Allow me to disagree. 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!
Seconded.

TACP is an excellent text. Everyone except (many) novice should own a copy and study it carefully. My cop is well-thumbed, 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

2009-10-01, 20:24   #8
m_f_h

Feb 2007

24×33 Posts

Quote:
 Originally Posted by Mini-Geek Here's an example in Java that uses BigInteger, an arbitrary-precision integer, and prints every factorial up to the size you specify:
Maybe the answer the O.P. was looking for is rather something like
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)); }
Of course I know that I could have used "unsigned long long" or mpz instead of "int" in factorial, but w.r.t. n1=1; n2=2; ... it should be just perfect as is.

2009-10-01, 21:40   #9
R.D. Silverman

Nov 2003

22×5×373 Posts

Quote:
 Originally Posted by CRGreathouse Allow me to disagree. 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!
The subject at hand is the coding of arithmetic algorithms.

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 (multi-precise)
arithmetic involved in computing/coding n!

2009-10-01, 22:04   #10
CRGreathouse

Aug 2006

3×52×79 Posts

Quote:
 Originally Posted by R.D. Silverman I was not suggesting a general text about coding, but rather one that discusses numerical algorithms and the (multi-precise) arithmetic involved in computing/coding n!
I don't think the OP needs to read a book about numerical algorithms to discover how to program the factorial function in C. I think a quick pointer on recursive algorithms or loops would be better.

Quote:
 Originally Posted by R.D. Silverman Do you have a better book? Rivest et al? Aho, Hopcroft, Ullman? Sedgwick?
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.

2009-10-02, 01:19   #11
R.D. Silverman

Nov 2003

22×5×373 Posts

Quote:
 Originally Posted by CRGreathouse I don't think the OP needs to read a book about numerical algorithms to discover how to program the factorial function in C. I think a quick pointer on recursive algorithms or loops would be better. 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.
Perhaps I am giving the OP too much credit, but he did not say that
he was a novice programmer; only that he was a novice with C.

 Similar Threads Thread Thread Starter Forum Replies Last Post petrw1 PrimeNet 7 2013-08-14 13:54 Puzzle-Peter Twin Prime Search 0 2011-06-25 07:41 Uncwilly Puzzles 35 2006-11-15 01:07 fetofs Puzzles 10 2006-11-03 03:29 Mini-Geek Puzzles 3 2006-10-19 17:14

All times are UTC. The time now is 14:17.

Fri Sep 25 14:17:43 UTC 2020 up 15 days, 11:28, 1 user, load averages: 1.02, 1.35, 1.39