Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
...which is actually just a straight clone of the Ruby version thanks to Crystal being heavily influenced by Ruby. Signed-off-by: Eddie Ringle <eddie@ringle.io>
- Loading branch information
1 parent
38aa89b
commit 4698a89
Showing
3 changed files
with
66 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
genprime-* | ||
genprime.*.py | ||
*.rbc | ||
.crystal |
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
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,55 @@ | ||
def isprime(x) | ||
if x < 2 | ||
return 0 | ||
end | ||
if x == 2 | ||
return 1 | ||
end | ||
if x % 2 == 0 | ||
return 0 | ||
end | ||
if x < 9 | ||
return 1 | ||
end | ||
if (x + 1) % 6 != 0 | ||
if (x - 1) % 6 != 0 | ||
return 0 | ||
end | ||
end | ||
lim = Math.sqrt(x).to_i + 1 | ||
y = 3 | ||
while y <= lim | ||
if x % y == 0 | ||
return 0 | ||
end | ||
y += 2 | ||
end | ||
return 1 | ||
end | ||
|
||
def genprime(max) | ||
count = 0 | ||
current = 1 | ||
while count < max | ||
if isprime(current) != 0 | ||
count += 1 | ||
end | ||
current += 1 | ||
end | ||
return current - 1 | ||
end | ||
|
||
start = ARGV[0].to_i | ||
stop = ARGV[1].to_i + 1 | ||
i = start | ||
if start == 0 | ||
Process.exit | ||
end | ||
until i >= stop | ||
starttime = Time.now | ||
last = genprime(i) | ||
endtime = Time.now | ||
duration = endtime - starttime | ||
printf "Found %8d primes in %10.5f seconds (last was %10d)\n", i, duration, last | ||
i += start | ||
end |