Finding out the last non zero number in the factorial of a given number

If we have a number like 92928212819319231923192312313212312 (means the size can be any thing) so how can i find the last non-zero number in its factorial. Using STL i wanna to get solved this problem...............
I don't understand the question. No number has a factorial of zero except zero.

And computing the factorial of a number like the one you posted would require more memory and time than all the computers on the earth from its creation until it is swallowed by the sun.
Last edited on
Duoas' post got me thinking, so I did a little searching.
I found this article: http://puhep1.princeton.edu/~mcdonald/examples/QM/lloyd_nature_406_1047_00.pdf
I really love this part:
Figure 1 The ultimate laptop. The ‘ultimate laptop’ is a computer with a mass of 1 kg and a volume of 1 l, operating at the fundamental limits of speed and memory capacity fixed by physics. The ultimate laptop performs 2*mc^2/pi<weird symbol> = 5.4258*10^50 logical operations per second on ~10^31 bits. Although its computational machinery is in fact in a highly specified physical state with zero entropy, while it performs a computation that uses all its resources of energy and memory space it appears to an outside observer to be in a thermal state at ~10^9 degrees Kelvin. The ultimate laptop looks like a small piece of the Big Bang.

It also says that a bit takes 10^9 seconds to travel from one end of the computer to the other (~31.69 years).
Last edited on
Do you mean that for the number 120 you should get 6 or for 24 you should get 4?

If so, just divide the number by 1, 2, 3, ... oo until the result is equal or smaller than 1.

If your result is 1, you have your last number, if it is smalle than 1, it is not a factorial.

int main

Suppose I have a number 5
factorial of this number is 120
the last non-zero number in the factorial of 5 is 2.
so if i want to get the last non-zero number in the factorial of the number 9239012119318902831932 (means a number which can have any number of digits). I want to solve this problem using C++ STL.
Well, then it's simple:

Divide the number by ten until the modulo is not zere. Then you have your last digit.

That's it.

1
2
3
4
5
6
7
while (test!=0)
[
test = factorial%10
factorial = factorial / 10
}

cout <<test;


int main
Above code is wrong. Tell me how can you save such a long number in any data type.
You can save it in an arbitrary precision data type. The above code is right (I suppose) if you overload the necessary operators.
Google "c++ bignum" for arbitrary-precision number libraries. There are a few good ones.

And you still won't be able to generate the factorial of such a large number in your lifetime. But I'm starting to think that by "factoral" you mean digit. int main has it exactly right. Good luck!
I think what he wants is something like:

Input is 5
5! = 120
Last non-zero digit is a 2
There may well be an algorithm that exists that can do this. I haven't searched the 'net, but I have given it some thought.

For example, 5! = 5 * 4 * 3 * 2 * 1. Since 5 * 2 = 10, it follows that for all integers N >= 5, N! can
be written in the form N! = x * 10^k, where x % 10 != 0 and x % 10 is what you are looking for.
Since 5! contains factors 2 and 5 (which yield one zero) and since 10! contains factors 2, 5, and 10 (which yield two zeroes), I think k = trunc( N / 5 ). Further, given a and b such that at least one of the two is even, then ab is even. Since 4 is even and since 4 does not combine with another integer < 10 to form a product evenly divisble by 10 (2 and 5 are already used), it follows that in the formula N! = x * 10^k, x must be even (and non-zero). Which means that x must be 2, 4, 6, or 8.

Not sure if this helps at all. Sounds like an interesting problem.
Topic archived. No new replies allowed.