Prime generator
1
Name:
Anonymous
2012-01-06 11:59
#include <iostream>
using namespace std;
///////World's smallest prime number generator (u jelly? u mirin? lol deal with it homo)
int main() {
int count,n = 0;
cout << "\t>that feel when prime number generator\n\nEnter the number at which you want to stop checking primes--go!\n";
cin >> n;
cout << "\n\n";
for(int i=2;i<=n;++i) {
for(int b=2;b<i;++b) {
if((i%b)==0 && i!=b) ++count;
}
if(count==0) cout << i << " is a prime\n";
count=0;
}
return 0;
}
2
Name:
Anonymous
2012-01-06 12:04
Your code is full of memory leaks. Now go scrub another nigger.
3
Name:
Anonymous
2012-01-06 12:18
>>2
no, it isn't, fag. it works fine in all situations--no memory leak to be found. u jelly?
4
Name:
Anonymous
2012-01-06 12:43
>>1
OP, why do you have to keep
counting
even when you know b is not prime ? You should break the loop as soon as
i % b == 0.
Also, this is not a very optimal way of listing primes.
5
Name:
Anonymous
2012-01-06 12:44
>>4
sorry, when
i is not prime
6
Name:
Anonymous
2012-01-06 12:48
b<i
Oh gosh.
'Does not break when non-prime found
Oh gosh.
'Using int instead of superior unsigned long long
Oh gosh.
So stupid that it's even stupid rather than funny
7
Name:
Anonymous
2012-01-06 13:16
c++
No i'm not jelly, I'm disappointed
8
Name:
Anonymous
2012-01-06 13:26
try writing a sieve, OP
9
Name:
Anonymous
2012-01-06 13:41
>>1
hey OP look at my sieve, written for my custom compiler:
// prime sieve
#define MAX 5
array sieve(1500000);
sieve[1] = 1;
k = 1;
while(k++ < MAX){
n = 2;
while((n*k) <= MAX){
sieve[n*k] = 1;
n++;
}
}
k = 0;
while(k++ < MAX){
if(!sieve[k]){
echo k;
}
}
10
Name:
Anonymous
2012-01-06 14:27
(defun prime (n)
(loop for i from 2 to (round (sqrt n)) do
(when (= (mod n i) 0) (return-from prime -1)))
(return-from prime n))
(defun primes (n)
(loop for i from 1 to n do
(when (> (prime i) 0) (print i))))
(primes 100)
1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
NIL
11
Name:
Anonymous
2012-01-06 14:43
12
Name:
Anonymous
2012-01-06 14:58
>>10
loop
Oh god! Go back to whichever imperative cesspit you crawled from,
please!
13
Name:
Anonymous
2012-01-06 15:22
>>11
You're not very good at trolling are you?
>>12
For autistics like you:
(defun prime (n i)
(if (= (mod n i) 0)
(return-from prime -1)
(if (< i (round (sqrt n)))
(prime n (+ i 1))
(return-from prime n))))
(defun primes (l h)
(if (> (prime l) 0) (print l))
(if (< l h) (primes (+ l 1) h)))
(primes 1 100)
1
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
NIL
14
Name:
Anonymous
2012-01-06 15:28
>>13
that's even uglier than the first. Please use a better language like java, python or C#. We don't want your ugly dead languages around here.
15
Name:
Anonymous
2012-01-06 15:40
(defun prime (n i)
(if (zerop (mod n i))
(return-from prime -1)
(if (< i (round (sqrt n)))
(prime n (1+ i))
(return-from prime n))))
(defun primes (l h)
(if (plusp (prime l)) (print l))
(if (< l h) (primes (1+ l) h))
OMG SO MUCH BETTER
16
Name:
Anonymous
2012-01-06 15:41
(defun prime (n i)
(if (zerop (mod n i))
-1
(if (< i (round (sqrt n)))
(prime n (1+ i))
n)))
(defun primes (l h)
(if (plusp (prime l)) (print l))
(if (< l h) (primes (1+ l) h))
OH GOD I AM GNEIAUS
17
Name:
Anonymous
2012-01-06 15:44
(defun prime (n i)
(if (zerop (mod n i))
-1
(if (< i (isqrt n))
(prime n (1+ i))
n)))
(defun primes (l h)
(if (plusp (prime l)) (print l))
(if (< l h) (primes (1+ l) h))
BITCH
18
Name:
Anonymous
2012-01-06 15:49
primes :: [Integer]
primes = 2:filter (isPrime) [3,5..]
isPrime :: Integer -> Bool
isPrime n = all (\p -> n `mod` p /= 0) $ takeWhile (\p -> p * p <= n) primes
19
Name:
Anonymous
2012-01-06 15:53
(defun prime (n i)
(cond ((zerop (mod n i)) -1)
((< i (isqrt n)) (prime n (1+ i))
(defun primes (l h)
(if (plusp (prime l)) (print l))
(if (< l h) (primes (1+ l) h))
DICKBAG
20
Name:
Anonymous
2012-01-06 16:01
#define PRIMEGEN "..\\worldsgreatestprimegenerator.exe"
system(PRIMEGEN);
21
Name:
Anonymous
2012-01-06 16:22
if you're on plan 9 you can probably do
system("primes 1 100");
I don't understand why e.g. ubuntu doesn't have primes and factor.
22
Name:
Anonymous
2012-01-06 16:53
(loop for n from 0 to 100
when (loop for i from 2 to (isqrt n) never (zerop (mod n i))) do
(print n))
Fuck this thread.
23
Name:
Anonymous
2012-01-06 17:29
>>19
>>17
>>16
>>15
They all look like shit and are slower than the iterative version
24
Name:
Anonymous
2012-01-06 18:14
25
Name:
Anonymous
2012-01-06 18:32
No,
>>23- san, they are the iterative version.
26
Name:
Anonymous
2012-01-06 19:41
$o=1;{(grep!($o%$_),2..$o++)||print$o;redo}
27
Name:
Anonymous
2012-01-06 19:46
>>22
>>19
>>18
>>17 '
>zerop
>plusp
why would you write more characters when (= 0 (...)) is much easier to write.
28
Name:
Anonymous
2012-01-06 19:49
>>27
This is what morons actually believe!
29
Name:
Anonymous
2012-01-06 19:52
>>28
would you like to explain your elitism rather than act like it's something special rather than just a more verbose way of saying it.
30
Name:
Anonymous
2012-01-06 21:08
>>27
If I were designing a Lisp dialect, I'd use
0? and
>0?.
31
Name:
Anonymous
2012-01-06 21:41
>>30
designing a language with numbers?, of course.
it's simple and more consistent with the rest of the system.
32
Name:
Anonymous
2012-01-06 23:28
>>31
That actually works in a lot of the schemes I've used.
(define (0? n) (= n 0))
#<unspecified>
(0? 4)
#f
(0? 0)
#t
33
Name:
Anonymous
2012-01-07 18:07
struct nil;
template<typename x, typename xs>
struct Cons {
typedef x car;
typedef xs cdr;
};
template<int n>
struct Constant {
enum { value = n };
};
template<int n>
struct isPrime {
template<int p, bool acc>
struct f {
template<bool presult, bool accresult>
struct predicate {
enum { result = (n % p != 0) && f<p + 1, accresult>::result };
};
template<bool accresult>
struct predicate<false, accresult> {
enum { result = accresult };
};
enum { result = predicate<(p * p <= n), acc>::result };
};
enum { result = f<2, n>::result };
};
template<>
struct isPrime<2> {
enum { result = true };
};
template<>
struct isPrime<1> {
enum { result = false };
};
template<int from, int to>
struct primesInRange {
template<bool presult, typename dummy_>
struct predicate;
template<typename dummy_>
struct predicate<true, dummy_> {
typedef Cons<Constant<from>, typename primesInRange<from + 1, to>::result> result;
};
template<typename dummy_>
struct predicate<false, dummy_> {
typedef typename primesInRange<from + 1, to>::result result;
};
typedef typename predicate<isPrime<from>::result, void>::result result;
};
template<int n>
struct primesInRange<n, n> {
typedef nil result;
};
template<typename xs>
struct printList {
static void execute() {
printf("%d\n", xs::car::value);
printList<typename xs::cdr>::execute();
};
};
template<>
struct printList<nil> {
static void execute() { };
};
#include <stdio.h>
int main() {
typedef primesInRange<2, 200>::result myPrimes;
printList<myPrimes>::execute();
}
34
Name:
Anonymous
2012-01-07 18:10
I'm puking, thanks /prog/ you just ruined my day
35
Name:
saging slowly
2012-01-07 18:12
def isPrime(n):
for x in range(2, int(n**0.5)+1):
if n % x == 0:
return False
return True
36
Name:
Anonymous
2012-01-07 18:44
require "prime"
1.upto(1000000){|x|puts x if x.prime?}
You all can suck my dick. Why do you keep making shit people have done for you in a more efficient way?
Fuck you all.
37
Name:
Anonymous
2012-01-07 18:53
>>36
Most languages do not have built in utilities for prime numbers. I personally did not know they extended the Fixnum class to check for prime numbers. Thank you for this wisdom.
38
Name:
Anonymous
2012-01-07 18:57
>>36
$ cat > a.rb
require "prime"
1.upto(1000000){|x|x.prime?}
$ time ruby a.rb
real 0m20.909s
user 0m20.895s
sys 0m0.003s
39
Name:
Anonymous
2012-01-07 18:59
The classic APL solution:
(∼R∊R∘.×R)/R←1↓⍳R←1000
40
Name:
Anonymous
2012-01-07 19:11
41
Name:
Anonymous
2012-01-07 19:46
>>36
Go scrub another toilet you stupid shit mental midget.
42
Name:
Anonymous
2012-01-08 1:01
>>41
Go scrub another midget you stupid shit mental toilet.