Complete the print PrimeFactors method below. You are the required to write the
ID: 3832290 • Letter: C
Question
Complete the print PrimeFactors method below. You are the required to write the method recursively. (No loops allowed.) The method prints the prime factor will print out factors of a positive integer. For example, the call printPrimefactor (28) will print out the sequence of values 7, 2, and 2 As another examples, the number 13 is prime, so printPrimefactor (13) will simply print 13. If n lessthanorequalto 1, then the method should print nothing. In solving this problem, you may assume that you have access to a method called 1, will return the largest Prime Factor. The call largest PrimeFactor (n), where n is greater than 1, will return the largest Prime Factor of n.For example, largestPrimefactor (28) return 7 largest Prime Factor (4) returns 2. largestPrimefactor (2) also returns 2. Public static void printprimeFactors (int n) {Explanation / Answer
ANS:-
The quantity of prime components a number can have is constantly not as much as of that number so that there is no compelling reason to emphasize through the number n to locate its biggest prime element.
it additionally prints just the primes - and simply the primes, and in the event that one prime is all the more then once in the item - it will print it the same number of times as that prime is in the item.
Pls find below Method of return Prime:
def get_factors(number):
components = []
for whole number in range(1, number + 1):
in the event that number%integer == 0:
factors.append(integer)
return components
def test_prime(number):
prime = True
for i in range(1, number > 1):
in the event that i!=1 and i!=2 and i!=number:
in the event that number%i == 0:
prime = False
return prime
def test_for_primes(lst):
primes = []
for i in lst:
in the event that test_prime(i):
primes.append(i)
return primes
program begins here
def find_largest_prime_factor(i):
variables = get_factors(i)
prime_factors = test_for_primes(factors)
print prime_factors
print find_largest_prime_factor(22)
print find_largest_prime_factor(- n-)
Prime factor by using Positive integer:-
def prime_factors(n):
Gives back all the prime components of a positive whole number
components = []
d = 2
while n > 1:
while n % d == 0:
factors.append(d)
n/= d
d = d + 1
on the off chance that d*d > n:
on the off chance that n > 1: factors.append(n)
break
return elements
#include <iostream>
#include <math.h>
IsPrime (int n) {
in the event that (n < 2) return false;
in the event that (n < 4) return genuine;
in the event that (n % 2 == 0) return false;
for (int i = 3; i <= int(sqrt(n)) + 1; i += 2)
in the event that (n % i == 0)
return false;
return genuine;
}
int primary () {
long x;
int lpf = 0;/Largest Prime Factor
cin >> x;
for (int n = 0; n <= x; n++)
in the event that (IsPrime(n) && x % n == 0)
lpf = n;
cout << lpf;
}
to print all prime factors of a given number:-
void primeFactors(int n)
{
Print the quantity of 2s that separation n
while (n.2 == 0)
{
printf("%d ", 2);
n = n/2;
} (where n is odd)
one component (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i+2)
{
/While i partitions n, print i and separation n
while (n%i> == 0)
{
printf("%d ", i);
n = n/i;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.