Solved ProjectEuler/010: php, ruby, c
This commit is contained in:
20
ProjectEuler/010/desc.yml
Normal file
20
ProjectEuler/010/desc.yml
Normal file
@ -0,0 +1,20 @@
|
||||
title: Calculate the sum of all the primes below two million.
|
||||
url: http://projecteuler.net/problem=10
|
||||
|
||||
desc: |
|
||||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
Find the sum of all the primes below two million.
|
||||
|
||||
solution: |
|
||||
Use a primenumber tester
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
||||
solve.c:
|
||||
desc: ANSI C solution compiled with gcc-4.3.4
|
||||
language: c
|
28
ProjectEuler/010/solve.c
Normal file
28
ProjectEuler/010/solve.c
Normal file
@ -0,0 +1,28 @@
|
||||
#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( )
|
||||
{
|
||||
long long sum = 2+3;
|
||||
long number;
|
||||
for(number = 5; number < 2000000; number+=2) {
|
||||
if (is_prime(number))
|
||||
{
|
||||
sum += number;
|
||||
}
|
||||
}
|
||||
printf("%llu",sum);
|
||||
}
|
18
ProjectEuler/010/solve.php
Normal file
18
ProjectEuler/010/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;
|
||||
}
|
||||
|
||||
$sum = 2+3;
|
||||
for($number = 5; $number < 2000000; $number+=2) {
|
||||
if (is_prime($number))
|
||||
{
|
||||
$sum += $number;
|
||||
}
|
||||
}
|
||||
echo $sum;
|
21
ProjectEuler/010/solve.rb
Normal file
21
ProjectEuler/010/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
|
||||
|
||||
number = 3
|
||||
sum = 5
|
||||
until(number >= 2000000)
|
||||
number += 2
|
||||
if(is_prime(number))
|
||||
sum += number
|
||||
end
|
||||
end
|
||||
puts sum;
|
Reference in New Issue
Block a user