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

Advertisements

Pages: 1 2

print bin(64)[2:]

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

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

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 ,)

Forth program inspired by [1].

The number one million represented using postfix notation with only the number one, addition and multiplication.

[1] Zeroless Arithmetic: Representing Integers ONLY using ONE,

Edinah K. Gnang and Doron Zeilberger,

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

Postscript program using the number one, addition and exponentiation.

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)`

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.

1 million is such a small number. 1,000,000 in binary is only 64 in decimal :)