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

90% of /prog/ cannot write this BASIC PROGRAM

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

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

Name: Anonymous 2008-05-08 20:34

>>2
that should be divisor, not denominator
my bad
^lowestdivisor

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;
}

Name: Anonymous 2008-05-08 20:38

>>4
my bad, 0 and 1 should be swapped in is_prime()

Name: Anonymous 2008-05-08 20:41

>>5
and read that as int a, b, i, num=0; of course

Name: Anonymous 2008-05-08 20:41

>>4-5

I failed, didn't i :(

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;  
   
}

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?

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'

Name: Anonymous 2008-05-08 21:14

>>10
It is because you are a Nomad. Haskell Nomad.

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()));}

Name: Anonymous 2008-05-08 22:36

>>12
Not valid C89

Name: Anonymous 2008-05-08 22:40

>>13
only sepplesfags still use pre-c99.

Name: Anonymous 2008-05-08 22:45

>>12
Terrible!

Name: Anonymous 2008-05-08 22:57

>>14
only people confined to reality still use pre-c99
ftfy

Name: Anonymous 2008-05-08 23:08

>>16
only if your reality is limited to the inside of an anus.

Name: Anonymous 2008-05-08 23:11

>>17
It is, yet is is hax'd

Name: Anonymous 2008-05-08 23:49

>>18
*grabs dick*

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, ;_;

Name: Anonymous 2008-05-09 5:13

>>10
HELLO VIXEY

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.

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++.

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


Name: Anonymous 2008-05-09 14:31

>>23
gcc -std=c99 -pedantic doesn't give any errors with this code...

Name: Anonymous 2008-05-09 15:07

>>10,21
back to #sicp, please

Name: Anonymous 2008-05-09 16:03


     HAX MY ANUS

   ╳       ANII are cool
  ╱ ╲    
 ╱   ╳     if i wanted boring "vagoos"
╱   ╱ ╲    i wouldn't have become gay

Name: Anonymous 2008-05-09 16:14

>>25
Needs more -malicious-double.

Name: Anonymous 2008-05-09 17:35

>>25
CFLAGS JUST KICKED IN YO!

-Wall -Wextra also undefined behaviour

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

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

Name: Anonymous 2008-05-10 13:14

>>31
Did you mean PIG SLOW?

Name: Anonymous 2008-05-10 16:50

>>32
Is there a difference?

Name: Anonymous 2008-05-10 19:59

>>33
Yes. Pig slow makes no sense

Name: Anonymous 2008-05-10 21:12

>>34
Why not? Pigs aren't exactly fast. They sure ain't faster than dogs.

Name: Anonymous 2008-05-10 21:19

Name: Anonymous 2008-05-10 21:31

>>36
No fair, that pig is on the juice.

Name: Anonymous 2008-05-10 22:21

float fTemp1 = 0.0f,
fTemp2 = 1.0f,
fResult = 0.0f;

while(fResult < INT_MAX){
     fResult = fTemp2 / fTemp1;
}

Name: Anonymous 2008-05-10 22:30

>>38
Hungarian notation considered harmful.

Name: Anonymous 2008-05-10 22:34

typedef float fFloat;

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