Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Algorithm writing tips?
#4
the isPrime function is not fully functional (in fact, it's horribly slow), as of right now;
- all primes apart from 2 and 3 can be written as 6k+/-1 (But all numbers written as 6k +/- 1 are not primes!)
- even numbers, apart from 2 can be taken out. Since you're not going to include 2 anyway, we may look at every number in binary-form: Every even number has its last bit as 0. Thus:
if ((n & 1) == 1) return false;
This is way faster than returning the modulo of the number. Thing is, you should just skip even numbers anyway. More handy. Faster.

As a sieve looping-technique for this (pseudo):
Code:
long nextPrime(){
-     sets n to the last number stored in sieve,
    solves the 6k +/- 1 = n equation
    if the equation's 6k + 1, set n equal to n + 4 (which makes n = 6k + 5, or 6k - 1)
    else, check if n + 2 is a prime. If it is, add n into the sieve and return n
    
    loop for n = n, then n + 6{
        if n is a prime, add n into the sieve and return n
        if n + 2 is a prime, add n into the sieve and return n
    }
    //oh. Do I have to tell you, if you pineapple the code up, you'll get a free cookie and a never-ending loop? ^,~ Just as a 'lil notice.
    
}

boolean isPrime(long n){
    // if (n < 2) //Hopefully not needed.
        //return false;
    //if ((n & 1) == 1) //skipp'd. Won't be checked by the nextPrime-function anyway.
        //return false;
    if ((n % 5) == 0)
        return false;
    int square of n = (int) Math.sqrt(n); //Most likely no need to be a long.
    loop for prime in prime-sieve until square of n is lower than prime,
        if ((n % prime) == 0)
            return false;
    return true;
}
Reply


Messages In This Thread
Algorithm writing tips? - by KajitiSouls - 2009-01-11, 10:40 PM
Algorithm writing tips? - by Stereo - 2009-01-11, 10:58 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-11, 11:36 PM
Algorithm writing tips? - by Nikkey - 2009-01-12, 09:58 AM
Algorithm writing tips? - by Stereo - 2009-01-12, 11:52 AM
Algorithm writing tips? - by KajitiSouls - 2009-01-12, 03:53 PM
Algorithm writing tips? - by Nikkey - 2009-01-12, 04:19 PM
Algorithm writing tips? - by Stereo - 2009-01-12, 04:50 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-12, 05:25 PM
Algorithm writing tips? - by Nikkey - 2009-01-12, 05:59 PM
Algorithm writing tips? - by GummyBear - 2009-01-12, 08:13 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-12, 10:26 PM
Algorithm writing tips? - by Nikkey - 2009-01-13, 10:28 AM
Algorithm writing tips? - by GummyBear - 2009-01-13, 11:41 PM
Algorithm writing tips? - by Stereo - 2009-01-15, 10:38 AM
Algorithm writing tips? - by Nikkey - 2009-01-15, 11:02 AM
Algorithm writing tips? - by Kiley - 2009-01-16, 02:37 AM
Algorithm writing tips? - by KajitiSouls - 2009-01-18, 01:48 AM
Algorithm writing tips? - by Stereo - 2009-01-18, 04:46 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-18, 05:37 PM
Algorithm writing tips? - by Nikkey - 2009-01-18, 06:26 PM
Algorithm writing tips? - by Stereo - 2009-01-18, 08:02 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-18, 08:29 PM
Algorithm writing tips? - by Stereo - 2009-01-18, 10:45 PM
Algorithm writing tips? - by Nikkey - 2009-01-19, 02:10 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)