
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the smallest number X such that X! contains at least Y trailing zeros in C++
We have to take a number Y, we will find smallest number X, such that X! contains at least Y number of training zeros. For example, if Y = 2, then the value of X = 10. As X! = 3228800. It has Y number of zeros.
We can solve this using binary search. The number of trailing zeros in N! is given by the count of the factors 5 in N!. X can be found using binary search in range [0, 5*Y]
Example
#include<iostream> using namespace std; int factorCount(int n, int X) { if (X < n) return 0; return (X / n + factorCount(n, X / n)); } int findX(int Y) { int left = 0, right = 5 * Y; int N = 0; while (left <= right) { int mid = (right + left) / 2; if (factorCount(5, mid) < Y) { left = mid + 1; }else { N = mid; right = mid - 1; } } return N; } int main() { int Y = 4; cout << "Smallest value of X: " << findX(Y); }
Output
Smallest value of X: 20
- Related Articles
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find smallest values of x and y such that ax – by = 0 in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Count of pairs (x, y) in an array such that x < y in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- Get at least x number of rows in MySQL?
- Find minimum positive integer x such that a(x^2) + b(x) + c >= k in C++
- Convert a number of length N such that it contains any one digit at least 'K' times in C++
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Count of sub-strings that contain character X at least once in C++
- Maximum average of a subarray of size of at least X and at most Y in C++
- Find larger of x^y and y^x in C++
- Find a number x such that sum of x and its digits is equal to given n in C++
- Count trailing zeros in factorial of a number in C++
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++

Advertisements