90% of /prog/ cannot write this BASIC PROGRAM
1
Name:
Anonymous
2008-05-08 20:11
Task: Using your language of choice, implement a program that will a) Prompt the user for 2 numbers, and b) output the number of prime numbers between the two numbers (inclusive.)
Output: A B N
2
Name:
Anonymous
2008-05-08 20:26
thats not that hard, but im not spending any time writing it
>prompt user for number
>input number into m and n
>call count primes
count primes:
>loop through m to n with i
>call isprime(i)
>if isprime(i) is true count + 1
>return count
isprime :
if i < 1 return no
if i == 1 return no
if lowestdenominator i == i return yes
lowestdenominator:
loop through 2 to i with k
if k divides i return k
if k^2 > i return i
else lowestdenominator k+1
that should work
3
Name:
Anonymous
2008-05-08 20:34
>>2
that should be divisor, not denominator
my bad
^lowestdivisor
4
Name:
Anonymous
2008-05-08 20:36
These "challenges" are stupid.#include <math.h>
#include <stdio.h>
int is_prime(int n) {
int i, m=sqrt(n);
for(i=2; i<=m; i++) {
if (n%i == 0)
return 1;
}
return 0;
}
int main(int argc, char** argv) {
int a, b, i, num;
scanf("%d",&a);
scanf("%d",&b);
for(i=a; i<=b; i++) {
if(is_prime(i)) {
num++;
}
}
printf("%d %d %d", a, b, num);
return 0;
}
5
Name:
Anonymous
2008-05-08 20:38
>>4
my bad,
0 and
1 should be swapped in
is_prime()
6
Name:
Anonymous
2008-05-08 20:41
>>5
and read that as
int a, b, i, num=0 ; of course
7
Name:
Anonymous
2008-05-08 20:41
>>4-5
I failed, didn't i :(
8
Name:
Anonymous
2008-05-08 20:48
o hi!
#include <stdlib.h>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
int first, second, counter = 0, primecount = 0;
string s = "Computing";
char Junk[2];
void find_primes(int, int);
int main(int argc, char** argv) {
cout << "Please enter the first number: ";
cin >> first;
cout << "Please enter the second number: ";
cin >> second;
find_primes(first, second);
while ((first >= 0) && (second >= 0))
{
cout << "Press [ENTER] to continue." << endl;
cin.getline(Junk, 1, '\n');
cin.getline(Junk, 1, '\n');
cout << "\033[2J\033[H";
s = "Computing";
primecount = 0;
cout << "Please enter the first number: ";
cin >> first;
cout << "Please enter the second number: ";
cin >> second;
find_primes(first, second);
}
return (EXIT_SUCCESS);
timecounter = 0;
}
void find_primes(int a, int b)
{
for (int i = a; i <= b; i ++)
{
counter = 0;
for (int j = 1; j <= i; j ++)
{
if ((i % j) == 0)
{
counter++;
}
}
if (counter <= 2)
{
primecount++;
}
}
cout << "\033[2J\033[H";
cout << "First: " << a << " Second: " << b << " #Primes: " << primecount << endl;
}
9
Name:
Anonymous
2008-05-08 21:04
Come on, guys. How about using a primality algorithm that runs in better than O(sqrt(n)) time?
10
Name:
Anonymous
2008-05-08 21:06
Why do I do these /prog/ challenges? Its like I can't resist a chance to show off my baby, my Haskell. HELP ME!!!
module Main where
import Control.Monad
primes = map head $ iterate seive [2 ..]
where seive (x:xs) = [n | n <- xs, not $ x `divides` n]
a `divides` b = b `mod` a == 0
main :: IO ()
main = do [a, b] <- replicateM 2 $ liftM read $ getLine
let primes' = takeWhile (<b) $ dropWhile (<a) primes
print $ length primes'
11
Name:
Anonymous
2008-05-08 21:14
>>10
It is because you are a Nomad. Haskell Nomad.
12
Name:
Anonymous
2008-05-08 22:24
>>10
how would your haskell do this?
main(){for(int c;c;printf(++c?" %02x ":"\n",c=getchar()));}
13
Name:
Anonymous
2008-05-08 22:36
14
Name:
Anonymous
2008-05-08 22:40
>>13
only sepplesfags still use pre-c99.
15
Name:
Anonymous
2008-05-08 22:45
16
Name:
Anonymous
2008-05-08 22:57
>>14
only people confined to reality still use pre-c99
ftfy
17
Name:
Anonymous
2008-05-08 23:08
>>16
only if your reality is limited to the inside of an anus.
18
Name:
Anonymous
2008-05-08 23:11
>>17
It is, yet is is hax'd
19
Name:
Anonymous
2008-05-08 23:49
20
Name:
Anonymous
2008-05-09 4:08
>>10
You stupid, you crushed my dreams! I see this thread and I'm like ''hell, I'll show this guy my Haskell skills`` but then I see your post and I see that there is nothing left for me to do. Damn you, ;_;
21
Name:
Anonymous
2008-05-09 5:13
22
Name:
Anonymous
2008-05-09 6:08
(define trial-divide-max 100)
(define fermat-test-max 20)
(define (modpow x e m)
(do ((a 1 (if (odd? e) (remainder (* a x) m) a))
(x x (remainder (* x x) m))
(e e (quotient e 2)))
((zero? e) a)))
(define (is-prime? x)
(call-with-current-continuation
(lambda (k)
(if (< x 2)
#f
(do ((i 3 (+ i 2)))
((> i trial-divide-max)
(do ((i 2 (+ i 1)))
((> i fermat-test-max) #t)
(if (not (= i (modpow i x x))) (k #f))))
(if (> (* i i) x) (k #t))
(if (zero? (remainder x i)) (k #f)))))))
(display "Please enter two integers.") (newline)
(display
(let ((a (read)) (b (read)))
(if (not (and (integer? a) (integer? b)))
"I said two *integers*."
(let ((low (min a b)) (high (max a b)))
(do ((i (if (even? low) (+ 1 low) low) (+ i 2))
(c 0 (if (is-prime? i) (+ c 1) c)))
((> i high) (if (and (<= low 2) (>= high 2)) (+ c 1) c)))))))
(newline)
There might be a better solution using the π function and the Fermat test isn't perfect, but whatever.
23
Name:
Anonymous
2008-05-09 6:23
>>12
It wouldn't because Haskell doesn't let you use uninitialized variables. Also, that's neither C89 nor C99 nor C++.
24
Name:
Anonymous
2008-05-09 7:32
DONT EXPECT A FUCKING CROWD
╳ these challenges are lame
╱ ╲
╱ ╳ if i wanted boring "assignments"
╱ ╱ ╲ i wouldn't have dropped out of college
25
Name:
Anonymous
2008-05-09 14:31
>>23
gcc -std=c99 -pedantic doesn't give any errors with this code...
26
Name:
Anonymous
2008-05-09 15:07
>>10,21
back to #sicp, please
27
Name:
Anonymous
2008-05-09 16:03
HAX MY ANUS
╳ ANII are cool
╱ ╲
╱ ╳ if i wanted boring "vagoos"
╱ ╱ ╲ i wouldn't have become gay
28
Name:
Anonymous
2008-05-09 16:14
>>25
Needs more
-malicious-double.
29
Name:
Anonymous
2008-05-09 17:35
>>25
CFLAGS JUST KICKED IN YO!
-Wall -Wextra also undefined behaviour
30
Name:
Anonymous
2008-05-09 18:02
>>10
Why so verbose, dude?
primes = map head $ iterate (\(x:xs) -> filter ((0/=) . (`mod` x)) xs) [2..]
main = do [lo, hi] <- mapM readIO . words =<< getLine
print $ length $ takeWhile (<=hi) $ dropWhile (<lo) $ primes
31
Name:
Anonymous
2008-05-09 23:05
>>30
That may be beautiful and short, but it's dog slow. Here's one with a decent performance:
import System
import Data.Array.Unboxed
listUArray :: (IArray UArray e, Ix i) => (i, i) -> [e] -> UArray i e
listUArray = listArray
sieve limit = sieveLoop 1 $ listUArray (1, limit) $ False: repeat True
where
sqrtLimit = floor (sqrt $ fromIntegral limit)
sieveLoop last numbers | prime <= sqrtLimit = prime: sieveLoop prime nextNums
| otherwise = map fst $ filter snd $ assocs numbers
where
prime = fst $ head $ filter snd $ drop last $ assocs numbers
nextNums = numbers // zip (tail [0, prime..limit]) (repeat False)
main = do [lo, hi] <- mapM readIO . words =<< getLine
print $ length $ dropWhile (<lo) $ sieve hi
32
Name:
Anonymous
2008-05-10 13:14
>>31
Did you mean
PIG SLOW ?
33
Name:
Anonymous
2008-05-10 16:50
>>32
Is there a difference?
34
Name:
Anonymous
2008-05-10 19:59
>>33
Yes. Pig slow makes no sense
35
Name:
Anonymous
2008-05-10 21:12
>>34
Why not? Pigs aren't exactly fast. They sure ain't faster than dogs.
36
Name:
Anonymous
2008-05-10 21:19
37
Name:
Anonymous
2008-05-10 21:31
>>36
No fair, that pig is on the juice.
38
Name:
Anonymous
2008-05-10 22:21
float fTemp1 = 0.0f,
fTemp2 = 1.0f,
fResult = 0.0f;
while(fResult < INT_MAX){
fResult = fTemp2 / fTemp1;
}
39
Name:
Anonymous
2008-05-10 22:30
>>38
Hungarian notation considered harmful.
40
Name:
Anonymous
2008-05-10 22:34
typedef float fFloat;
Newer Posts