Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Can /prog/ please comment on my code?

Name: Anonymous 2010-08-02 19:20


class prob3{
   
    static long BigBossPrimeOne;
   
    public static void primeFactor(long n){
    boolean prime = true;
    long n1=0;
    long n2=0;
    for(long i=2;(i<=(n/2))&&(prime!=false);i++)
        if ((n%i)==0){
        prime = false;
        n1=i;
        n2=n/i;   
        }   
    if ((prime==true)&&(n>BigBossPrimeOne))
        BigBossPrimeOne = n;
   
    if (prime==false){
        primeFactor(n1);
        primeFactor(n2);
        }            
    }

    public static void main(String args[]){
 
    primeFactor(600851475143L);
    System.out.println(BigBossPrimeOne);
    }

}


I'm just doing the Euler thing to pass the time and I thought why not post this on /prog/ for some insight.

Using the Java language.

Feel free to give me any advice.

Thanks!

Please don't mind the presentation. It's the first time using Emacs and I was under the clock (did this in 15 minutes).

Name: Anonymous 2010-08-02 19:45

$ time factor 600851475143 | awk '{print $NF}'
6857

real    0m0.010s
user    0m0.004s
sys    0m0.004s


Beat you.

Name: Anonymous 2010-08-02 19:56

>>8

Thanks for teaching me the new command!

My results

nigger@kfc:~/Desktop/Euler$ time java prob3
6857

real    0m0.140s
user    0m0.090s
sys     0m0.010s

I don't know if it factors in but I'm still on a single 1.8GHz core.

Name: Anonymous 2010-08-02 21:10

$ time ./a.out
6857

real    0m0.008s
user    0m0.001s
sys     0m0.007s


/* Find the largest prime factor of 600851475143. */
#include <stdio.h>

int main(int argc, char* argv[])
{ unsigned long long n = 600851475143ULL;
  while(n != 2 && !(n % 2)) n >>= 1;
  while(n != 3 && !(n % 3)) n /= 3;
  for(unsigned long long i = 5, s = 2; i * i <= n; i += 2 + (s ^= 2))
    while(n != i && !(n % i)) n /= i;
  printf("%llu\n", n);
  return 0; }

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List