The Wayback Machine - https://web.archive.org/web/20240930191728/https://www.geeksforgeeks.org/largest-power-k-n-factorial-k-may-not-prime/
Open In App

Largest power of k in n! (factorial) where k may not be prime

Last Updated : 17 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Show Topics
Solve Problem
Medium
19.79%
609

Given two numbers k and n, find the largest power of k that divides n!  
Constraints:  K > 1

Examples: 

Input : n = 7, k = 2
Output : 4
Explanation : 7! = 5040
The largest power of 2 that
divides 5040 is 24.

Input : n = 10, k = 9
Output :  2
The largest power of 9 that
divides 10! is 92.

We have discussed a solution in below post when k is always prime.
Legendre’s formula (Given p and n, find the largest x such that p^x divides n!)
Now to find the power of any non-prime number k in n!, we first find all the prime factors of the number k along with the count of number of their occurrences. Then for each prime factor, we count occurrences using Legendre’s formula which states that the largest possible power of a prime number p in n is ?n/p? + ?n/(p2)? + ?n/(p3)? + ……
Over all the prime factors p of K, the one with the minimum value of findPowerOfK(n, p)/count will be our answer where count is number of occurrences of p in k.
 

C++





Java





Python3





C#





Javascript





Output

4
2

Time Complexity: O(k*log(n))
Auxiliary Space: O(k)

 

Approach 2: –Another approach to find the largest power of a non-prime number k in n! is by using the concept of the number of times a factor appears in a number’s prime factorization. The number of times a factor p appears in n! is given by the sum of the quotients n/p, n/p^2, n/p^3, and so on until the quotient becomes 0.

We can extend this concept to find the largest power of k in n! by considering the prime factorization of k. If k can be written as a product of primes, say k = p1^a1 * p2^a2 * … * pk^ak, then the largest power of k in n! is given by the minimum of the largest powers of each prime pi in k.

C++





Java





Python3





C#





Javascript





Output

4
2

Time complexity:- O(sqrt(k) + log n).

Auxiliary Space: O(logN)



Previous Article
Next Article

Similar Reads

Count numbers in a given range having prime and non-prime digits at prime and non-prime positions respectively
Given two integers L and R, the task is to find the count of numbers in the range [L, R] having prime digits at prime positions and non-prime digits at non-prime positions. Examples: Input: L = 5, R = 22 Output: 7Explanation: The numbers 6, 8, 9, 12, 13, 15, and 17 have prime digits at prime positions and non-prime digits at non-prime positions. In
15 min read
Find the last digit when factorial of A divides factorial of B
We are given two numbers A and B such that B >= A. We need to compute the last digit of this resulting F such that F = B!/A! where 1 = A, B <= 10^18 (A and B are very large).Examples: Input : A = 2, B = 4Output : 2Explanation : A! = 2 and B! = 24. F = 24/2 = 12 --> last digit = 2Input : 107 109Output : 2As we know, factorial function grows
10 min read
Find power of power under mod of a prime
Given four numbers A, B, C and M, where M is prime number. Our task is to compute A raised to power (B raised to power C) modulo M. Example: Input : A = 2, B = 4, C = 3, M = 23Output : 643 = 64 so,2^64(mod 23) = 6 A Naive Approach is to calculate res = BC and then calculate Ares % M by modular exponential. The problem of this approach is that we ca
7 min read
Number of digits in N factorial to the power N
Given a positive integer N, we have to find the total number of digits in the factorial of N raised to the power N, i.e, [Tex](N!)^N [/Tex]Examples: Input: 4 Output: 6 Explanations: [Tex](4!)^4[/Tex] = [Tex](24)^4[/Tex] = 331776. Total number of digits in 331776 is 6.Input: 5Output: 11Explanations: [Tex](5!)^5[/Tex] = [Tex](120)^5[/Tex] = 248832000
4 min read
Find maximum power of a number that divides a factorial
Given two numbers, fact and n, find the largest power of n that divides fact! (Factorial of fact). Examples: Input : fact = 5, n = 2 Output : 3 Explanation: Value of 5! is 120. The largest power of 2 that divides 120 is 8 (or 23 Input : fact = 146, n = 15 Output : 35 The idea is based on Legendre’s formula which finds largest power of a prime numbe
12 min read
Check if N is a Factorial Prime
Given a positive integer N, the task is to check if N is a Factorial prime or not. If it is a factorial prime then print YES else print NO.Note: In mathematics, a factorial prime number is a prime number that is one less than or one more than a factorial of any number. First few factorial primes are 2, 3, 5, 7, 23, 719, 5039, ....Examples: Input: N
9 min read
Check whether the sum of prime elements of the array is prime or not
Given an array having N elements. The task is to check if the sum of prime elements of the array is prime or not. Examples: Input: arr[] = {1, 2, 3} Output: Yes As there are two primes in the array i.e. 2 and 3. So, the sum of prime is 2 + 3 = 5 and 5 is also prime. Input: arr[] = {2, 3, 2, 2} Output: No Approach: First find prime number up to 10^5
10 min read
Prime Number Program | Code to Check whether a number is Prime or not
Given a positive integer n, write a code to check whether the number is prime. What is Prime Number? A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples of the first few prime numbers are {2, 3, 5, ...} Examples: Input: n = 11Output: true Input: n = 15Output: false Input: n = 1Output: false Tab
12 min read
Largest number not greater than N which can become prime after rearranging its digits
Given a number N, the task is to find the largest number less than or equal to the given number N such that on rearranging its digits it can become prime.Examples: Input : N = 99 Output : 98 Explanation : We can rearrange the digits of 98 to 89 and 89 is a prime number. Input : N = 84896 Output : 84896 Explanation : We can rearrange the digits of 8
11 min read
Sum of largest divisor of numbers upto N not divisible by given prime number P
Given a number N and a prime number P, the task is to find the sum of the largest divisors of each number in the range [1, N], which is not divisible by P. Examples: Input: N = 8, P = 2Output: 22Explanation: Numbers are in the range [1, 8].Number Largest Divisor not divisible by P = 21 12 13 34 15 56 37 78 1Sum of all divisors with given constraint
7 min read
Check if the remainder of N-1 factorial when divided by N is N-1 or not
Given an integer N where 1 ? N ? 105, the task is to find whether (N-1)! % N = N - 1 or not.Examples: Input: N = 3 Output: Yes Explanation: Here, n = 3 so (3 - 1)! = 2! = 2 => 2 % 3 = 2 which is N - 1 itself Input: N = 4 Output: No Explanation: Here, n = 4 so (4 - 1)! = 3! = 6 => 6 % 3 = 0 which is not N - 1. Naive approach: To solve the ques
6 min read
Check if a subarray of length K with sum equal to factorial of a number exists or not
Given an array arr[] of N integers and an integer K, the task is to find a subarray of length K with a sum of elements equal to factorial of any number. If no such subarray exists, print “-1”. Examples: Input: arr[] = {23, 45, 2, 4, 6, 9, 3, 32}, K = 5Output: 2 4 6 9 3Explanation:Subarray {2, 4, 6, 9, 3} with sum 24 (= 4!) satisfies the required co
12 min read
Maximum number of bomb blasts that may occur before the thief gets caught
Given an array, arr[] of M integers, where the ith element represents the time after which the ith bomb will blast after dropping it, and three integers N, X, and Y representing the number of adjacent continuous cells on the X-coordinate, and the initial cell positions of a thief and police. The task is to find the maximum number of bomb blasts tha
9 min read
Largest number not exceeding N that does not contain any of the digits of S
Given a numberic string N (1 ≤ |N| ≤ 105) and another numeric string S (1 ≤ |S| ≤ 105), the task is to find the maximum number ≤ N such that it doesn't contain digits of string S. Remove leading zeros, if required. Note: The string S doesn't contain '0'. Examples: Input: N = "2004", S = "2" Output: 1999 Explanation: 2004 has digit 2 which is in the
14 min read
Check if given number is a power of d where d is a power of 2
Given an integer n, find whether it is a power of d or not, where d is itself a power of 2.Examples: Input : n = 256, d = 16 Output : Yes Input : n = 32, d = 16 Output : No Method 1 Take log of the given number on base d, and if we get an integer then number is power of d. C/C++ Code // CPP program to find if a number is power // of d where d is po
9 min read
Compute power of power k times % m
Given x, k and m. Compute (xxxx...k)%m, x is in power k times. Given x is always prime and m is greater than x. Examples: Input : 2 3 3 Output : 1 Explanation : ((2 ^ 2) ^ 2) % 3 = (4 ^ 2) % 3 = 1 Input : 3 2 3 Output : 0 Explanation : (3^3)%3 = 0 A naive approach is to compute the power of x k times and do modulus operation every time. C/C++ Code
15+ min read
Larger of a^b or b^a (a raised to power b or b raised to power a)
Given two numbers [Tex]$a and b$ [/Tex], find which is greater [Tex]a^b or \, b^a [/Tex].If [Tex]a^b > b^a [/Tex], print a^b is greater If [Tex]a^b < b^a [/Tex], print b^a is greater If [Tex]a^b = b^a [/Tex], print Both are equalExamples: Input : 3 5 Output : a^b is greater 3^5 = 243, 5^3 = 125. Since, 243>125, therefore a^b > b^a. Inpu
5 min read
Find the sum of power of bit count raised to the power B
Given an integer, array A. Find the sum of set bits raised to the power A[i] for each element in A[i].Example: Input: N = 3, A[] = {1, 2, 3}Output: 10Explanation:Set bit of each array element is1 = 1 set bit, 2 = 1 set bit, 3 = 2 set bit store each set bit in b[i].Compute sum of power(b[i], i) where i is ranging from 1 to n., that is sum = power(1,
6 min read
Maximize the product of sum and least power by choosing at most K elements from given value and power Arrays
Given two arrays arr[] and powr[] of size N and an integer K. Every element arr[i] has its respective power powr[i]. The task is to maximize the value of the given function by choosing at most K elements from the array. The function is defined as : f(N) = (arr[i1] + arr[i2] + arr[i3]+.....arr[in]) * min(powr[i1], powr[i2], powr[i3], .....powr[in])
10 min read
Maximum power in N levels from K such that defeating boss at level A[i] increases power by B[i]
Given two arrays a[] and b[] of size N and an integer K. The task is to find the maximum power that can be achieved in N levels with initial power K such that after defeating the ith level boss of strength a[i], power is increased by b[i]. In order to defeat a boss the power should be greater than or equal to his power. Examples: Input: N = 3, K =
7 min read
Elements of Array which can be expressed as power of prime numbers
Given an array arr[] of size N, the task is to print all the elements of the Array which can be expressed as power of a prime number. Examples: Input: arr = {2, 8, 81, 36, 100} Output: 2, 8, 81 Explanation: Here 2 = 21, 8 = 23 and 81 = 34 Input: arr = {4, 7, 144} Output: 4, 7 Recommended: Please try your approach on {IDE} first, before moving on to
12 min read
Check if the number is a Prime power number
Given an integer N, the task is to check if the number is a Prime power number. If yes, then print the number along with its power which is equal to N. Else print -1. A prime power is a positive integer power of a single prime number. For example: 7 = 71, 9 = 32 and 32 = 25 are prime powers, while 6 = 2 × 3, 12 = 22 × 3 and 36 = 62 = 22 × 32 are no
8 min read
Count of distinct power of prime factor of N
Given a positive integer N, the task is to find the total number of distinct power of prime factor of the given number N.Examples: Input: N = 216 Output: 4 Explanation: 216 can be expressed as 2 * 22 * 3 * 32. The factors satisfying the conditions are 2, 22, 3 and 32 as all of them are written as distinct positive powers of prime factors.Input: N =
6 min read
Count subarrays having product equal to the power of a given Prime Number
Given an array arr[] of size N and an integer M, the task is to count the number of subarrays having product of its elements equal to the power of M, where M is a prime number. Examples: Input: arr[] = {2, 2, 2, 2}, M = 2Output: 10Explanation: All possible non-empty subarrays having product equal to the power of M = (4 * (4 + 1)) / 2 = 10 Input: ar
8 min read
Sort given Array in descending order according to highest power of prime factors
Given an array arr[] of size N. The task is to sort the elements in arr[] based to their Highest Degree of Expression, in descending order. The Highest Degree of a number is defined as the maximum value in which it can be expressed as the power of its factors. Note: If the numbers have the same degree of expression the element coming first in the o
12 min read
GFact | 2x + 1(where x > 0) is prime if and only if x is a power of 2
A number of the form 2x + 1 (where x > 0) is prime if and only if x is a power of 2, i.e., x = 2n. So overall number becomes 22n + 1. Such numbers are called Fermat Number (Numbers of form 22n + 1). The first few Fermat numbers are 3, 5, 17, 257, 65537, 4294967297, .... An important thing to note is a number of the form 22n + 1) is not always pr
1 min read
Finding power of prime number p in n!
Given a number 'n' and a prime number 'p'. We need to find out the power of 'p' in the prime factorization of n!Examples: Input : n = 4, p = 2 Output : 3 Power of 2 in the prime factorization of 2 in 4! = 24 is 3 Input : n = 24, p = 2 Output : 22 Naive approach The naive approach is to find the power of p in each number from 1 to n and add them. Be
8 min read
Power of a prime number ‘r’ in n!
Given an integer n, find the power of a given prime number(r) in n!Examples : Input : n = 6 r = 3 Factorial of 6 is 720 = 2^4 * 3^2 *5 (prime factor 2,3,5) and power of 3 is 2 Output : 2 Input : n = 2000 r = 7 Output : 330 A simple method is to first calculate factorial of n, then factorize it to find the power of a prime number. The above method c
5 min read
Largest subset where absolute difference of any two element is a power of 2
Given an array arr[] of distinct elements -109 ? ai ? 109. The task is to find the largest sub-set from the given array such that the absolute difference between any two numbers in the sub-set is a positive power of two. If it is not possible to make such sub-set then print -1. Examples: Input: arr[] = {3, 4, 5, 6, 7} Output: 3 5 7 |3 - 5| = 21, |5
10 min read
Prime numbers after prime P with sum S
Given three numbers sum S, prime P, and N, find all N prime numbers after prime P such that their sum is equal to S.Examples : Input : N = 2, P = 7, S = 28 Output : 11 17 Explanation : 11 and 17 are primes after prime 7 and (11 + 17 = 28) Input : N = 3, P = 2, S = 23 Output : 3 7 13 5 7 11 Explanation : 3, 5, 7, 11 and 13 are primes after prime 2.
13 min read