orget it, nvm
iberian Huskies on the
I've done a glance over on your site via a VPN

  Main Index Search Posts Who's Online Log in LOG
IN

Free Games Forum
:
Technology
:
Programming
:
Genius solution to extremely hard computational mathematical problem

 



jeff
Surgeon / Chief Medical Officer


Feb 4, 2018, 8:08 PM

Post #1 of 2 (1702 views)
Shortcut
Genius solution to extremely hard computational mathematical problem Can't Post

Coded by: jeff

Copyright jeff © 2018

You may only view this code for educational purposes only, ABSOLUTELY NO UNAUTHORIZED COPYING OR DISTRIBUTING OR YOU WILL HEAR FROM MY LAWYER AT ONCE. Frown


package testerPack; 

public class eulerLargestPrimeFactor {

//===========================================================================

//takes the sqrt of the given variable
public static double sqrt(double input) {

double d;

double sqrt = input / 2;

do {
d = sqrt;
sqrt = (d + (input / d)) / 2;
} while ((d - sqrt) != 0);

return sqrt;
}


//===========================================================================

//will return an answer after determining if the variable is prime
public static boolean isPrime(Double dub) {

boolean result = false;

for (double i = 2; i <= eulerLargestPrimeFactor.sqrt(dub); i++) {
if (dub % i == 0) {
result = false;
i = dub;
}
else {
result = true;
}
}

return result;
}

//===========================================================================

public static void main(String[] args) {
double numberA = 600851475143L;
//converts the double from the sqrt calculator into an int by rounding down
//this makes it easier to plug into the for method
int roundedPrime = (int) Math.rint(eulerLargestPrimeFactor.sqrt(numberA));

//we count down from the largest whole numbers that could be a factor of numberA
//if a number leaves no remainder when dividing into whole #s we consider it a factor
//that factor is plugged into our isPrime boolean to determine if it is prime
for (double i = roundedPrime; i > 1; i--) {
if (numberA % i == 0 && eulerLargestPrimeFactor.isPrime(i) == true) {
System.out.println(i);
i = 1;
}
}
}
}

Later..jeff...Cool...
Medical Doctor & Retired TV Technician


Mikey the Diamond Miner
Banished from Programming Forum

Feb 4, 2018, 8:12 PM

Post #2 of 2 (1699 views)
Shortcut
Re: [jeff] Genius solution to extremely hard computational mathematical problem [In reply to] Can't Post

boy oh boy that for loop is a train wreck

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

here's my version that i coughed up in 3.14 seconds:

import java.util.Arrays; 


public class problem3 {
public static boolean[] primes(int limit) {
boolean[] primes = new boolean[limit+2];
Arrays.fill(primes, Boolean.TRUE);

primes[0] = false;
primes[1] = false;

for (int i=2; i<=limit; i++) {
if (primes[i]) {
for (int j=i*i; j<=limit; j+=i) {
if (j > limit || j < 0) break;
primes[j] = false;
}
}
}
return primes;
}

public static void main(String[] args) {
long n = 600851475143L;
int nSqrt = (int)Math.sqrt(n);
boolean[] isPrime = primes(nSqrt);

int i = nSqrt;

while (true) {
if (isPrime[i]) {
if (n % i == 0) break;
}
i--;
}

System.out.println(i);
}
}


(jeff fucked around with this message on Feb 7, 2018, 5:01 AM)

 
 


Search for (options) Free Games Forum Refugees