Prime Factors, Alternative to Diamond Brackets

134 views Asked by At

I am trying to write a program that prints ALL prime factors, as well as specify the smallest prime factor of the number from user input. (e.g. If 12 is given, prime factors are 2, 2, and 3.) I have done a bit of searching, but all results for programs that remember all prime factors seem to use <>. For some reason, this is not recognized. I was wondering if there was an alternative way around this?

Edit: I have successfully printed lowest factors, but am still having trouble with printing all prime factors. Edited code:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class PrimeFactor {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter an integer");
    long n = in.nextLong();
    System.out.println("Smallest prime factor of "+n+" is "+leastFactor(n));
}

public static ArrayList<Integer> leastFactor(long n) {
    ArrayList primeFactors = new ArrayList<Integer>();
    for (int i=2; i<=n; i++) {
        if (n%i==0) {
            primeFactors.add(i);
        }
    }

    if(primeFactors.size() > 0){
        return (primeFactors);
    }
}
}
3

There are 3 answers

5
SmashCode On BEST ANSWER
Scanner z = new Scanner(System.in);
//int n;
long n;//long to display all prime factors.
List primefactors = new ArrayList():
System.out.print("Enter a Number : ");
//n= z.nextInt();
n = z.nextLong();
System.out.print("The Prime Factors of "+n+" are : "); 
int i=2;
while(n>1)
  {
   if(n%i == 0)
    {
     primefactors.add(i);
     n=n/i;
    }
   else
    i++;
  }
System.out.println(Collections.min(primefactors));
primefactors.forEach(System.out::println);//to display all values

Hope you found my code useful.

0
pczeus On

The below modifications fix your code. Note the Set<Integer>. You need to add the type information of what will be contained within the Set. The <> is just shorthand so you don't have to repeat Integer. In other words, you could do either:

Set<Integer> primeFactors = new HashSet<>();

Or:

Set<Integer> primeFactors = new HashSet<Integer>();

Here is your modified code:

import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;
public class PrimeFactor {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter an integer");
        int input = sc.nextInt();
        System.out.println(leastFactor(input));
    }

    public static int leastFactor(int input) {
        Set<Integer> primeFactors = new HashSet<>();
        for (int i=2; i<=input; i++) {
            if (input%i==0) {
                primeFactors.add(i);
            }
        }

        if(primeFactors.size() > 0){
            return primeFactors.toArray(new Integer[primeFactors.size()])[0];
        }
        return 1;
    }
}

Now your primeFactors have been populated. I have added the additional check for the size of primeFactors and if it has elements, return the first element, which will be the smallest. If it has no elements, return 1

0
Andriy Kryvtsun On

BTW: you can easily increase speed of you code with a changing upper cycle value

for (int i=2; i<=input; i++) {
...
}

to square root from your input

int upper = Math.round(Math.sqrt(input));
for (int i = 2; i < upper; i++) {
...
}