Question

Write a function primefactorization(n) that returns a list that represents a complete factorization of n into...

Write a function primefactorization(n) that returns a list that represents a complete factorization of n into primes.For example primfactorization(24)=[2,2,2,3] .Test your function on n=2 through 100.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

IN JAVA !!

import java.util.ArrayList;

public class PrimeFactors {
  
public static void main(String[]args)
{
System.out.println("*** PRIME FACTORS BETWEEN 2 AND 100 ***\n"
+ "---------------------------------------");
for(int i = 2; i <= 100; i++)
{
if(getPrimeFactors(i).isEmpty())
System.out.println(i + ": No Factors");
else
System.out.println(i + ": " + getPrimeFactors(i));
}
System.out.println();
}
  
public static ArrayList<Integer> getPrimeFactors(int n)
{
ArrayList<Integer> factors = new ArrayList<>();
for(int i = 2; i < n; i++)
{
while(n % i == 0)
{
factors.add(i);
n /= i;
}
}
if(n > 2)
factors.add(n);
  
return factors;
}
}

************************************************************************ SCREENSHOT ***************************************************

Add a comment
Know the answer?
Add Answer to:
Write a function primefactorization(n) that returns a list that represents a complete factorization of n into...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT