Created
January 31, 2018 21:55
-
-
Save naaando/1dfad0496dac2ea4b793299833f3ace5 to your computer and use it in GitHub Desktop.
Vala primes
This file contains hidden or 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
// Ultimo numero a ser testado | |
const long BREAKPOINT = 10000; | |
int main (string[] args) { | |
for (int number = 0; number <= BREAKPOINT; number++) { | |
if (prime (number)) { | |
print (@"[$number]"); | |
} | |
} | |
// test_prime (); | |
print ("\n"); | |
return 0; | |
} | |
// Função para testar se um número é primo | |
bool prime (float n) { | |
// Damos um offset nos números pra evitar calculos inúteis | |
for (float x = 2; x < n; x++) { | |
if (n % x == 0) { | |
return false; | |
} | |
} | |
// Pelo offset 0 e 1 passam pela verificação, aqui verificamos se ele | |
return n != 0 && n != 1; | |
} | |
void test_primes () { | |
assert (!prime (1)); | |
assert (prime (3)); | |
assert (prime (5)); | |
assert (prime (7)); | |
assert (!prime (10)); | |
} |
This file contains hidden or 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
// Ultimo numero a ser testado | |
const long BREAKPOINT = 10000; | |
int main () { | |
if (Thread.supported () == false) { | |
stdout.printf ("Threads are not supported.\n"); | |
return 0; | |
} | |
ThreadFunc<bool> even_func = () => { | |
for (int number = 1; number <= BREAKPOINT; number=number+2) { | |
if (prime (number)) { | |
print (@"[$number]"); | |
} | |
} | |
return false; | |
}; | |
ThreadFunc<bool> odd_func = () => { | |
for (int number = 0; number <= BREAKPOINT; number=number+2) { | |
if (prime (number)) { | |
print (@"[$number]"); | |
} | |
} | |
return false; | |
}; | |
Thread<bool> thread1 = new Thread<bool> ("even", even_func); | |
Thread<bool> thread2 = new Thread<bool> ("odd", odd_func); | |
thread1.join (); | |
thread2.join (); | |
print ("\n"); | |
return 0; | |
} | |
bool prime (float n) { | |
// Damos um offset nos números pra evitar calculos inúteis | |
for (float x = 2; x < n; x++) { | |
if (n % x == 0) { | |
return false; | |
} | |
} | |
// Pelo offset 0 e 1 passam pela verificação, aqui verificamos se ele | |
return n != 0 && n != 1; | |
} | |
void test_primes () { | |
assert (!prime (1)); | |
assert (prime (3)); | |
assert (prime (5)); | |
assert (prime (7)); | |
assert (!prime (10)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment