(´∀`) Prime numbers (,,゚Д゚)
1
Name:
Anonymous
2007-04-15 11:11
ID:00E9fssW
|code]isPrime x = (filter (0 ==) (map (mod x) [2..x-1])) == [][/code]
2
Name:
Anonymous
2007-04-15 11:11
ID:Heaven
lol
3
Name:
Anonymous
2007-04-15 11:27
ID:FfXQ49DI
isPrime x = null $ filter (0 ==) $ map (mod x) [2..x-1]
Next up: pointless primality test.
4
Name:
Anonymous
2007-04-15 11:33
ID:ex6iFldH
>>3
GOA> :pl \ x -> null $ filter (0 ==) $ map (mod x) [2..x-1]
null . filter (0 ==) . liftM2 map mod (enumFromTo 2 . subtract 1)
FAPFAPFAP
5
Name:
ddddddddddddddddddddddd
2007-04-15 11:35
ID:IQKiOh1T
Prelude Data.List> nubBy (((==0).).flip mod) [2..]
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71Interrupted.
6
Name:
Anonymous
2007-04-15 11:37
ID:s5txUzY+
isPrime x = divs x 2 where divs x y = if x == y then True else if mod x y == 0 then False else divs x (y + 1)
7
Name:
Anonymous
2007-04-15 11:40
ID:Heaven
>>6
That's horrible. It's not a one-liner just because you've removed the newlines.
8
Name:
Anonymous
2007-04-15 11:41
ID:1BpXn22L
sub is_prime{$_='x'x pop;/../^/^(..+)\1+$/}
or, if you want something a little faster:
sub is_prime{my$n=pop;map{$n*=$n%$_>0}2..$n**.5;$n>1}
9
Name:
Anonymous
2007-04-15 11:44
ID:Heaven
10
Name:
Anonymous
2007-04-15 11:44
ID:Heaven
11
Name:
Anonymous
2007-04-15 11:45
ID:Heaven
12
Name:
U
2007-04-15 11:50
ID:oOJQGwpP
printNPrimes=.p:@i.
printNPrimes 5
2 3 5 7 11
13
Name:
Anonymous
2007-04-15 11:53
ID:ex6iFldH
>>12
Multi-character function names? In *MY* J? The sure sign of a novice programmer.
14
Name:
Anonymous
2007-04-15 11:56
ID:Heaven
sub factor{my@f,$n=pop;map{$n/=$_,push@f,$_ until$n%$_}2..$n/2;@f}
sub is_prime{!factor pop}
15
Name:
Anonymous
2007-04-15 11:57
ID:s5txUzY+
function isPrime($x) {
for($i=2;$i++<$x;) if(!($x%$i)) return false;
return true;
}
16
Name:
Anonymous
2007-04-15 12:00
ID:Heaven
Whitespace version
17
Name:
Anonymous
2007-04-15 12:01
ID:IQKiOh1T
nubBy (((==0).).flip mod) [2..]
is an infinite list of all prime numbers. Good luck doing that in your faggot language.
18
Name:
Anonymous
2007-04-15 12:01
ID:Heaven
19
Name:
Anonymous
2007-04-15 12:06
ID:Heaven
>>17
good luck doing what
>>14 does in your faggot language
20
Name:
Anonymous
2007-04-15 12:11
ID:oOJQGwpP
>>17
its pretty easy to write in any language actually
21
Name:
Anonymous
2007-04-15 12:36
ID:ex6iFldH
>>19
What does
>>14 do? I can't make anything at all out of it except that it defined two subroutines.
22
Name:
Anonymous
2007-04-15 12:42
ID:Heaven
>>21
factor returns a list of prime factors of a number. if the number is prime, it returns an empty list.
is_prime is pretty obvious.
23
Name:
Anonymous
2007-04-15 16:29
ID:pr2p8hC+
>>15
Very inefficient way to do it in most cases.
#include <math.h>
const size_t NumPrimes = 1000000;
int main(void)
{
size_t i, j;
__int64 cur, number;
__int64 *primes = (__int64 *) 0;
if (primes = new __int64[NumPrimes]) {
for (i = 0; i < NumPrimes; i++) {
primes
[i] = 0;
}
for (primes[i = 0] = 2, number = 3; i < NumPrimes-1; number++) {
__int64 max = (__int64) (ceil(sqrt((long double) number)));
for (j = 0; cur = primes[j]; j++) {
if (number % cur == 0) {
break;
} else if (cur >= max) {
primes[++i] = number;
break;
} else {
continue;
}
}
}
delete[] primes;
primes = (__int64 *) 0;
}
return 0;
}
There you have many primes. You can increase NumPrimes to any number you like, but you 'might' need more RAM. :)
24
Name:
Anonymous
2007-04-15 17:43
ID:Heaven
>>23
so how do you use that to check if 18446744073709551617 is prime?
25
Name:
Anonymous
2007-04-15 17:48
ID:xesyUPI8
26
Name:
Anonymous
2007-04-15 21:32
ID:pr2p8hC+
>>23
Well, you would need a lot of memory to build a list that long, but once you have n primes, finding the next prime is of the order n log(n). The person above advocating the exponential algorithm clearly hasn't got a clue about programming.
27
Name:
Anonymous
2007-04-15 21:33
ID:pr2p8hC+
>>23
Well, you would need a lot of memory to build a list that long, but once you have n primes, finding the next prime is of the order n log(n). The person above advocating the exponential algorithm clearly hasn't got a clue about programming.
28
Name:
Anonymous
2007-04-15 21:34
ID:pr2p8hC+
29
Name:
Anonymous
2007-04-19 5:43
ID:YPBSKL5/
>>1 >>3 >>4
You don't need to go 2..x-1, you only need to go 2..sqrt(x)
30
Name:
Anonymous
2007-05-15 2:36
ID:sveUUF0G
So I did some benchmarking and came to the conclusion that
List.nubBy (\ a b -> 0 == mod b a) [2..]
is about twice as fast as
List.nubBy (((0 ==).).flip mod) [2..]
Why?
31
Name:
Anonymous
2007-05-15 6:03
ID:ySXS+/P2
use Quantum::Superpositions;
sub is_prime{$n=shift;$s=any(2..$n-1);return !eigenstates($s*$s=$n);}
32
Name:
Anonymous
2007-05-15 8:36
ID:eDQwaHkE
p:i. 1000
33
Name:
Anonymous
2007-05-15 9:36
ID:1BpXn22L
#!/usr/bin/perl -l
use Quantum::Superpositions;
my $n=any 2..shift;
print join',',eigenstates($n!=all(eigenstates($n==$n*$n)));
34
Name:
Anonymous
2007-05-15 9:46
ID:Heaven
>31
use Quantum::Superpositions;
sub is_prime{$n=shift;$s=all(2..$n);$s*$s!=$n}
35
Name:
Anonymous
2007-05-15 10:05
ID:rtRZaPe2
isPrime = lambda n: n > 1 and n not in set(x*y for x in range(2, n) for y in range(2, n))
36
Name:
Anonymous
2007-05-15 11:21
ID:Heaven
>>35
Lol, forced indentation of code.
37
Name:
Anonymous
2007-05-15 13:41
ID:fB6+u2Vv
>>7
`where` is not really considered to make a new line and if-then-else is like a function. If you are going to say someone just removed the new-lines then that line has to have a semi-colon in there somewhere.
38
Name:
Anonymous
2007-05-15 14:06
ID:FfXQ49DI
>>37
I meant that in the pull-your-head-out-of-you-ass-and-realize-no-sane-person-really-writes-Haskell-like-that-because-it-would-be-completely-unreadable kind of way.
I really do wonder who trolled who this time, though.
39
Name:
Anonymous
2007-05-15 14:53
ID:M2BoesvI
>>35
def isprime(n): return n > 1 and not any(n % d == 0 for d in xrange(2, n//2+1))
40
Name:
Anonymous
2007-05-15 15:26
ID:ynCmddE9
if-then-else is like a function
This seems like a good time to bring up one of my pet peeves about Haskell. Why _isn't_ if a function? Observe:
if :: Bool -> a -> a -> a
if True then _ = then
if False _ else = else
No ugly keywords fagging my shit up, and unlike if functions in other languages you don't have to worry about both then and else both being evaluated when they could potentially be computationally expensive - thanks to Haskell's laziness you're guaranteed only one will be.
41
Name:
Anonymous
2007-05-15 15:33
ID:Heaven
>>40
Oh, I just realized it really isn't a function. I'd like to know the reason, too.
42
Name:
Anonymous
2007-05-15 15:38
ID:fB6+u2Vv
>>40
I think it's because if-then-else looks nice than any real reason. Reasonable name for your function there could be ifte.
43
Name:
Anonymous
2007-05-15 15:41
ID:Heaven
>>42
But is there a need for `then' and `else'? Lisp does just fine without them.
44
Name:
Anonymous
2007-05-15 18:46
ID:Heaven
>>43
No, there is no need for it. It just looks nice.
45
Name:
Anonymous
2009-01-14 14:11
Satori
47
Name:
Anonymous
2011-02-02 23:09
48
Name:
Anonymous
2011-02-04 13:36