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:23

Oh forgot.

Euler problem 3.

Find the largest prime factor of 600851475143.

Less than a minute to compile.

The above code took about 1.2 seconds on my crappy pc.

Name: Anonymous 2010-08-02 19:25

It's definitely code. Good job, Champ.

Name: Anonymous 2010-08-02 19:29

Can you please comment your code?

Name: Anonymous 2010-08-02 19:33

>>3

Thanks!

>>4

This is only one static method?!

Do you comment in detail how every method works?

I generally just comment the method's function unless it's a big ass huge method.

Name: Anonymous 2010-08-02 19:41

!=false ... ==true ... ==false
(i<=(n/2))&&(prime!=false) ... ((n%i)==0) ... ((...)&&(...))
щ(`Д´щ)

Also, indent your code properly. Sun publishes guidelines; if you must use Java, follow them.

And your choice of primality test is stupid, even for a naïve algorithm.

Name: Anonymous 2010-08-02 19:43

Use HASKAL

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:49

(i<=(n/2))&&(prime!=false) ... ((n%i)==0) ... ((...)&&(...))

I'm afraid I can't fallow. ;_;

And your choice of primality test is stupid, even for a naïve algorithm.

;_; Please explain. I'm such a newb I can't follow?!

Thanks for helping out!

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 20:01

>>9
Use a sieve. Constructing one will give you insight about what's wrong with your prime test.

Name: Anonymous 2010-08-02 20:08

>>5 (>>4)
Well, yeah. I can't see what the hell this is meant to do past the line noise and public static void and complete lack of whitespace in important places.

Name: Anonymous 2010-08-02 20:11

>>11

sieve

As in the algorithm to find prime numbers?

I know I could used that but I made it a point to build the all the algorithms myself however crappy they may be. I know it's not good practice. I just did it for a greater sense of satisfaction!

Thanks

Also I must be dumb....

I just realized that a "prime test" is nothing but an algorithm to test for primarily lol. Yeah I read the title but I didn't link them automatically. Guess that's why I'm autistic ;_;

Thanks for the advice!

Name: Anonymous 2010-08-02 20:15

>>12

I'll try better indentation! Already on the to-do list.

And all it does is return the largest prime number for a given number.

Sorry!

Name: Anonymous 2010-08-02 20:17

>>13
I know I could used that but I made it a point to build the all the algorithms myself
That's what I meant, implement the sieve yourself. You will find some subtle things about your existing test that could have gone better. That n/2 bit is particularly telling; 2 isn't just a number, it's a prime number.

Name: Anonymous 2010-08-02 20:22

Java
homework thread
nigger@kfc
People, how much more obvious do you want it to be?

Name: Anonymous 2010-08-02 20:29

uint64 Projects::EulerProject3() {
    /*
        The prime factors of 13195 are 5, 7, 13 and 29.

        What is the largest prime factor of the number 600851475143 ?
    */
    const uint64 n = 600851475143;
    uint64 primefactor = 1;
    vector<uint64> f = Helper::GetFactors<uint64>(n);

    for (size_t e = 0; e < f.size(); e++) {
        uint64 testval = f[e];
        if (Helper::IsPrime<uint64>(testval)) {
            primefactor = testval;
        }
    }
    return primefactor;
}

template <typename T> vector<T> Helper::GetFactors(T n) {
    vector<T> division;
    vector<T> divisor;
    vector<T> f;

    f.push_back(1);
    T b = static_cast<T>(sqrt((sizeof(T) == 4 ? (float)(n) : (double)(n))));
    for (T c = 2; c <= b; c++) {
        if (n % c == 0) {
            divisor.push_back(c);
            T t2 = n / c;
            if (c != t2)
                division.push_back(t2);
        }
    }
    for (size_t e = 0; e < divisor.size(); e++)
        f.push_back(divisor.at(e));
    for (size_t e = division.size(); e > 0; e--)
        f.push_back(division.at(e - 1));
    f.push_back(n);
    return f;
}
Obviously not the most optimal solution at all, but it performed fairly quick for a nonsieve algorithm.

Name: Anonymous 2010-08-02 20:32

My python implementation is faster. vroooooooooooooooooooom

Name: Anonymous 2010-08-02 20:39

Everyone help >>1 just to piss off >>16.

Name: Anonymous 2010-08-02 20:48

>>1,19
SPAWHBTC

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; }

Name: Anonymous 2010-08-02 21:14

omg, optimized

Name: Anonymous 2010-08-02 21:45

>>21: In function ‘main’:
>>21: warning: ISO C90 does not support ‘long long’
>>21:4:26: error: use of C99 long long integer constant
>>21:7: warning: ISO C90 does not support ‘long long’
>>21:7: error: ‘for’ loop initial declaration used outside C99 mode
>>21:9: warning: ISO C90 does not support the ‘ll’ printf length modifier
>>21: At top level:
>>21:3: warning: unused parameter ‘argc’
>>21:3: warning: unused parameter ‘argv’

Name: Anonymous 2010-08-02 22:02

There's more efficient ways to find prime factors.

Name: Anonymous 2010-08-02 22:05

>>23
Use the current version of ANSI C, then, idiot.

Name: Anonymous 2010-08-02 22:10

>>25
Less bumping boring threads.

Name: Anonymous 2010-08-02 22:29

Bumping lemma:
Any sufficiently shit thread on /prog/ can be bumped any number of times, with the resulting thread remaining shit.

Name: Anonymous 2010-08-02 22:31

>>8,10,21
Hey guys, these times don't mean shit unless you provide times for the versions you're comparing against as well.

Name: Anonymous 2010-08-02 22:40

>>28
If you want meaningful times, compile the source and time it yourself.

Name: Anonymous 2010-08-02 23:37

>>29
That really doesn't change the fact that the posted times are completely meaningless.

Name: Anonymous 2010-08-02 23:43

>>30
Any times times that anyone could post would be meaningless.
That doesn't change the fact that on almost all platforms >>1's code is ridiculously slow and >>21's is the fastest in this thread.

Name: Anonymous 2010-08-03 0:47

>>30
$ time for i in `seq 1000`; do factor 600851475143 | awk '{print $NF}' >/dev/null; done

real    0m8.290s
user    0m4.144s
sys    0m4.976s
$ time for i in `seq 1000`; do ./a.out >/dev/null; done

real    0m2.518s
user    0m0.728s
sys    0m1.652s
$ time for i in `seq 1000`; do java prob3 >/dev/null; done

real    2m49.491s
user    1m33.826s
sys    0m31.478s

Name: Anonymous 2010-08-03 1:34

>>31
That doesn't change the fact that on almost all platforms >>1's code is ridiculously slow and >>21's is the fastest in this thread.
Well I'm not so sure about that. You've just rejected a fine measure, so 'fastest' doesn't have much place to look for meaning left.

>>32
That is more telling, particularly in the contrast between the >>8 and >>21 versions.

Name: Anonymous 2010-08-03 4:50

if you want fast, this is the way to do it:
#include <stdio.h>
int main(void){ puts("6857"); return 0; }

Name: Anonymous 2010-08-03 4:57

>>21 Corrected to optimize space use:
#include "void.h"
mainstart;uquad start,end;cputime(start);
uquad n=strtoull(argv[1]?argv[1]:"600851475143",NULL,10),i=5,s=2;
while((n!=2)&&(!(n%2))) n>>=1;;while((n!=3)&&(!(n%3))) n/=3;;
while((i*i)<=n){while((n!=i)&& (!(n%i)))n/=i;;i+=2+(s^=2);}
cputime(end);printf("%llu\n",n);printf("Elapsed:%llu cycles",end-start);}

Name: Anonymous 2010-08-03 5:22

>>35
What is cputime?

Name: Anonymous 2010-08-03 5:46

#define cputime(timer) ;asm volatile("rdtsc\n\t":"=A"(timer));//uquad timer

Name: Anonymous 2010-08-03 5:57

>>35
>>21
People who program like this don't deserve to get real jobs programming.

Should be cleaner. Opening braces can go on a newline if you like, but they should be there. For such simple programs, using single letter variable names is fine, but normally they should be descriptive and self documenting.

#include <stdio.h>

int main() {
    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;
}

Name: Anonymous 2010-08-03 6:03

>>38

You don't deserve a real job in programming, you forgot to comment your code.

Name: Anonymous 2010-08-03 6:11

>>39
For such a simple program, there's no need to comment. And commenting every line of code with verbose statements is for amateurs.

Real EXPERT PROGRAMMERS document public APIs, keep their code well-factored and self-documenting using descriptive identifier names, and use comments primarily to provide rational for taking certain approaches or explaining why they're doing something that isn't so obvious. They don't go overboard with comments.

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