Question
Java Program Objective Note: This homework does not use polymorphism. We are going to do a math problem instead Find the set of smallest factors
Java Program
Objective
Note: This homework does not use polymorphism. We are going to do a math problem instead
Find the set of smallest factors that compose a number
- Example: n = 90
- Factors = {1, 2, 3, 5, 6, 9, 10, 15, 18, 30, 45, 90 }
- Least Factors = {2,3,3,5}
- That is, 2 * 3 * 3 * 5 = 90
- Notice these are the smallest of the factors?
Part 1
Write a method that returns a List of all factors for a variable n
What are the factors of n?
- They are the numbers from 1 to n that divide evenly into n
- Note: No number greater than n can divide evenly into it
- You can test if something divides evenly with Modulus (%)
- Returns the remainder of the division
Part 2
Write a Method that takes a List of factors and the number n and returns the List of smallest factors
Follow this algorithm (written in pseudocode)
- while(n != 1)
- i = 1
- nextDivFound = false
- while(nextDivFound == false)
- if factors[i] divides evenly into n
- nextDivFound = true
- returnList.add(factors[i])
- n = n / factors[i]
- else i++
- if factors[i] divides evenly into n
Note that the factor[0] is skipped. This will contain the number 1 and will lead to an infinite loop.
Main Function
Read in the value n from the console using any method you prefer.
- Example is Scanner from java.util.*
- Scanner scan = new Scanner(System.in);
- int n = scan.nextInt();
Call your first method to find the factors then output the List it returns. Next call the second method to find the smallest factors and output the List it returns.
nter n: Enter n: 5 Enter n: 12 Factors: 1 2 3 4 6 12 Factors: 1 2 3 5 69 10 15 18 30 45 90 Factors 1 5 Smallest Factors: 2 3 3 5 Smallest Factors: 5Smallest Factors: 2 2 3Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started