Rahul Jose me@rahuljose.com

Rahul Jose is a Bangalore based Engineer with a passion for Technology,Startups and Statistics and is currently working on Automotive Embedded Systems

You can contact him at
me[at]rahuljose[dot]com
Dec 05
Permalink

Finding the Number of zeroes in the product of all numbers between 1 and 100

life-src:

rahuljose:

I am sure there is a simpler mathematical solution but this is good enough for now I have always been a brute force guy sadly

#include<stdio.h>
void main()
{
int i;
long double p=1;
for(i=1;i<101;i++)
{
p=p*i;
}
printf("Product of all numbers between 1&100 is %LG \n",p);
}


No Compiler installed no issues head to http://codepad.org/

PS:-

The answer is 152 zeroes.

My takeaway from this is the printf Format placeholder for Long Double-%LG


Actually, there is a better (and more accurate way. Note that your double isn’t large enough to hold a value as large as 100, neither is the unsigned long. But Java does have the BigNumber object that can be used to store numbers larger than you can imagine. What you’d have to do in C is to keep an array to keep track of all those digits and then perform the computation as if you were writing it down on pen and paper, take each thing one-by-one and multiply. I will work on a possible solution to this.

I really screwed this one up should have guessed 152 is way large a number hope this helps someone.100! is definitely a much simpler approach and combined with Wolframalpha the best.What happened here was the answer provided by the program was a approximation of the final result.The data structure is definitely big enough to hold the result no need to fix that.I shall re-post the program with precision later.

PS: The correct result is 24 as pointed out by Manu and Wolframalpha