-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ary Borenszweig
committed
Nov 12, 2016
1 parent
db8ef30
commit 86f9069
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
class Natural | ||
@cnt = 1 | ||
|
||
def next | ||
@cnt += 1 | ||
end | ||
end | ||
|
||
class Filter | ||
getter number | ||
property! next : Filter | ||
property! last : Filter | ||
|
||
def initialize(@number : Int32) | ||
@last = self | ||
end | ||
|
||
def accept_and_add(n) | ||
filter = self | ||
upto = Math.sqrt(n) | ||
loop do | ||
if n.divisible_by?(filter.number) | ||
return false | ||
end | ||
if filter.number > upto | ||
new_filter = Filter.new(n) | ||
last.next = new_filter | ||
@last = new_filter | ||
return true | ||
end | ||
filter = filter.next | ||
end | ||
end | ||
end | ||
|
||
class Primes | ||
@natural = Natural.new | ||
|
||
def next | ||
loop do | ||
n = @natural.next | ||
filter = @filter | ||
unless filter | ||
@filter = Filter.new(n) | ||
return n | ||
end | ||
if filter.accept_and_add(n) | ||
return n | ||
end | ||
end | ||
end | ||
|
||
def compute | ||
start = Time.now | ||
cnt = 0 | ||
prnt_cnt = 97 | ||
res = 0 | ||
loop do | ||
res = self.next | ||
cnt += 1 | ||
|
||
if cnt.divisible_by?(prnt_cnt) | ||
took = Time.now - start | ||
puts "Computed #{cnt} primes in #{took.milliseconds} ms. Last one is #{res}" | ||
prnt_cnt *= 2 | ||
end | ||
break if cnt >= 100000 | ||
end | ||
res | ||
end | ||
end | ||
|
||
loop do | ||
p = Primes.new | ||
time = Time.now | ||
p.compute | ||
took = Time.now - time | ||
puts "Hundred thousand primes computed in #{took.milliseconds}ms" | ||
end |