Amazon Interview Question
Quality Assurance EngineersCountry: India
Interview Type: In-Person
24. Factorize multiples of 5
5 = 5x1
10 = 5x2
15 = 5x3
20 = 5x2x2
...
25 = 5x5 (one extra 5)
..
50 = 5x5x2 (one extra 5)
...
75 = 5x5x3 (one extra 5)
..
100 = 5x5x4 (one extra 5)
All of the 20 multiples of 5 up to 100 have one 5 each. The multiples 25,50,75 & 100 have one extra 5 each. Hence the total number of 5s = 24. Each of these twenty four 5s when multiplied by 2(which are at least 50 in number) yields a zero and hence the total number of 0s = 24.
The easiest way is to check the maximum power of 5 which can be there in factorial.
10=5x2
If we are calculating the factorial then n!=n x n-1 .... 1= A x 5^p x 2^q, hence it will depend on the value of p which will define the number of zeros.
Steps to find the max number of 5 is
If the number is say Z then max power of 5
p= z/(5) +z/(25) + z/(125)...z/(5^n) till z/(5^n) results a integer number.
int Zeros(int n)
{
int k=0,nb=0;
while(pow(5,k+1)<=n)
{
k=k+1;
nb=nb+n/pow(5,k);
}
return nb;
}
بسم الله الرحمــــــــان الرحيـــــــــــــــــــــــم
انه الحل للمسألة الاعلامية
مع الشكر
public class CalculateNumberOfZeros {
public static void main(String[] args) {
findMaxPowerof5(100);
}
public static void findMaxPowerof5(int num) {
LinkedList<Integer> link = new LinkedList<Integer>();
int totPower = 0;
for (int i = 1; i <= num; i++) {
if (Math.pow(5, i) <= num) {
totPower += num / Math.pow(5, i);
}
}
System.out.println("Number of zero=" + totPower);
}
}
In Excel, cell A1: =FACT(100) Result is 93326215443944200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In cell B1: =LEN(A1)
Copy the significant digits out of A1 (933262154439442)
In cell A2: =A1/933262154439442
In cell B2: =LEN(A2)
Number of zeros = B1-B2=14
This question is worded incorrectly, there are 30 zeros contained in 100!
100! =
9332621544394415268169923885626670049071596826438162146859 29638952175999932299156089414639761565182862536979208272237582511852109168 64000000000000000000000000
The question should be "How many consecutive zeros are there at the end of 100! (100 factorial)."
import java.util.*;
class FactMag
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double no=0,fact=1;
System.out.println("enter a number");
no=sc.nextDouble();
double r=0,c=0;
for(double i=1;i<=no;i++)
{
fact=fact*i;
}
System.out.println(fact+" ");
while(fact>0)
{
r=fact%10;
if(r==0)
c++;
fact=fact/10;
}
System.out.println("no of zeros are "+c);
}
}
import java.util.*;
class FactMag
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double no=0,fact=1;
System.out.println("enter a number");
no=sc.nextDouble();
double r=0,c=0;
for(double i=1;i<=no;i++)
{
fact=fact*i;
}
System.out.println(fact+" ");
while(fact>0)
{
r=fact%10;
if(r==0)
c++;
fact=fact/10;
}
System.out.println("no of zeros are "+c);
}
}
def f(n):
if n==1:
return 0
if n%10==0:
c=0
a=n
while a%10==0:
a=a/10
c=c+1
return c+f(n-1)
if n%5==0:
c=0
a=n
while a%5==0:
a=a/5
c=c+1
return c+f(n-1)
return f(n-1)
100! will have 23 zeroes
- Anonymous January 22, 2014