### Thread: Prime numbers in Scheme

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2011
Posts
1
Rep Power
0

#### Prime numbers in Scheme

Okay guys, I'm trying to do a program that checks if a given number is prime or not.

So far what I got:

- Even numbers are not prime so I need check if a given number is even or not;
- If it is, I give "#f", if it isn't I need to divide it by 2, if the remainder is 0 I give "#f", if it's not 0 I divide it by the 3,..., until sqrt N where N is the given number.

I'm not getting how I will pass that to the language itself...

Can I do it recursively?

I think I need to create a new variable that will add one everytime the remainder isn't 0...

Would be glad if someone could help me.
2. Regardless of the language you use, The Sieve of Eratosthenes is an easy implementation.

Code:
```define primes as list (2)

define number = 3

while program not halted OR number < pre defined ending condition {
for prime in primes {
if  number %mod% prime == 0 {
increment number
continue loop
}
}
add number to primes
increment number
}

print list of primes```
-Dan
3. There's another solution involving regular expressions, or you could also simply attempt to factor the number using modular division.