Prime Factors
Sun Feb 06 2022 04:39:27 GMT+0000 (Coordinated Universal Time)
Saved by @Uttam #java #mathematics #lecture #gfg #geeksforgeeks #efficientmethod #naivemethod #primefactors
Prime factor is the factor of the given number which is a prime number. Factors are the numbers you multiply together to get another number. In simple words, prime factor is finding which prime numbers multiply together to make the original number. Example: The prime factors of 15 are 3 and 5 (because 3×5=15, and 3 and 5 are prime numbers). Naive solution: ----------------- Given a number n, write a function to print all prime factors of n. For example, if the input number is 12, then output should be “2 2 3” and if the input number is 315, then output should be “3 3 5 7”. Following are the steps to find all prime factors: While n is divisible by 2, print 2 and divide n by 2. After step 1, n must be odd. Now start a loop from i = 3 to square root of n. While i divides n, print i and divide n by i, increment i by 2 and continue. If n is a prime number and is greater than 2, then n will not become 1 by above two steps. So print n if it is greater than 2. Efficient Solution: ------------------- 1.) Divisors always appear in pairs 30 : (1,30) , (2,15) , (3,10) , (5,6) 2.) A number n can be written as multiplications of powers of prime factors 12 = 2^2 * 3 450 = 2^1 * 3^2 * 5^2 ________________________ Let (x,y) be a pair x * y = n If x<=y x * x <= n x <= sqrt(n)
Comments