Welcome to Dream.In.Code
Become an Expert!

Join 149,493 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,358 people online right now. Registration is fast and FREE... Join Now!




factorial

 
Reply to this topicStart new topic

factorial, big number factorial

vennik
17 Mar, 2007 - 03:26 AM
Post #1

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 9


My Contributions
Hello,
I search efective algorithm for calculating factorial of big number (f.e. 1000 or even more). It is not easy task, because the result is very huge. I have written some program in C# that calculate it (you can see it here: http://www.geocities.com/ventsislavnikolov/b_n_f.htm), but I think that there is more efficiently decision of the problem (especially regading time consuming). Thank you in advance.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Factorial
17 Mar, 2007 - 04:15 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
in practice large factorials are not calculated becasue they must traverse each element. There are method to estimate large factorials accurately, as well as using prime factorization to calculate precisely, but these are large algorithms and very complicated.

why do you need 1000 factorial?
User is offlineProfile CardPM
+Quote Post

vennik
RE: Factorial
17 Mar, 2007 - 05:59 AM
Post #3

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 9


My Contributions
QUOTE(William_Wilson @ 17 Mar, 2007 - 05:15 AM) *

in practice large factorials are not calculated becasue they must traverse each element. There are method to estimate large factorials accurately, as well as using prime factorization to calculate precisely, but these are large algorithms and very complicated.

why do you need 1000 factorial?


I need to calculate factorial in my job, there is real situation to reach value near to 1000. Do you know where I could find more information for the methods you are talking about?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Factorial
17 Mar, 2007 - 09:25 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
well lets see mathematica does 1000! is well less then 1 second on my 2Ghz pc (my machine is very slow and old).

You are right that there are faster algorithems then just the standard recursive ones.

Now these numbers don't fit into a double: 1000! ruffly = 4.0238726x10^2567

so you need to think about the format you want the number in as that has a lot to do with how you will construct the algorithm.

I know of two aproches. One is to view the thing as a polynomial and use the polynomial algorithm. The other does a ruff and dirty factorization and uses the pow() function to cut down on the multiplications.


User is offlineProfile CardPM
+Quote Post

vennik
RE: Factorial
18 Mar, 2007 - 02:25 AM
Post #5

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 9


My Contributions
I use polymonial wrote multiplier and the product is in kind shown below:
Z=X.Y=X.(y(n-1).10^(n-1)+y(n-2).10^(n-2)+...+y(1).10^1+y(0).10^(0))=X.y(n-2).10^(n-2)+X.y(n-3).10^(n-3)+...+X.y(1).10^(1)+X.y(0).2^(0)
y(i) is consequtive digit of multiplier in n digits decimal area(i=0 is junior, i=n-1 is senior digit). The count of addends is equals to digits of the multiplier. The scheme I use is:

-------> ------>
---------------------- -----------------------------
| | | | |
---------------------- -----------------------------
transitory sum multiplier |-> y(i)
+
----------------------
| |
----------------------
multiplicant

in every step is forming product ((multiplicant).y(i)) and add the result to the current transitory sum (in the beggining transitory sum is 0), after that I shift transitory sum and multiplier one digit rigth-hand side and the operation repeate. After consideration of all multiplier digits, the transitory sum is final result.
In that way I forming multiplication to calculate factorial, but transitory sum grow bigger ang bigger and I use string for its representation. When I need to process it I work in portions that could be represent in C# long int type, and after that every portion is again transfrom in string that is put into string represented transitory sum. Moreover multiplier and multiplicant grow up too in the process of factorial calculation, so I need to represent them like a strings too. Frequently transform from string to long int data type and back into the string is slow. But in truth also maybe I could consider some other algorithm to calculate factorial. But I need it to be accurate (not round).



QUOTE(NickDMax @ 17 Mar, 2007 - 10:25 PM) *

well lets see mathematica does 1000! is well less then 1 second on my 2Ghz pc (my machine is very slow and old).

You are right that there are faster algorithems then just the standard recursive ones.

Now these numbers don't fit into a double: 1000! ruffly = 4.0238726x10^2567

so you need to think about the format you want the number in as that has a lot to do with how you will construct the algorithm.

I know of two aproches. One is to view the thing as a polynomial and use the polynomial algorithm. The other does a ruff and dirty factorization and uses the pow() function to cut down on the multiplications.


User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Factorial
18 Mar, 2007 - 09:08 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
well rather than try to write out an example myself here is link to several algorithems: Fast Factorial Functions I think you will find much more there than I would be able to type out.
User is offlineProfile CardPM
+Quote Post

vennik
RE: Factorial
19 Mar, 2007 - 10:10 AM
Post #7

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 9


My Contributions
Yes, there is useful information. Thank you very much.


QUOTE(NickDMax @ 18 Mar, 2007 - 10:08 AM) *

well rather than try to write out an example myself here is link to several algorithems: Fast Factorial Functions I think you will find much more there than I would be able to type out.


User is offlineProfile CardPM
+Quote Post

alpha02
RE: Factorial
23 Mar, 2007 - 06:38 AM
Post #8

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
QUOTE(vennik @ 19 Mar, 2007 - 02:10 PM) *

Yes, there is useful information. Thank you very much.


QUOTE(NickDMax @ 18 Mar, 2007 - 10:08 AM) *

well rather than try to write out an example myself here is link to several algorithems: Fast Factorial Functions I think you will find much more there than I would be able to type out.



To keep this simple, use this Java code:

CODE
public class test1{
    public static void main (String[] args){
        int fact = 1000; //Number to factorize
        for (int i=1; i<=fact; i++){
            if (fact%i == 0){ //It works
                System.out.print (i + " "); //Print this number
            }
        }
    }
}


This code prints all factors of this number. In this example, the output is:
1 2 4 5 8 10 20 25 40 50 100 125 200 250 500 1000

This post has been edited by alpha02: 23 Mar, 2007 - 06:41 AM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Factorial
23 Mar, 2007 - 06:43 AM
Post #9

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
QUOTE(alpha02 @ 23 Mar, 2007 - 12:38 PM) *

QUOTE(vennik @ 19 Mar, 2007 - 02:10 PM) *

Yes, there is useful information. Thank you very much.


QUOTE(NickDMax @ 18 Mar, 2007 - 10:08 AM) *

well rather than try to write out an example myself here is link to several algorithems: Fast Factorial Functions I think you will find much more there than I would be able to type out.



To keep this simple, use this Java code:

CODE
public class test1{
    public static void main (String[] args){
        int fact = 1000; //Number to factorize
        for (int i=1; i<=fact; i++){
            if (fact%i == 0){ //It works
                System.out.print (i + " "); //Print this number
            }
        }
    }
}


This code prints all factors of this number. In this example, the output is:
1 2 4 5 8 10 20 25 40 50 100 125 200 250 500 1000

he was looking for a factorial, not a factor, these are completely different:
a factorial is of the form:
5! = 5*4*3*2*1
User is offlineProfile CardPM
+Quote Post

alpha02
RE: Factorial
23 Mar, 2007 - 08:38 AM
Post #10

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
QUOTE(William_Wilson @ 23 Mar, 2007 - 10:43 AM) *

QUOTE(alpha02 @ 23 Mar, 2007 - 12:38 PM) *

QUOTE(vennik @ 19 Mar, 2007 - 02:10 PM) *

Yes, there is useful information. Thank you very much.


QUOTE(NickDMax @ 18 Mar, 2007 - 10:08 AM) *

well rather than try to write out an example myself here is link to several algorithems: Fast Factorial Functions I think you will find much more there than I would be able to type out.



To keep this simple, use this Java code:

CODE
public class test1{
    public static void main (String[] args){
        int fact = 1000; //Number to factorize
        for (int i=1; i<=fact; i++){
            if (fact%i == 0){ //It works
                System.out.print (i + " "); //Print this number
            }
        }
    }
}


This code prints all factors of this number. In this example, the output is:
1 2 4 5 8 10 20 25 40 50 100 125 200 250 500 1000

he was looking for a factorial, not a factor, these are completely different:
a factorial is of the form:
5! = 5*4*3*2*1


ah ok. well someone can find my code useul, and for a factorial I thought about:

CODE
public class test1{
    public static void main (String[] args){
        int fact = 1000; //Number to factorize
                int total = 1;
        for (int i=fact; i>0; i--){
            total = total*i;
        }
                System.out.println(total);
    }
}


It outputs the number. However I'm afraid this will not work if the number is too high because of Java's data types limitations, but it is worth the try.

This post has been edited by alpha02: 23 Mar, 2007 - 08:39 AM
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Factorial
23 Mar, 2007 - 09:36 AM
Post #11

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Java's BigInteger class will buy you a little more breathing room, but with some factorial algorithms, it's a losing battle. smile.gif
User is offlineProfile CardPM
+Quote Post

vennik
RE: Factorial
24 Mar, 2007 - 04:59 PM
Post #12

New D.I.C Head
*

Joined: 17 Mar, 2007
Posts: 9


My Contributions
There is algorithms solve problem without using of BigInteger class. Just must some ingenuity.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 05:52PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month