Last active
December 13, 2018 06:14
-
-
Save kylelong/4d442867be1b9c1532375a2fbb191edd to your computer and use it in GitHub Desktop.
Calculates the number of non-prime factors a number has.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by kylel95 on 12/13/18. | |
*/ | |
import java.io.*; | |
import java.util.*; | |
/** | |
* Calculate number of non-prime factors of a number N | |
*/ | |
public class nonprimefactors { | |
public static void main(String [] args) throws IOException { | |
Scanner scan = new Scanner(); | |
long n = scan.nextLong(); | |
for(long i = 0; i < n; i++){ | |
System.out.println(getPrimeCount(scan.nextLong())); | |
} | |
} | |
/** | |
* Calculated number of non-prime numbers of N | |
* @param N number to generate non-prime numbers of N | |
* @return the non-prime factor count | |
*/ | |
public static long getPrimeCount(long N){ | |
long count = 0; | |
for(long i = 1; i <= N; i++){ | |
if( N % i == 0 && !isPrime(i)){ | |
count++; | |
} | |
} | |
count++; //for 1 | |
return count; | |
} | |
/** | |
* | |
* @param N Number to check if its is prime | |
* @return whether N is prime | |
*/ | |
public static boolean isPrime(long N){ | |
for(long i = 2; i <= Math.sqrt(N); i++){ | |
if(N % i == 0){ | |
return false; | |
} | |
} | |
return true; | |
} | |
private static class Scanner { | |
BufferedReader br; StringTokenizer st; | |
public Scanner(Reader in) { br = new BufferedReader(in); } | |
public Scanner() { this(new InputStreamReader(System.in)); } | |
String next() { | |
while (st == null || !st.hasMoreElements()) { | |
try { st = new StringTokenizer(br.readLine()); | |
} catch (IOException e) { e.printStackTrace(); } } | |
return st.nextToken(); } | |
int nextInt() { return Integer.parseInt(next()); } | |
long nextLong() { return Long.parseLong(next()); } | |
double nextDouble() { return Double.parseDouble(next()); } | |
String readNextLine() { | |
String str = ""; | |
try { str = br.readLine(); | |
} catch (IOException e) { e.printStackTrace(); } | |
return str; } | |
int[] readIntArray(int n) { | |
int[] a = new int[n]; | |
for (int idx = 0; idx < n; idx++) { a[idx] = nextInt(); } | |
return a; } | |
long[] readLongArray(int n) { | |
long[] a = new long[n]; | |
for (int idx = 0; idx < n; idx++) { a[idx] = nextLong(); } | |
return a; } | |
} // end Scanner | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment