How to find largest prime factor of 600851475143 in java
How about this solution:
public static long factor(long rc) {
long n = rc;
List<Long> pfactors = new ArrayList<Long>();
for (long i = 2 ; i <= n ; i++) {
while (n % i == 0) {
pfactors.add(i);
n = n / i;
}
}
return pfactors.get(pfactors.size() - 1);
}
Runs fast for me.
Find largest prime factor for the number 600851475143L
try this solution :
public class LargestPrimeFactor
{
public static int largestPrimeFactor(long number) {
int i;
for (i = 2; i <= number; i++) {
if (number % i == 0) {
number /= i;
i--;
}
}
return i;
}
public static void main(String[] args) {
System.out.println(LargestPrimeFactor.largestPrimeFactor(600851475143l));
}
}
The problem was because you have nested loop with very big number, and that what made the loop
Greatest Prime Factor
let primeFactor = x => { if (x === 1 || x === 2) { return x; }
while (x % 2 === 0) { x /= 2; } if (x === 1) { return 2; }
let max = 0; for (let i = 3; i <= Math.sqrt(x); i += 2) { while (x % i === 0) { x /= i; max = Math.max(i, max); } }
if (x > 2) { max = Math.max(x, max); } return max;};
console.log(primeFactor(35));console.log(primeFactor(13195));console.log(primeFactor(27));console.log(primeFactor(1024));console.log(primeFactor(30974914));console.log(primeFactor(600851475143));
Related Topics
How to Automate Drag & Drop Functionality Using Selenium Webdriver Java
Having a 3Rd Party Jar Included in Maven Shaded Jar Without Adding It to Local Repository
Java.Lang.Noclassdeffounderror: Org/Hamcrest/Selfdescribing
What Is Better: Multiple "If" Statements or One "If" with Multiple Conditions
Log4J: How to Use Socketappender
Javafx Launch Another Application
When Does the JPA Set a @Generatedvalue @Id
What Is the Purpose of 'Class.Forname("My_Jdbc_Driver")'
Comparing Arrays in Junit Assertions, Concise Built-In Way
Exception Starting Filter Struts2 - Tried Adding Jar'S, But Same Result
How to Open a Windows Named Pipe from Java
Java.Lang.Illegalstateexception: Not on Fx Application Thread; Currentthread = Thread-4
Calling Superclass from a Subclass Constructor in Java
How to Keep the Iteration Order of a List When Using Collections.Tomap() on a Stream
How to Handle Precision Error with Float in Java
Can't Transparent and Undecorated Jframe in Jdk7 When Enabling Nimbus