Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

In-Class Exrercise-3: (10 Marks) Prime Numbers and Prime Factors: Write a program that takes a whole number input from a user and determines whether it’s prime. If the number is not prime, display its...

1 answer below »
In-Class Exrercise-3: (10 Marks)

Prime Numbers and Prime Factors:
Write a program that takes a whole number input from a user and determines whether it’s prime. If the
number is not prime, display its unique prime factors. Remember that a prime number’s factors are only
1 and the prime number itself. Every number that’s not prime has a unique prime factorization. For
example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied
together, the result is 54. For the number 54, the prime factors output should be 2 and 3.
Use Sets as part of your solution.
Deliverable:
1. Report (2 Marks)
2. Source code (8 marks)
Sample output:
Answered Same Day Oct 13, 2021

Solution

Vaibhav answered on Oct 14 2021
131 Votes
Prime Numbers and Prime Factors.pdf
Report
Prime Numbers and Prime Factors
Prime numbers are a set of those numbers, which have only 2 factors, i.e 1 and the
number itself.
Prime factors of a number are those numbers which are prime and divide the given
number leaving 0 as remainder.
In the Prime.java, the following approach has been used.
1. Ask the number from the user as input until the number is not -1.
2. Check if the number is -1 or not. If it is -1, just exit from the program.
3. If it is a positive integer other than -1 perform following condition check:
a. Use the isPrime() method to check if the number is prime or not. If the
number is prime, this method returns true, else returns false.
. If the number is prime, just display to the user that the number is prime.
c. If the number is not a prime number, then do the following
i. Initialize a variable to 2 (smallest prime factor possible). Call it x.
ii. Check if the number is divisible by x. If so, add x to the set and
keep dividing the number by x till it is divisible by x. This reduces
the overall complexity to compute further prime factors.
iii. Once the number becomes indivisible by x, update the value of x by
1.
iv. Keep repeating this process until...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here