Solved 007: php, ruby, c
This commit is contained in:
@ -19,6 +19,6 @@ solutions:
|
||||
solve.rb:
|
||||
desc: Basic Ruby solution
|
||||
language: ruby
|
||||
solve.rb
|
||||
solve.rb:
|
||||
desc: ANSI C solution (Tested with TCC)
|
||||
language: c
|
20
ProjectEuler/007/desc.yml
Normal file
20
ProjectEuler/007/desc.yml
Normal file
@ -0,0 +1,20 @@
|
||||
title: Find the 10001st prime.
|
||||
url: http://projecteuler.net/problem=7
|
||||
|
||||
desc: |
|
||||
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
|
||||
What is the 10 001st prime number?
|
||||
|
||||
solution: |
|
||||
Move up the among the prime numbers till you reach 10001
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb
|
||||
desc: Basic solution
|
||||
language: PHP
|
||||
solve.c
|
||||
desc: ANSI C (Compiled with TCC) solution
|
||||
language: C
|
30
ProjectEuler/007/solve.c
Normal file
30
ProjectEuler/007/solve.c
Normal file
@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int is_prime( int prime) {
|
||||
|
||||
int max_test = sqrt( prime );
|
||||
|
||||
int i;
|
||||
for (i = 3; i <= max_test; i+=2){
|
||||
if (prime % i == 0){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main( )
|
||||
{
|
||||
//Use start val
|
||||
int i=13;
|
||||
int primes = 6;
|
||||
do{
|
||||
i+= 2;
|
||||
if(is_prime(i)){
|
||||
primes++;
|
||||
}
|
||||
|
||||
} while(primes < 10001);
|
||||
printf("%d", i);
|
||||
}
|
18
ProjectEuler/007/solve.php
Normal file
18
ProjectEuler/007/solve.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
function is_prime($prime) {
|
||||
$sqrt = sqrt($prime);
|
||||
for ($i = 3; $i <= $sqrt; $i+=2){
|
||||
if ($prime%$i == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
$i=13;
|
||||
$primes = 6;
|
||||
do{
|
||||
$i+= 2;
|
||||
if(is_prime($i)){
|
||||
$primes++;
|
||||
}
|
||||
}while($primes < 10001);
|
||||
echo $i;
|
21
ProjectEuler/007/solve.rb
Normal file
21
ProjectEuler/007/solve.rb
Normal file
@ -0,0 +1,21 @@
|
||||
def is_prime(prime)
|
||||
sqrt = Math.sqrt(prime)
|
||||
i=3
|
||||
while(i <= sqrt)
|
||||
if (prime % i == 0)
|
||||
return false
|
||||
end
|
||||
i+= 2
|
||||
end
|
||||
return true;
|
||||
end
|
||||
|
||||
i=13
|
||||
primes = 6
|
||||
until (primes == 10001)
|
||||
i+= 2
|
||||
if(is_prime(i))
|
||||
primes+=1
|
||||
end
|
||||
end
|
||||
puts i
|
Reference in New Issue
Block a user