One Million Hits

March 29, 2013

I have to do something about prime numbers:

(define (eratosthenes n)
  (let ((sieve (make-vector (+ n 1) #t)))
    (let loop ((p 2) (ps (list)))
      (cond ((< n p) (reverse ps))
            ((vector-ref sieve p)
              (do ((i (* p p) (+ i p))) ((< n i))
                (vector-set! sieve i #f))
              (loop (+ p 1) (cons p ps)))
            (else (loop (+ p 1) ps))))))

Then we can enumerate the first million primes:

> (length (eratosthenes 15485863))
1000000

You can run the program at http://programmingpraxis.codepad.org/h0U9O7ww.

About these ads

Pages: 1 2

9 Responses to “One Million Hits”

  1. No one relevant said

    print bin(64)[2:]

  2. [...] today’s Programming Praxis exercise, our goal is ro print the number 1000000 in a creative way. Let’s [...]

  3. My Haskell solution (see http://bonsaicode.wordpress.com/2013/03/29/programming-praxis-one-million-hits/ for a version with comments):

    main :: IO ()
    main = print . pred $ sum (zipWith (*) q (tail q)) + head q - last q
        where q = map fromEnum "Real programmers don't comment their code, \
                               \if it was hard to write, it should be hard \
                               \to understand and harder to modify."
    
  4. Jussi Piitulainen said

    This is too simple, but I needed it for real earlier today.
    I just wasn’t sure of counting the zeroes, and I don’t know if
    the system understands anything but a string of digits, so:

    ./configure \
    --enable-single-host \
    --enable-c-opt \
    --enable-multiple-versions \
    --enable-shared \
    CFLAGS=-D___FORCE_MAX_HEAP=$(echo 1,000,000 | tr -d ,)

  5. Jan Van lent said

    Forth program inspired by [1].
    The number one million represented using postfix notation with only the number one, addition and multiplication.

    1 1 + 1 + 1 1 + 1 + * 1 1 + * 1 1 + * 1 + 1 1 + 1 + * 1 1 + 1 + * 1 1 + 1 + * 1 +
    1 1 + 1 + 1 1 + 1 + * 1 1 + * 1 1 + * 1 + 1 1 + 1 + * 1 1 + 1 + * 1 1 + 1 + * 1 +
    * .
    

    [1] Zeroless Arithmetic: Representing Integers ONLY using ONE,
    Edinah K. Gnang and Doron Zeilberger,

    http://www.math.rutgers.edu/~zeilberg/mamarim/mamarimhtml/arif.html

  6. Jan Van lent said

    Postscript program using the number one, addition and exponentiation.

    1 1 add 1 add 1 1 add exp 1 add 1 1 add 1 add exp 1 1 add exp =
    
  7. Jussi Piitulainen said

    This one I wrote just for fun: a random binary digit for each letter
    of “million”, freely instantiated but constrained to match. Cheers.

    (define (digit) (random-integer 2))
    (define (fold op o args)
      (if (null? args) o
          (fold op (op o (car args)) (cdr args))))

    (fold (lambda (o arg)
            (do ((x (digit) (digit))
                 (y (digit) (digit)))
                ((o x y) (write x) <))) >
          (string->list "million"))

    (newline)

  8. djschoch said
    declare
        one_million pls_integer := 1;
    begin
        dbms_output.put_line('To my good friend, Mr. P. Praxis,'||chr(10));
        dbms_output.put_line('Congratulations on reaching one million total hits!'||chr(10));
        
        -- count to one million (or thereabouts)
        for i in 1..1000000 loop
            one_million := one_million + 1;
        end loop;
        
        dbms_output.put_line('As you requested, here is the number one million written '||
            'from one of your least favorite programming languages, Oracle PL/SQL:');
        dbms_output.put_line(to_char(one_million,'9,999,990')||chr(10));
        dbms_output.put_line('(What fun is a PL/SQL program without a little bug in it?)'||chr(10));
        dbms_output.put_line('-Dan-');
    end;
    /
    
    To my good friend, Mr. P. Praxis,
    
    Congratulations on reaching one million total hits!
    
    As you requested, here is the number one million written from one of your least
    favorite programming languages, Oracle PL/SQL:
    1,000,001
    
    (What fun is a PL/SQL program without a little bug in it?)
    
    -Dan-
    P.S. WordPress currently states that the "pre" tag IS allowed, so I'm trying it here.
    
    PL/SQL procedure successfully completed.
    
  9. programmingpraxis said

    Thanks Dan (and others). I’m beginning to think this Internet thing really works.

    By the way, WordPress doesn’t count what it calls syndicated views, which are things like RSS feeds where people read the blog without actually generating a hit. Most exercises generate two or three times as many syndicated views as hits, so the actual count is higher. Unfortunately WordPress doesn’t give a total on syndicated views.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 609 other followers

%d bloggers like this: