Project Euler : Problem 007

Project Euler problems in Oforth . Feel free to post your own code.

Project Euler : Problem 007

Postby Franck » 29 Jul 2015 17:47

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

Code: Select all
// Here, we define a method #isPrime for integers using trial division.
Integer method: isPrime    // ( n -- [ true | false ] )
{
| i |
   self 3 <= ifTrue: [ self 1 <> return ]
   self isEven ifTrue: [ false return ]
   3 self sqrt asInteger for: i [ self i mod ifFalse: [ false return ] i 1 + ->i ]
   true
}

: pe007(n)    // ( n -- n ) : Returns n th prime number>pmax
{
   n 2 - 3 while(over) [ 2 + dup isPrime ifTrue: [ swap 1 - swap ] ] nip
}
Franck
 
Posts: 162
Joined: 29 Oct 2014 19:01

Return to Project Euler in Oforth

Who is online

Users browsing this forum: No registered users and 2 guests

cron