How to resolve the algorithm Sieve of Eratosthenes step by step in the RATFOR programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sieve of Eratosthenes step by step in the RATFOR programming language
Table of Contents
Problem Statement
The Sieve of Eratosthenes is a simple algorithm that finds the prime numbers up to a given integer.
Implement the Sieve of Eratosthenes algorithm, with the only allowed optimization that the outer loop can stop at the square root of the limit, and the inner loop may start at the square of the prime just found. That means especially that you shouldn't optimize by using pre-computed wheels, i.e. don't assume you need only to cross out odd numbers (wheel based on 2), numbers equal to 1 or 5 modulo 6 (wheel based on 2 and 3), or similar wheels based on low primes. If there's an easy way to add such a wheel based optimization, implement it as an alternative version.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sieve of Eratosthenes step by step in the RATFOR programming language
Source code in the ratfor programming language
program prime
#
define(true,1)
define(false,0)
#
integer loop,loop2,limit,k,primes,count
integer isprime(1000)
limit = 1000
count = 0
for (loop=1; loop<=limit; loop=loop+1)
{
isprime(loop) = true
}
isprime(1) = false
for (loop=2; loop<=limit; loop=loop+1)
{
if (isprime(loop) == true)
{
count = count + 1
for (loop2=loop*loop; loop2 <= limit; loop2=loop2+loop)
{
isprime(loop2) = false
}
}
}
write(*,*)
write(*,101) count
101 format('There are ',I12,' primes.')
count = 0
for (loop=1; loop<=limit; loop=loop+1)
if (isprime(loop) == true)
{
Count = count + 1
write(*,'(I6,$)')loop
if (mod(count,10) == 0) write(*,*)
}
write(*,*)
end
You may also check:How to resolve the algorithm Church numerals step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Nth root step by step in the Sidef programming language
You may also check:How to resolve the algorithm Numerical integration step by step in the Ada programming language
You may also check:How to resolve the algorithm Empty program step by step in the eC programming language
You may also check:How to resolve the algorithm Sphenic numbers step by step in the Java programming language