Advertisement
Opteronic

cs j math Trailing Zeroes

Feb 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. /*
  2. Write a program that calculates with how many zeroes the factorial of a given number N has at its end.
  3. Your program should work well for very big numbers, e.g. N = 100000.
  4.  
  5. 5^1 :  4617 ÷ 5 = 923.4, so I get 923 factors of 5
  6. 5^2 :  4617 ÷ 25 = 184.68, so I get 184 additional factors of 5
  7. 5^3 :  4617 ÷ 125 = 36.936, so I get 36 additional factors of 5
  8. 5^4 :  4617 ÷ 625 = 7.3872, so I get 7 additional factors of 5
  9. 5^5 :  4617 ÷ 3125 = 1.47744, so I get 1 more factor of 5
  10. 5^6 :  4617 ÷ 15625 = 0.295488, which is less than 1, so I stop here.
  11. Then 4617! has 923 + 184 + 36 + 7 + 1 = 1151 trailing zeroes.
  12. http://www.purplemath.com/modules/factzero.htm
  13.  */
  14.  
  15. using System;
  16.  
  17. class TrailingZeroes
  18. {
  19.     static void Main()
  20.     {
  21.         int  number = int.Parse(Console.ReadLine());
  22.         int count = 1, sumZeroes = 0, truncated = 0;
  23.        
  24.         do
  25.         {
  26.              truncated = (int)Math.Truncate(number / Math.Pow(5, count));
  27.              sumZeroes += truncated;
  28.              count++;
  29.         }
  30.         while ((truncated) >= 1);
  31.        
  32.         Console.WriteLine(sumZeroes);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement