=begin
The Sieve of Eratosthenes implemented in Ruby
Robin Broad
Feb 2019
Part of a project to demonstrate an ability to
implement an algorithm in a range of different
computing languages and to use a range of
compilers, including the following:
Java: BlueJ
C: Miracle C
C++: Microsoft Visual C++.NET/ GNU GCC/ Linux Code::Blocks IDE
C#: Microsoft Visual C#.NET
Visual Basic: Microsoft Visual Basic.NET
Python: Linux Ninja Python IDE
Ruby: ruby 2.3.1p112 (2016-04-26) [i386-linux-gnu]
=end
#variable declarations
RANGE=1000 #Define the range of the problem
root=Math.sqrt(RANGE) #needed later on when removing multiples
listOfIntegers = Array.new #declare a new array ready to hold the integers 1..1000
printCount=0 #used during print formatting of the program output
#Populate array with the integers 1..RANGE
for n in 1..RANGE
listOfIntegers[n]=n
end #end for
#starting from n=2, "remove" multiples of n up the value of RANGE
#repeat for increasing values of n up to the square root of RANGE
for n in 2..root
#delete the multiples of n
#for multiplier in 2..RANGE
multiplier=2 #start at 2n and rise to 3n etc.. (1n may be a prime number, but 2n is not)
while multiplier*n<=RANGE do #A while loop was used as Ruby appears not to support conditional for loops?
product=n*multiplier
listOfIntegers[product]=0 #write a zero to remove it from the list of prime numbers
multiplier=multiplier+1 #Ha ha, no ++ operator in Ruby!
end #end while
end #end for
puts "The Sieve of Eratosthenes program"
puts "Written in Ruby and compiled using ruby 2.3.1p112 (2016-04-26) [i386-linux-gnu]"
puts "Robin Broad, February 2019"
puts "The prime numbers in the range: 1 to "+String(RANGE)+" are:-"#Note RANGE is cast to a string here
#Print the resulting prime numbers
for n in 1..RANGE
#print all non-zero elements of the integer array in blocks of 10 numbers at a time
if listOfIntegers[n]!=0
print String(listOfIntegers[n])+"\t"#note the need of 'then' and 'end' in a single line if statement in Ruby; crazy!
#Note also the need to cast and integer into string so that a space can be added.
#'print' is also needed to prevent the linefeed given by 'puts'. OMG, this feels like BASIC! Not Good! :'( Note:\t is a tab character
printCount=printCount+1 #No increment operator, ha ha :')
if printCount%10==0 then puts "\n" end #output a newline
end #end if
end #end for
puts "\nEnd of Program." #output a newline ready for new terminal prompt
#End of program
=begin
Execution Date: Friday 1st February 2019, 17.55
This program gave the following output:
The Sieve of Eratosthenes program
Written in Ruby and compiled using ruby 2.3.1p112 (2016-04-26) [i386-linux-gnu]
Robin Broad, February 2019
The prime numbers in the range: 1 to 1000 are:-
1 2 3 5 7 11 13 17 19 23
29 31 37 41 43 47 53 59 61 67
71 73 79 83 89 97 101 103 107 109
113 127 131 137 139 149 151 157 163 167
173 179 181 191 193 197 199 211 223 227
229 233 239 241 251 257 263 269 271 277
281 283 293 307 311 313 317 331 337 347
349 353 359 367 373 379 383 389 397 401
409 419 421 431 433 439 443 449 457 461
463 467 479 487 491 499 503 509 521 523
541 547 557 563 569 571 577 587 593 599
601 607 613 617 619 631 641 643 647 653
659 661 673 677 683 691 701 709 719 727
733 739 743 751 757 761 769 773 787 797
809 811 821 823 827 829 839 853 857 859
863 877 881 883 887 907 911 919 929 937
941 947 953 967 971 977 983 991 997
End of Program.
=end