Calculate Prime Factor in java
The program demonstrate how to find Least common mutiple and Highest common factor in java.
public class
/**
* raviatgmail.com
*
*
*/
private Scanner scanner_ins = null; // To scan user inputed value
private int totalNumber = 0; // totalNumber by user
private int minimumValue = 0; // Minimum value inside the user inputed array
private int lcm = 1;
private int hcf = 1;
private int [] value_array = null; // User entered value
private int [] denominators = {1,9,8,7,6,5,4,3,2};
/**
* Constructor
*/
PrimeFactor(){
scanner_ins = new Scanner(System.in);
}
/**
* getUserInputs()
*
* To Get input from user
*/
private boolean getUserInputs() {
boolean isZero = false;
System.out.println(" Enter the total number of elements (Say n) : ");
totalNumber = scanner_ins.nextInt(); //scan the user inputs into array
value_array = new int[totalNumber]; // initialized the size of array
for(int i =0 ;i < totalNumber; i++) //This for loop used to get the elements from user
{
System.out.print(" Enter the "+(i+1)+"th elements : ");
value_array[i]=scanner_ins.nextInt();
if(value_array[i] == 0)
isZero = true;
}
return isZero;
}
/**
* findMinimumValue()
*
* To find the minimum value from user inputs
*/
private void findMinimumValue() {
int temp =0; // A temporary variable
temp = value_array[0]; // assingning first value of array to temp
for(int i=1; i value_array[i]){
temp = value_array[i]; // changing the temp value to minimum
}
}
minimumValue = temp;
}
/**
* findCommonMultiples()
*
* To find the H C F
*/
private void findCommomMultiples() {
denominators[0] = minimumValue;
int reminder =0; // reminder
boolean isCommonMultiper = false; // is a common multipler
boolean isComputed = false;
for(int denominator : denominators)
{
if(isComputed == false){
for(int value : value_array)
{
reminder = value % denominator;
if(reminder == 0)
{
isCommonMultiper = true;
continue;
}
else
{
isCommonMultiper = false;
break;
}
}
if(isCommonMultiper == true)
{
lcm = lcm * denominator;
for(int i = 0; i < totalNumber ; i++)
{
if(value_array[i] !=0){
value_array[i] = value_array[i] / denominator;
if(value_array[i] == 1)
isComputed = true;
}
}
}
} //end of if(is computed)
}
hcf = lcm;
}
/**
* findLCM()
*
* To calculate LCM from HCF
*/
private void findLCM() {
for(int i=0; i < value_array.length; i++)
{
lcm = lcm * value_array[i];
}
}
public static void main(String[] args) {
// main class where program starts
PrimeFactor primeFactor = new PrimeFactor(); //instance (ins) of Lcm class
boolean isZeroExist = primeFactor.getUserInputs(); //for getting user input (total elements, elements into array)
if(isZeroExist == false){
long startTime = System.currentTimeMillis();
primeFactor.findMinimumValue(); //To find minimum value user inputed
primeFactor.findCommomMultiples(); //To calculated H C F
primeFactor.findLCM();
primeFactor.showDetails((System.currentTimeMillis() - startTime));
}
else{
System.out.println("\n\t\t PRIME FACTORS \n ");
System.out.println("\t H C F is Zero");
System.out.println("\t L C M is Zero");
}
}
/**
* showDetails()
*
* To Display the output to user
*/
private void showDetails(long timeTaken) {
System.out.println("\n\t\t PRIME FACTORS \n ");
System.out.print("\n User inputs [ ");
for(int i = 0 ; i < totalNumber ; i++) //This for loop used to user inputed values
{
System.out.print(" " + (value_array[i] * hcf));
}
System.out.println(" ]");
System.out.print("\n After dividing by common multiples [ ");
for(int i = 0 ; i < totalNumber ; i++) //This for loop used to view value array after iteration
{
System.out.print(" " + value_array[i]);
}
System.out.println(" ]");
System.out.println(" H C F is "+hcf);
System.out.println(" L C M is "+lcm);
System.out.println(" Computation Time : "+timeTaken+" ms");
}
public class
PrimeFactor{
/**
* raviatgmail.com
*
*
*/
private Scanner scanner_ins = null; // To scan user inputed value
private int totalNumber = 0; // totalNumber by user
private int minimumValue = 0; // Minimum value inside the user inputed array
private int lcm = 1;
private int hcf = 1;
private int [] value_array = null; // User entered value
private int [] denominators = {1,9,8,7,6,5,4,3,2};
/**
* Constructor
*/
PrimeFactor(){
scanner_ins = new Scanner(System.in);
}
/**
* getUserInputs()
*
* To Get input from user
*/
private boolean getUserInputs() {
boolean isZero = false;
System.out.println(" Enter the total number of elements (Say n) : ");
totalNumber = scanner_ins.nextInt(); //scan the user inputs into array
value_array = new int[totalNumber]; // initialized the size of array
for(int i =0 ;i < totalNumber; i++) //This for loop used to get the elements from user
{
System.out.print(" Enter the "+(i+1)+"th elements : ");
value_array[i]=scanner_ins.nextInt();
if(value_array[i] == 0)
isZero = true;
}
return isZero;
}
/**
* findMinimumValue()
*
* To find the minimum value from user inputs
*/
private void findMinimumValue() {
int temp =0; // A temporary variable
temp = value_array[0]; // assingning first value of array to temp
for(int i=1; i value_array[i]){
temp = value_array[i]; // changing the temp value to minimum
}
}
minimumValue = temp;
}
/**
* findCommonMultiples()
*
* To find the H C F
*/
private void findCommomMultiples() {
denominators[0] = minimumValue;
int reminder =0; // reminder
boolean isCommonMultiper = false; // is a common multipler
boolean isComputed = false;
for(int denominator : denominators)
{
if(isComputed == false){
for(int value : value_array)
{
reminder = value % denominator;
if(reminder == 0)
{
isCommonMultiper = true;
continue;
}
else
{
isCommonMultiper = false;
break;
}
}
if(isCommonMultiper == true)
{
lcm = lcm * denominator;
for(int i = 0; i < totalNumber ; i++)
{
if(value_array[i] !=0){
value_array[i] = value_array[i] / denominator;
if(value_array[i] == 1)
isComputed = true;
}
}
}
} //end of if(is computed)
}
hcf = lcm;
}
/**
* findLCM()
*
* To calculate LCM from HCF
*/
private void findLCM() {
for(int i=0; i < value_array.length; i++)
{
lcm = lcm * value_array[i];
}
}
public static void main(String[] args) {
// main class where program starts
PrimeFactor primeFactor = new PrimeFactor(); //instance (ins) of Lcm class
boolean isZeroExist = primeFactor.getUserInputs(); //for getting user input (total elements, elements into array)
if(isZeroExist == false){
long startTime = System.currentTimeMillis();
primeFactor.findMinimumValue(); //To find minimum value user inputed
primeFactor.findCommomMultiples(); //To calculated H C F
primeFactor.findLCM();
primeFactor.showDetails((System.currentTimeMillis() - startTime));
}
else{
System.out.println("\n\t\t PRIME FACTORS \n ");
System.out.println("\t H C F is Zero");
System.out.println("\t L C M is Zero");
}
}
/**
* showDetails()
*
* To Display the output to user
*/
private void showDetails(long timeTaken) {
System.out.println("\n\t\t PRIME FACTORS \n ");
System.out.print("\n User inputs [ ");
for(int i = 0 ; i < totalNumber ; i++) //This for loop used to user inputed values
{
System.out.print(" " + (value_array[i] * hcf));
}
System.out.println(" ]");
System.out.print("\n After dividing by common multiples [ ");
for(int i = 0 ; i < totalNumber ; i++) //This for loop used to view value array after iteration
{
System.out.print(" " + value_array[i]);
}
System.out.println(" ]");
System.out.println(" H C F is "+hcf);
System.out.println(" L C M is "+lcm);
System.out.println(" Computation Time : "+timeTaken+" ms");
}
}
prime factor
No comments:
Post a Comment
Your feedback may help others !!!