-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #392 from Pantotone/challenges/6-numeros-primos/cr…
…ystal/pantotone feat(6-numeros-primos/crystal): baby first prime number checker
- Loading branch information
Showing
3 changed files
with
51 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,22 @@ | ||
# Submissão de Exercicio | ||
|
||
**Exercicio:** 6 - Números Primos | ||
|
||
**Nickname:** Pantotone | ||
|
||
**Nível Técnico:** Estudante | ||
|
||
**Empresa:** Freelancer | ||
|
||
**Twitter**: https://twitter.com/pantotone | ||
|
||
**Dificuldade de Resolução:** - Média | ||
|
||
**Comentários:** Mexendo com código faz anos, e nunca fiz um checker pra números primos.. | ||
|
||
**Como rodar o desafio**: | ||
|
||
Use o comando abaixo: | ||
```bash | ||
crystal main.cr <input> | ||
``` |
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,16 @@ | ||
number : Int32 = ARGV[0].to_i | ||
|
||
def isPrime(number : Int32) | ||
return false if number < 2 | ||
return true if number == 2 | ||
|
||
divisor : Int32 = 1 | ||
unless divisor > Math.isqrt(number) | ||
divisor += 1 | ||
return false if number % divisor == 0 | ||
end | ||
|
||
return true | ||
end | ||
|
||
puts isPrime(number) |
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,13 @@ | ||
number : Int32 = ARGV[0].to_i | ||
|
||
def isPrime(number : Int32) | ||
return false if number < 2 | ||
|
||
divisors = (2..number).select do |divisor| | ||
number % divisor == 0 | ||
end | ||
|
||
return divisors.size == 1 | ||
end | ||
|
||
puts isPrime(number) |