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

Pages: 1-4041-

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.

Name: Anonymous 2010-08-03 6:51

>>37 Ok, its now converted to Purebasic4.5 and added a CPU speed for speedstep/dynamic mhz detection.

Procedure GetCycleCount():  !RDTSC  
::ProcedureReturn:EndProcedure;
Procedure CPUSPEED(): Protected A.q,B.q: time_fix2:A = GetCycleCount(): Sleep_(100): B = GetCycleCount():While (B-A)<1: Goto time_fix2:Wend: ProcedureReturn (B-A)/100000:EndProcedure;
Define.q n,i=5,s=2,starttime,endtime:OpenConsole():If CountProgramParameters():n.q=Val(ProgramParameter()):Else:n.q=600851475143:EndIf:starttime=GetCycleCount():
While n<>2 And (Not (n%2)) :n=n>>1::Wend:While n<>3 And (Not (n%3)) :n=n/3::Wend:
While ((i*i)<=n) :While n<>i And (Not (n%i)) :n=n/i::Wend:: s=s!2: i=i+s+2:Wend:endtime=GetCycleCount()-starttime:
PrintN(Str(n)):PrintN("Elapsed:"+Str(endtime)+" cycles at speed "+Str(CPUSPEED())+" MHZ"):

Name: Anonymous 2010-08-03 7:27

>>41
Too bad cause PureBasic is 3 times slower than GCC

Name: Anonymous 2010-08-03 7:56

Old crap from when I did that problem:

import Prelude hiding (all, filter, takeWhile, last)
import Data.List.Stream
n = 600851475143

primeFactors = [x | x <- [3..floor $ sqrt $ fromIntegral n],
               n `mod` x == 0, isPrime x]

main = print $ last primeFactors

primes :: [Int]
primes = 2 : filter isPrime [3,5..]

isPrime :: Int -> Bool
isPrime n = all ((/=0) . mod n) $ takeWhile (\p -> p*p <= n) primes


% time (for i in `seq 1 1000`; do ./a.out >/dev/null; done)
0,26s user 1,09s system 95% cpu 1,412 total
% time (for i in `seq 1 1000`; do ./3 >/dev/null; done)
32,64s user 2,21s system 90% cpu 38,368 total

Name: Anonymous 2010-08-03 7:58

JavaScript conversion(which is faster than java unsurprisingly)
n="600851475143",i=5,s=2;start=new Date();
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);}
end=new Date();alert(n+ "Elapsed:"+(end-start)+" ms");

Name: Anonymous 2010-08-03 11:03

>>40

I think >>39 was joking.

Name: Anonymous 2010-08-03 12:49

>>45
I think >>38 was too, what with his "cleaner" code being less readable than >>21. Some kind of parody of ENTERPRISE coding standards or something.

>>38
int main() is wrong. it should always be either int main(void) or int main(int argc, char *argv[argc]).

Name: Anonymous 2010-08-03 13:13

>>46
int main(int argc, char *argv[argc])
That won't work and you should know it.

Name: Anonymous 2010-08-03 14:42

>>47
*argv[argc] is a null pointer. just saying, faggot.

Name: Anonymous 2010-08-03 14:43

>>47
It does work. The ANSI/ISO/IEC C standard guarantees it.

Name: Anonymous 2010-08-03 17:02

>>1
Euler
using the Java language
( ≖‿≖)

Name: Anonymous 2010-08-03 19:15

>>50

Makes for a more interesting challenge no?

What are you a faggot?

And besides if you ever participated in Euler you'll find that Java ISN'T the most cumbersome and sluggish language used.

Name: Anonymous 2010-08-03 19:22

>>51
Do you mean to say that you've solved every Euler project in every language conceivable, and have found some to be less efficient than Java?

Name: Anonymous 2010-08-03 20:26

>>52

derailed deduction

less efficient than Java

I believe that's 80% the programmers fault.

Name: Anonymous 2010-08-03 20:40

>>52
I can name a few that have no programs you wouldn't be hard pressed to underperform using Java. I'll start with malbolge. Should I go on?

Name: Anonymous 2010-08-03 20:53

>>52

Wait.

Are you implying that Java is the slowest widely used language?

I will give you the benefit of doubt due to my ignorance on the topic but still hardly believable.

Name: Anonymous 2010-08-03 22:16

>>51
No, I've solved a dozen or two problems.  Stop assuming, idiot.

Name: Anonymous 2010-08-03 23:16


include <stdio.h>

int main(void)
{
        unsigned long target = 600851475143L;
        unsigned int i = 1;
        unsigned int x;
        for (i = 1; target > 1; ++i)
        {
                /* already know it ain't divisble by 2, 3, or 5 */
                x = 6*i+1;
                if (0 == target % x)
                {
                        target /= x;
                }
                else
                {
                        x += 4;
                        if (0 == target % x)
                        {
                                target /= x;
                        }
                }
        }
        printf("%u\n", x);
        return 0;
}

project-euler→$ time ./a.out
6857

real    0m0.002s
user    0m0.000s
sys     0m0.004s
project-euler→$

Name: Anonymous 2010-08-04 4:37

>>53
Hardly derailed. Read >>51 again, they must have used Java as well as some other language in order to come to that conclusion.
In fact, >>51 didn't say “participated in Euler using Java”. His conclusion doesn't follow the rest of his post.
>>54
You're multiple negatives are confusing. What?
>>55
I'm not implying anything. In fact, given the absurd proposal in >>52 that >>51 has done that, I'd be implying the exact opposite if I were to imply at all.

Name: Anonymous 2010-08-12 10:13

This is the way!

Name: Anonymous 2010-08-12 14:30

It's not that hard. Here's a naive solution in CL. I like Haskell's ranges, so I decided to implement something similiar...


; euler 10

(defun square (n)
  (* n n))

(defun number-line (a b)
  (let ((numbers
     (make-array 1 :fill-pointer 0 :adjustable t)))
    (do ((i a (1+ i)))
    ((> i b) numbers)
      (vector-push-extend i numbers))))

(defun move-up (item sequence)
  (elt sequence (1+ (position item sequence))))

(defun multiple-p (n b)
  (and (/= n b) (= (mod n b) 0)))

(defun sieve-of-eratosthenes (n)
  (let ((numbers (number-line 2 n)))
    (do ((p 2 (move-up p numbers)))
    ((> (square p) n) numbers)
      (setf numbers (delete-if (lambda (x) (multiple-p x p)) numbers)))))

(defun problem-10 ()
  (reduce #'+ (sieve-of-eratosthenes 2000000)))

Name: ghd hair 2010-08-30 23:45


You can go to the party with so beautiful hair style made by ghd straighteners.Besides that, there are also several styles to choose from within the ghd hair straightener tools, one is the ghd mk4.GHD can not only create smooth, sleek and curly hair but also enable you to straighten your hair and then gives it flips, curls or waves what you want. Make sure that you hold your hair firmly when straightening your hair so that GHD can distribute heat in an even fashion.Since hair straighteners,are based on heat technology, they may damage your hair if they are not used properly.
http://www.ghdhairsonsale.com

Name: Anonymous 2010-08-31 0:58

>>64
You can go to the party with so beautiful hair style made by ghd straighteners.
FUCK YES I'VE ALWAYS WANTED BEAUTIFUL HAIR

also enable you to straighten your hair and then gives it flips, curls or waves what you want
IT JUST GETS BETTER AND BETTER

Name: Anonymous 2010-08-31 4:59

>>65
You do realize that she talks about pubic hair, don't you?

Name: Anonymous 2010-08-31 5:38

>>66
You do realize that she talks about general pubic hair, don't you?

Name: Anonymous 2010-08-31 7:58

>>67
Do you realize that she talks about GENERAL PUBIC LICENSE hair, don't you?

Name: Anonymous 2010-08-31 8:00

>>68

oh wow Shiitchan, why do you remove my [o] and [u] tags inside the [spoiler]?

Name: Anonymous 2010-08-31 8:36

>>69
I actually like it more.

Name: Anonymous 2010-08-31 9:20

>>69
WORKS ON MY MACHINE

Name: Anonymous 2010-08-31 10:23

>>69
Works on my machine. You must not be using Anonix.

Name: Anonymous 2010-08-31 20:13

>>48
Actually, *argv[argc] is a char.
argv[argc] is a null pointer (for their initial values).

Name: Anonymous 2010-12-10 16:58

Name: Anonymous 2011-02-04 15:51

Name: Anonymous 2011-02-17 20:07

that's cool and all, but check my doubles over there

Name: tiffany and company 925 2012-03-06 23:39

http://www.tiffany-cool.com                                            tiffany jewelry    
http://www.tiffany-cool.com/tiffany-silver-rings/discount-tiffany-signature-ring-black.html                                            peridot two teardrop ring
http://www.tiffany-cool.com/tiffany-silver-money-clips.html                                            tiffany and company 925

Name: North Face Pink Ribbon Denali 2012-09-27 21:26

Name: Anonymous 2012-09-27 21:30

>>1
Commented.

/*
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);
    }

}
*/

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