Last active
March 14, 2021 06:39
-
-
Save dmnugent80/1b045bc5f30ea3979f40 to your computer and use it in GitHub Desktop.
Find Larget Prime Factor
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
public class Test | |
{ | |
public static void main(String[] args) | |
{ | |
LargestPrimeFactor myObject = new LargestPrimeFactor(); | |
long largestPrimeFactor = myObject.largestPrimeFactor(600851475143L); | |
System.out.println("largest prime Factor: " + largestPrimeFactor); | |
} | |
} | |
public class LargestPrimeFactor | |
{ | |
public long largestPrimeFactor(long n){ | |
long curr = n; | |
for (long i=2; i < curr; i++){ | |
if (curr % i == 0){ | |
curr = curr/i; | |
System.out.println("potential largest prime Factor: " + curr + " divisor: " + i); | |
i = 2; | |
if (isPrime(curr)){ | |
return curr; | |
} | |
} | |
} | |
return -1; | |
} | |
public boolean isPrime(long n) { | |
//check if n is a multiple of 2 | |
if (n%2==0) return false; | |
//if not, then just check the odds | |
for(int i=3;i*i<=n;i+=2) { | |
if(n%i==0) | |
return false; | |
} | |
return true; | |
} | |
} | |
/*output: | |
potential largest prime Factor: 8462696833 divisor: 71 | |
potential largest prime Factor: 10086647 divisor: 839 | |
potential largest prime Factor: 6857 divisor: 1471 | |
largest prime Factor: 6857 | |
*/ | |
//More optimal solution: | |
public class PrimeTest | |
{ | |
public static void main(String[] args) | |
{ | |
PrintPrimeFactors myObject = new PrintPrimeFactors(); | |
myObject.primeFactors(600851475143L); | |
} | |
} | |
public class PrintPrimeFactors | |
{ | |
public void primeFactors(long n) | |
{ | |
long largestPrime = 0; | |
// Print the number of 2s that divide n | |
while (n%2 == 0) | |
{ | |
largestPrime = 2; | |
System.out.println(String.format("divisable by 2 : %-8d", largestPrime)); | |
n = n/2; | |
} | |
// n must be odd at this point. So we can skip one element (Note i = i +2) | |
for (int i = 3; i * i <= n; i = i+2) | |
{ | |
// While i divides n, print i and divide n | |
while (n%i == 0) | |
{ | |
largestPrime = i; | |
System.out.println(String.format("divisable by: %-8d", largestPrime)); | |
n = n/i; | |
} | |
} | |
// This condition is to handle the case whien n is a prime number | |
// greater than 2 | |
if (n > 2){ | |
System.out.println(String.format("n greater than 2 : %-8d", n)); | |
largestPrime = n; | |
} | |
System.out.println(String.format("Largest Prime: %-8d", largestPrime)); | |
} | |
} | |
/*output: | |
divisable by: 71 | |
divisable by: 839 | |
divisable by: 1471 | |
n greater than 2 : 6857 | |
Largest Prime: 6857 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public class LargestPrimeFactor { public static void main(String[] args) { System.out.println(largestPrimefactor(10)); } private static long largestPrimefactor(long num) { long n = num; long p = 0; for (int i = 2; i <=n-1; i++) { if(n%i==0 && i>p) { p = i; } else { continue; } } if(p==0) { // This step is for prime numbers which has prime factor equal to itself p = n; } return p; } }