How can I Calculate the factorial for the large number

8 views (last 30 days)
Hi I wont to Do a program to calculate the large prime numbers (large than 20 or 30 or so..., digits ) by using Wilson's theorem and it most be calculate the factorial of(n-1)! , when n is the large input number I do this program
n=input('input number');
m=1;
b=n-1;
for i=b:-1:1;
m=m*i;
a=mod(m,n);
end
if a== b
disp ('prime');
else
disp ('composite');
end
when I wont to check up to 19 the output is true otherwise the output is not true let we take the number 982451629 it is most be prime, but the output of my program is composite I think the problem in the factorial of this large number is not true

Answers (2)

John D'Errico
John D'Errico on 26 Jan 2015
Edited: John D'Errico on 27 Jan 2015
Use my vpi toolbox , found on the file exchange.
factorial(vpi(100))
ans =
93326215443944152681699238856266700490715968264381621468592963895217
599993229915608941463976156518286253697920827223758251185210916864000000
000000000000000000
A moderately small Mersenne prime...
N = vpi(2)^127 - 1
N =
170141183460469231731687303715884105727
isprime(N)
ans =
1
In case you care, 2 + 1723*17^1717 is also prime, a 2115 digit prime, for those of us who like the number 17.
More interesting is to find the SECOND prime of the form: (n+1)*17^n-1, for n large and odd. The first member of that family which yields a prime is n=41. However, I've not found a second prime of that form, and I've searched as high as roughly 22500, which yields a number with almost 28,000 decimal digits. I'm still looking though, although that search has involved the successor to vpi, called vpij.
(The point is that vpi can enable working with at least moderately large integers.)

abdulrasul sis
abdulrasul sis on 28 Jan 2015
Thank you soooooo much mr. John D'Errico For your interest to my matter
but what do you mean in this word ((In case you care, 2 + 1723*17^1717 is also prime, a 2115 digit prime, for those of us who like the number 17. ))
I used the vip but i cant get the factorial for the large number grater than 20 digits because of my computer hanging and I be forced to restarting my computer
  1 Comment
John D'Errico
John D'Errico on 28 Jan 2015
Edited: John D'Errico on 28 Jan 2015
No. You cannot compute the factorial of a number with 20 digits. That factorial would have at least some sextillions of digits, a number so large you could never store it on any supercomputer made today. I don't care what tool you will use.
So, roughly, if n = 1e20, then factorial(n) will have
n = 1e20;
n*log10(n/exp(1))
ans =
1.95657055180967e+21
digits. Not that the factorial is itself 2 sextillion, but it will have 2 sextillion digits, an immense number of digits. That it hung is no surprise.
You CAN compute the factorial of a number larger than 20 though. I showed exactly that in my answer.
If you intend to use Wilson's theorem to compute the modulus of a factorial, you COULD do so by an iterative scheme, as you were trying to do in your question. However, don't forget, that loop will still run forever, even on the fastest supercomputer. Running a loop for 1e20 iterations (or more) will take a LONG time. Suppose that each iteration through the loop takes 1e-6 seconds. With 3.15e7second in a year...
1e20*1e-6/3.15e7
ans =
3174603.17460317
So a little over 3 million years to terminate.
Your question stated that you wanted to use Wilson's theorem to test for primality of numbers larger than 20 or 30.
mod(factorial(vpi(23-1)),23)
ans =
22
Which is congruent to -1, mod 23. So 23 is prime.
If your goal is to use Wilson's theorem to test if numbers with 20 or 30 digits, like the number 123456789012345678901, are prime numbers, then be serious. There are other (better) ways to test for primality. Wilson's theorem is a poor choice.
And what I meant by the statement that 2+1723*17^1717 is that this is a rather large prime of interesting form that I found using my own tools, so proof that these tools can indeed be used for your purposes. Of course, it depends on how large the numbers you will work with are. No matter what tool you will use, you can exceed the limits of that tool if you try.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!