Menu
Topics Index
...
`

Calculate The Lowest Prime Factor of A Number


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to get the lowest prime factor of a number.

Input(Number) Output(Number)
209 11
493 17
91 7



class LowestPrimeFactor
{
    public static void main(String s[])
    {
        int inputNumber = 209;
        int primeFactor = lowestPrimeFactor(inputNumber);
        System.out.println("The factor is " + primeFactor);

    }


    public static int lowestPrimeFactor(int input)
    {
    }
    public static boolean isPrime(int number)
    {
    }
    
    public static int[] getFactors(int number)
    {
    }

}
Topic: Learn Arrays And Loops

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

start for loop from 1 to till the input
check whether i is factor or not..if it is factor than pass that value to the isPrime() than if it is true than we can return directly as small factor bcz we are starting from the 1 so we will get factors from smallest to highest
for(i=1;i<input;i++)
if(input%i==0)
if(isPrime(i))
return i;
*isPrime()
start for loop from 2 till the i<num and check if the i is factor than increase the count
after coming out of the loop check of count==0 than return true else false
getFactors() is optinal

Posted by Uday Kumar    2015-01-21 07:18:56


Firstly i thank Merit Campus .. to make me think .. according to question(requirement) ... Let me share my view reg. hint ..

here in the pic.. they have already divided the burden into 3 methods.. So.. Now We need to do only 3 Steps in 1st method(lowestPrimeFactor) ..
1) Get the info of Factors into array(we use getFactors methods and we get the factors in Ascending order )
2) Check for the prime Number in the Array of Factors we got...
3) we Return the 1st prime Number We Got to the main()..
there ends the Game of 1st method

Now.. we need to complete 2nd and 3rd methods

For isPrime() method
Take variable called count
Use a for loop starts from 2 till number-1 ..
in it use modulo operator ..as number%i == 0 increase count .. else do nothing..

so.. if count is 0 .. return true .. else return false..

For getFactors method ...
Take array variable
  Use a for loop starts from 1 till number .. in it use modulo operator ..as number%i == 0
add element to array .. else do nothing..

Finally return the array..

Posted by ?????????? ?????    2015-01-21 09:13:27


Here Execution starts from main() method and after that variable inputNumber is initialized with value 209 after that it will call the method lowestPrimeFactor() here we are divided the code into 3 parts to get lowest prime factor                                                  1.initially it will cal the lowestPrimeFactor(209) method inside this method we have to take one array of integer variable and which stores the values of getFactors() method to get all factors of the perticular number as                        int[] a=getFactors(209) after that it will go to that perticular method                                              2.here we have to take one array variable to store all factors as int result[] and also one int variable for index value as"r" then take one for-loop from i=2 to i<=number and i++ inside for loop one if condition to check whether it is factor or not like if(number%i==0) and inside if condition we have to store all factors into                      result[r] array variable as result[r]=i and also r++(for increment index).... after for-loop we will return the result[] value as return result; then compiler will go to lowestprimefactor() method there we have to check whether the perticular factor is prime or not  for that we are taking one     for-loop with the range x=0 to x=i.length this is the array length which is return from getFactors method inside the for-loop we have to takeif condition with calling method name as isPrime(number) as a parameter like if(isPrime(i[x])                                                              3.inside this method we have to take one for-loop to check wether it is prime or not  with the range i=2 to i<=number/2 and i++ and inside for-loop we have if condition to check whether it is prime or not like if(number%i==0) and then return false;  stmt  then compiler will return to lowestPrimeFactor method inside this we are storing the lowest prime factor in a variable "lowest" and then one return stmt like return lowest ; then compiler will go main() method and it will print the lowest prime factor of perticular number

Posted by Bhagi Bhagyasri    2015-01-21 11:20:41


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', ?????????? ?????,  for 'Second Correct Comment'. The 'lucky liker' is Varun Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-22 07:10:13


© meritcampus 2019

All Rights Reserved.

Open In App