Thursday, July 8

Primes

Yesterday night, a friend of mine IM'd me. He asked me if i could "help" him do his homework. Being idle then, I asked what it was about. He said it was simple: Make a psuedocode! Of course, i knew there was more to it that just that. He paused for a moment, then continued, as if waiting for a response. He said there was one problem. Actually it was something like this: Make a psuedocode that prints all prime numbers up until it reaches a number the user inputs.

Game on! Here's what I figured out after scanning through some material on prime numbers, i.e. sieves and such.


################################
################################
snippet
no declarations, start, end,
stop and next line
################################
################################
x=1
input y
if y=0 OR y=1
echo NONE
do
    { x=x+1
       count=1
            do
            { count=count+1
               rem = x modulus count
            } while rem!=0 AND count<x
    if count=x
    echo x
} while x!=y
################################
################################

No comments: