- Related Questions & Answers
- Maximum XOR using K numbers from 1 to n in C++
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Find product of prime numbers between 1 to n in C++
- Find smallest number n such that n XOR n+1 equals to given k in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Program to find sum of prime numbers between 1 to n in C++
- A data structure for n elements and O(1) operations?
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2) in C++
- Find count of Almost Prime numbers from 1 to N in C++
- C++ find four factors of N with maximum product and sum equal to N .
- Find four factors of N with maximum product and sum equal to N in C++
- Maximum value of |arr[0] – arr[1] - + |arr[1] – arr[2] - + … +|arr[n – 2] – arr[n – 1] - when elements are from 1 to n in C++
- Find duplicates in constant array with elements 0 to N-1 in O(1) space in C++
- Find gcd(a^n, c) where a, n and c can vary from 1 to 10^9 in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

With respect of given two numbers P and Q ( P and Q can be up to 10^6 ) which forms a number N = (P!/Q!). Our task is to reduce N to 1 by performing maximum number of operations possible. Remember, in each operation, one can replace N with N/X if N is divisible by X. Determine the maximum number of operations that can be possible.

A = 7, B = 4

4

N is 210 and the divisors are 2, 3, 5, 7

A = 3, B = 1

2

N is 6 and the divisor are 2,3.

It has been observed that factorization of number P!/Q! is this same as factorization of numbers (Q + 1)*(Q + 2)*…*(P – 1)*P.

It should be noted also, the number of operations will be maximum if we divide N with only with its prime factors. As a result of this, in other words we need to determine the count of prime factors of N which include duplicates also.

Assume count the number of prime factors in the factorization of every number from 2 to 1000000.

At first, implement **Sieve of Eratosthenes** to determine a prime divisor of each of these numbers.It is explained as follows −

Build a list of consecutive integers from 2 to N: (2, 3, 4, …, N).

At first, assume p equal 2, the first prime number.

Beginning from p^2, count up in increments of p and indicate each of these numbers larger than or equal to p^2 itself in the list. So, these numbers can be p(p+1), p(p+2), p(p+3), etc..

Determine the first number larger than p in the list that is not indicated. It has beenseen that if there was no such number, stop. Else, assume p now equal to this number (which indicates the next prime), and again repeat from step 3.

After implementing Sieve of Eratosthenes method we can compute the number of prime factors in the factorization of a implementing the following formula −

primefactors[num] = primefactors[num / primedivisor[num]] + 1 At present, one can implement prefix sum array for prime factors and then answer for the sum on an interval [P, Q].

// CPP program to find maximum // number moves possible #include <bits/stdc++.h> using namespace std; #define N 1000005 // Used to store number of prime // factors of each number int primeFactors1[N]; // Shows Function to find number of prime // factors of each number void findPrimeFactors(){ for (int a = 2; a < N; a++) // Now if a is a prime number if (primeFactors1[a] == 0) for (int b = a; b < N; b += a) // Now increase value by one from // it's preveious multiple primeFactors1[b] = primeFactors1[b / a] + 1; // Build prefix sum // this will be helpful for // multiple test cases for (int a = 1; a < N; a++) primeFactors1[a] += primeFactors1[a - 1]; } // Driver Code int main(){ // Create primeFactors1 array findPrimeFactors(); int P = 7, Q = 4; // required answer cout << primeFactors1[P] - primeFactors1[Q]; return 0; }

4

Advertisements