mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   Information & Answers (https://www.mersenneforum.org/forumdisplay.php?f=38)
-   -   Easy Question (https://www.mersenneforum.org/showthread.php?t=12524)

Unregistered 2009-09-30 23:34

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?

Mini-Geek 2009-10-01 00:06

[URL="http://montcs.bloomu.edu/%7Ebobmon/Code/C/two-factorials.shtml"]http://montcs.bloomu.edu/~bobmon/Code/C/two-factorials.shtml[/URL]

Brian-E 2009-10-01 11:48

In case the methods in Mini-Geek'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 Mini-Geek'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.

R.D. Silverman 2009-10-01 13:03

[QUOTE=Unregistered;191562]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?[/QUOTE]

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
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.

Mini-Geek 2009-10-01 16:33

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);
}
}
}[/code]

CRGreathouse 2009-10-01 17:09

[QUOTE=R.D. Silverman;191613]Start with Knuth "The Art of Computer Programming", vol 2.[/QUOTE]

Allow me to disagree. :smile:

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!

xilman 2009-10-01 17:18

[QUOTE=CRGreathouse;191638]Allow me to disagree. :smile:

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![/QUOTE]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

m_f_h 2009-10-01 20:24

[quote=Mini-Geek;191636]Here's an example in Java that uses BigInteger, an arbitrary-precision integer, and prints every factorial up to the size you specify:
[/quote]
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)); }
[/code]
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.

R.D. Silverman 2009-10-01 21:40

[QUOTE=CRGreathouse;191638]Allow me to disagree. :smile:

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![/QUOTE]

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!

CRGreathouse 2009-10-01 22:04

[QUOTE=R.D. Silverman;191653]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![/QUOTE]

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=R.D. Silverman;191653]Do you have a better book? Rivest et al? Aho, Hopcroft, Ullman? Sedgwick?[/QUOTE]

If the OP was really looking for a book, I would sooner recommend Yan's [i]Number Theory for Computing[/i] as its level is more appropriate. But I don't think any book is needed here, unless perhaps a text on introductory programming.

R.D. Silverman 2009-10-02 01:19

[QUOTE=CRGreathouse;191656]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 [i]Number Theory for Computing[/i] as its level is more appropriate. But I don't think any book is needed here, unless perhaps a text on introductory programming.[/QUOTE]

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.


All times are UTC. The time now is 15:43.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.