Initial commit
parent
316bd4898e
commit
018be0a886
|
@ -0,0 +1,53 @@
|
|||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<int> get_primes7(int n) { // ugly variable declarations but close to the other lang. syntaxes
|
||||
vector<int> res;
|
||||
|
||||
if (n < 2) return res;
|
||||
if (n == 2) {
|
||||
res.push_back(2);
|
||||
return res;
|
||||
}
|
||||
vector<bool> s;
|
||||
for (int i = 3; i < n + 1; i += 2) {
|
||||
s.push_back(true);
|
||||
}
|
||||
int mroot = sqrt(n);
|
||||
int half = (int)s.size();
|
||||
int i = 0;
|
||||
int m = 3;
|
||||
while (m <= mroot) {
|
||||
if (s[i]) {
|
||||
int j = (int)((m*m - 3)/2);
|
||||
s[j] = false;
|
||||
while (j < half) {
|
||||
s[j] = false;
|
||||
j += m;
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
m = 2*i + 3;
|
||||
}
|
||||
res.push_back(2);
|
||||
for (size_t it = 0; it < s.size(); ++it) {
|
||||
if (s[it]) {
|
||||
res.push_back(2*it + 3);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<int> res;
|
||||
for (int i = 1; i <= 100; ++i) {
|
||||
res = get_primes7(10000000);
|
||||
printf("Found %d prime numbers.\n", (int)res.size());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Some performance optimizations are commented out as an alternative implementation.
|
||||
// See the comments by Vinicius Miranda for more information:
|
||||
// http://blog.famzah.net/2010/07/01/cpp-vs-python-vs-perl-vs-php-performance-benchmark/#comment-5347
|
||||
|
||||
void get_primes7(int n, vector<int> &res) {
|
||||
if (n < 2) return;
|
||||
if (n == 2) {
|
||||
res.push_back(2);
|
||||
return;
|
||||
}
|
||||
vector<int> s;
|
||||
// 1 //s.reserve(n/2); // hint the compiler that we will use "n/2" elements in "s"
|
||||
// 2 //s.resize( static_cast<int>(n/2) ); // pre-allocate memory
|
||||
// 2 //int j = 0;
|
||||
for (int i = 3; i < n + 1; i += 2) {
|
||||
s.push_back(i);
|
||||
// 2 //s[j] = i;
|
||||
// 2 //++j;
|
||||
}
|
||||
// 2 //s.resize(j);
|
||||
int mroot = sqrt(n);
|
||||
int half = static_cast<int>(s.size());
|
||||
int i = 0;
|
||||
int m = 3;
|
||||
while (m <= mroot) {
|
||||
if (s[i]) {
|
||||
int j = static_cast<int>((m*m - 3)*0.5);
|
||||
s[j] = 0;
|
||||
while (j < half) {
|
||||
s[j] = 0;
|
||||
j += m;
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
m = 2*i + 3;
|
||||
}
|
||||
// 1 //res.reserve(n/log(n)); // "Prime number theorem" says that we expect this amount of primes
|
||||
res.push_back(2);
|
||||
|
||||
/*
|
||||
// loop manually
|
||||
for (vector<int>::iterator it = s.begin() ; it < s.end(); ++it) {
|
||||
if (*it) {
|
||||
res.push_back(*it);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// use standard methods instead of a loop
|
||||
std::vector<int>::iterator pend = std::remove(s.begin(), s.end(), 0);
|
||||
res.insert(res.begin() + 1, s.begin(), pend);
|
||||
}
|
||||
|
||||
int main() {
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
vector<int> res;
|
||||
get_primes7(10000000, res);
|
||||
printf("Found %d prime numbers.\n", (int)res.size());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<int> get_primes7(int n) { // ugly variable declarations but close to the other lang. syntaxes
|
||||
vector<int> res;
|
||||
|
||||
if (n < 2) return res;
|
||||
if (n == 2) {
|
||||
res.push_back(2);
|
||||
return res;
|
||||
}
|
||||
vector<int> s;
|
||||
for (int i = 3; i < n + 1; i += 2) {
|
||||
s.push_back(i);
|
||||
}
|
||||
int mroot = sqrt(n);
|
||||
int half = (int)s.size();
|
||||
int i = 0;
|
||||
int m = 3;
|
||||
while (m <= mroot) {
|
||||
if (s[i]) {
|
||||
int j = (int)((m*m - 3)/2);
|
||||
s[j] = 0;
|
||||
while (j < half) {
|
||||
s[j] = 0;
|
||||
j += m;
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
m = 2*i + 3;
|
||||
}
|
||||
res.push_back(2);
|
||||
for (vector<int>::iterator it = s.begin() ; it < s.end(); ++it) {
|
||||
if (*it) {
|
||||
res.push_back(*it);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<int> res;
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
res = get_primes7(10000000);
|
||||
printf("Found %d prime numbers.\n", (int)res.size());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
import java.util.*;
|
||||
import java.lang.Math;
|
||||
|
||||
class PrimeNumbersGenerator {
|
||||
IntList get_primes7(int n) {
|
||||
IntList res = new IntList();
|
||||
|
||||
if (n < 2) return res;
|
||||
if (n == 2) {
|
||||
res.add(2);
|
||||
return res;
|
||||
}
|
||||
IntList s = new IntList();
|
||||
for (int i = 3; i <= n; i += 2) {
|
||||
s.add(i);
|
||||
}
|
||||
int mroot = (int)Math.sqrt(n);
|
||||
int half = s.size();
|
||||
int i = 0;
|
||||
int m = 3;
|
||||
while (m <= mroot) {
|
||||
if (s.get(i) != 0) {
|
||||
int j = (int)((m*m - 3)/2);
|
||||
s.set(j, 0);
|
||||
while (j < half) {
|
||||
s.set(j, 0);
|
||||
j += m;
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
m = 2*i + 3;
|
||||
}
|
||||
res.add(2);
|
||||
for (int it = 0; it < s.data.length; ++it) {
|
||||
if (s.data[it] != 0) {
|
||||
res.add(s.data[it]);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Variable length int ArrayList.
|
||||
*
|
||||
* (Like the Java java.util.ArrayList.
|
||||
* Just without the slow java.lang.Integer unboxing/boxing)
|
||||
*/
|
||||
class IntList {
|
||||
|
||||
private static final int DEFAULT_CAPACITY = 1000;
|
||||
public int[] data;
|
||||
private int off = 0;
|
||||
|
||||
public IntList() {
|
||||
data = new int[DEFAULT_CAPACITY];
|
||||
}
|
||||
|
||||
public void add(int x) {
|
||||
// if there is not enough room to store the value a new array is created.
|
||||
// (like in java.lang.ArrayList)
|
||||
if (off >= data.length) {
|
||||
data = Arrays.copyOf(data, data.length * 2);
|
||||
}
|
||||
data[off++] = x;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
off = 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return off;
|
||||
}
|
||||
|
||||
public void set(int i, int x) {
|
||||
data[i] = x;
|
||||
}
|
||||
|
||||
public int get(int x) {
|
||||
return data[x];
|
||||
}
|
||||
}
|
||||
|
||||
class PrimeNumbersBenchmarkApp {
|
||||
public static void main(String[] args) {
|
||||
IntList res;
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
res = (new PrimeNumbersGenerator()).get_primes7(10000000);
|
||||
System.out.format("Found %d prime numbers.\n", res.size());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Some performance optimizations are commented out as an alternative implementation.
|
||||
// See the comments by Vinicius Miranda for more information:
|
||||
// http://blog.famzah.net/2010/07/01/cpp-vs-python-vs-perl-vs-php-performance-benchmark/#comment-5347
|
||||
|
||||
void get_primes7(int n, vector<int> &res) {
|
||||
if (n < 2) return;
|
||||
if (n == 2) {
|
||||
res.push_back(2);
|
||||
return;
|
||||
}
|
||||
vector<int> s;
|
||||
// 1 //s.reserve(n/2); // hint the compiler that we will use "n/2" elements in "s"
|
||||
// 2 //s.resize( static_cast<int>(n/2) ); // pre-allocate memory
|
||||
// 2 //int j = 0;
|
||||
for (int i = 3; i < n + 1; i += 2) {
|
||||
s.push_back(i);
|
||||
// 2 //s[j] = i;
|
||||
// 2 //++j;
|
||||
}
|
||||
// 2 //s.resize(j);
|
||||
int mroot = sqrt(n);
|
||||
int half = static_cast<int>(s.size());
|
||||
int i = 0;
|
||||
int m = 3;
|
||||
while (m <= mroot) {
|
||||
if (s[i]) {
|
||||
int j = static_cast<int>((m*m - 3)*0.5);
|
||||
s[j] = 0;
|
||||
while (j < half) {
|
||||
s[j] = 0;
|
||||
j += m;
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
m = 2*i + 3;
|
||||
}
|
||||
// 1 //res.reserve(n/log(n)); // "Prime number theorem" says that we expect this amount of primes
|
||||
res.push_back(2);
|
||||
|
||||
/*
|
||||
// loop manually
|
||||
for (vector<int>::iterator it = s.begin() ; it < s.end(); ++it) {
|
||||
if (*it) {
|
||||
res.push_back(*it);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// use standard methods instead of a loop
|
||||
std::vector<int>::iterator pend = std::remove(s.begin(), s.end(), 0);
|
||||
res.insert(res.begin() + 1, s.begin(), pend);
|
||||
}
|
||||
|
||||
int main() {
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
vector<int> res;
|
||||
get_primes7(10000000, res);
|
||||
printf("Found %d prime numbers.\n", (int)res.size());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
import java.util.*;
|
||||
import java.lang.Math;
|
||||
|
||||
class PrimeNumbersGenerator {
|
||||
ArrayList<Integer> get_primes7(int n) {
|
||||
ArrayList<Integer> res = new ArrayList<Integer>();
|
||||
|
||||
if (n < 2) return res;
|
||||
if (n == 2) {
|
||||
res.add(2);
|
||||
return res;
|
||||
}
|
||||
ArrayList<Integer> s = new ArrayList<Integer>();
|
||||
for (int i = 3; i < n + 1; i += 2) {
|
||||
s.add(i);
|
||||
}
|
||||
int mroot = (int)Math.sqrt(n);
|
||||
int half = s.size();
|
||||
int i = 0;
|
||||
int m = 3;
|
||||
while (m <= mroot) {
|
||||
if (s.get(i) != 0) {
|
||||
int j = (int)((m*m - 3)/2);
|
||||
s.set(j, 0);
|
||||
while (j < half) {
|
||||
s.set(j, 0);
|
||||
j += m;
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
m = 2*i + 3;
|
||||
}
|
||||
res.add(2);
|
||||
for (int it = 0; it < s.size(); ++it) {
|
||||
if (s.get(it) != 0) {
|
||||
res.add(s.get(it));
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
class PrimeNumbersBenchmarkApp {
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Integer> res;
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
res = (new PrimeNumbersGenerator()).get_primes7(10000000);
|
||||
System.out.format("Found %d prime numbers.\n", res.size());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
function get_primes7(n) {
|
||||
if (n < 2) { return []; }
|
||||
if (n == 2) { return [2]; }
|
||||
|
||||
var s = [];
|
||||
for (var i = 3; i < n + 1; i += 2) {
|
||||
s.push(i);
|
||||
}
|
||||
|
||||
var mroot = Math.floor(Math.sqrt(n));
|
||||
var half = s.length;
|
||||
var i = 0;
|
||||
var m = 3;
|
||||
|
||||
while (m <= mroot) {
|
||||
if (s[i]) {
|
||||
var j = Math.floor((m*m-3)/2); // int div
|
||||
s[j] = 0;
|
||||
while (j < half) {
|
||||
s[j] = 0;
|
||||
j += m;
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
m = 2*i + 3;
|
||||
}
|
||||
|
||||
var res = [2];
|
||||
for (var x = 0; x < s.length; x++) {
|
||||
if (s[x]) {
|
||||
res.push(s[x]);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
for (var i = 0; i < 10; i++) {
|
||||
var res = get_primes7(10000000);
|
||||
console.log("Found " + res.length + " prime numbers.");
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', '1');
|
||||
|
||||
function get_primes7($n) {
|
||||
if ($n < 2) return array();
|
||||
if ($n == 2) return array(2);
|
||||
$s = range(3, $n, 2);
|
||||
$mroot = sqrt($n);
|
||||
$half = count($s);
|
||||
$i = 0;
|
||||
$m = 3;
|
||||
while ($m <= $mroot) {
|
||||
if ($s[$i]) {
|
||||
$j = (int)(($m*$m - 3) / 2);
|
||||
$s[$j] = 0;
|
||||
while ($j < $half) {
|
||||
$s[$j] = 0;
|
||||
$j += $m;
|
||||
}
|
||||
}
|
||||
$i = $i + 1;
|
||||
$m = 2*$i + 3;
|
||||
}
|
||||
$res = array(2);
|
||||
foreach ($s as $v) {
|
||||
if ($v) {
|
||||
$res[] = $v;
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
$res = array();
|
||||
for ($i = 1; $i <= 10; ++$i) {
|
||||
$res = get_primes7(10000000);
|
||||
print "Found ".count($res)." prime numbers.\n";
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
sub get_primes7($) {
|
||||
my ($n) = @_;
|
||||
|
||||
if ($n < 2) { return (); }
|
||||
if ($n == 2) { return (2); }
|
||||
# do only odd numbers starting at 3
|
||||
my @s = ();
|
||||
for (my $i = 3; $i < $n + 1; $i += 2) {
|
||||
push(@s, $i);
|
||||
}
|
||||
# n**0.5 simpler than math.sqr(n)
|
||||
my $mroot = $n ** 0.5;
|
||||
my $half = scalar @s;
|
||||
my $i = 0;
|
||||
my $m = 3;
|
||||
while ($m <= $mroot) {
|
||||
if ($s[$i]) {
|
||||
my $j = int(($m*$m - 3) / 2);
|
||||
$s[$j] = 0;
|
||||
while ($j < $half) {
|
||||
$s[$j] = 0;
|
||||
$j += $m;
|
||||
}
|
||||
}
|
||||
$i = $i + 1;
|
||||
$m = 2*$i + 3;
|
||||
}
|
||||
my @res = (2);
|
||||
foreach (@s) {
|
||||
push(@res, $_) if ($_);
|
||||
}
|
||||
return @res;
|
||||
}
|
||||
|
||||
my @res;
|
||||
for (1..10) {
|
||||
@res = get_primes7(10000000);
|
||||
print "Found ".(scalar @res)." prime numbers.\n";
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
import sys
|
||||
|
||||
def get_primes7(n):
|
||||
"""
|
||||
standard optimized sieve algorithm to get a list of prime numbers
|
||||
--- this is the function to compare your functions against! ---
|
||||
"""
|
||||
if n < 2: return []
|
||||
if n == 2: return [2]
|
||||
# do only odd numbers starting at 3
|
||||
if sys.version_info.major <= 2:
|
||||
s = range(3, n+1, 2)
|
||||
else: # Python 3
|
||||
s = list(range(3, n+1, 2))
|
||||
# n**0.5 simpler than math.sqr(n)
|
||||
mroot = n ** 0.5
|
||||
half = len(s)
|
||||
i = 0
|
||||
m = 3
|
||||
while m <= mroot:
|
||||
if s[i]:
|
||||
j = (m*m-3)//2 # int div
|
||||
s[j] = 0
|
||||
while j < half:
|
||||
s[j] = 0
|
||||
j += m
|
||||
i = i+1
|
||||
m = 2*i+3
|
||||
return [2]+[x for x in s if x]
|
||||
|
||||
for t in range(10):
|
||||
res = get_primes7(10000000)
|
||||
print("Found {} prime numbers.".format(len(res)))
|
Binary file not shown.
|
@ -0,0 +1,39 @@
|
|||
#!/bin/bash
|
||||
|
||||
EXPSTR='Found 664579 prime numbers.'
|
||||
|
||||
function run_benchmark() {
|
||||
HEADER="$1"
|
||||
CMD1="$2"
|
||||
CMD2="$3"
|
||||
VERSION_CMD="$4"
|
||||
VERSION_FILTER_CMD="$5"
|
||||
|
||||
echo "== $HEADER =="
|
||||
for n in {1..2}; do
|
||||
$CMD1 && OUT=$(time $CMD2)
|
||||
|
||||
echo "$OUT" | grep -xv "$EXPSTR" # check that all scripts output the same lines
|
||||
|
||||
NLINES=$(echo "$OUT"|wc -l)
|
||||
[ "$NLINES" == '10' ] || echo "Unexpected loops count: $NLINES"
|
||||
done
|
||||
echo
|
||||
|
||||
$VERSION_CMD | $VERSION_FILTER_CMD
|
||||
echo
|
||||
}
|
||||
|
||||
C='g++' ; run_benchmark 'C++ (optimized with -O2)' "$C -Wall -O2 primes.cpp -o primes.cpp.out" './primes.cpp.out' "$C --version" 'head -n1'
|
||||
rm -f ./primes.cpp.out
|
||||
C='g++' ; run_benchmark 'C++ (not optimized)' "$C -Wall primes.cpp -o primes.cpp.out" './primes.cpp.out' "$C --version" 'head -n1'
|
||||
rm -f ./primes.cpp.out
|
||||
C='python2.7' ; run_benchmark 'Python 2.7' 'true' "$C ./primes.py" "$C -V" 'cat'
|
||||
C='python3.2' ; run_benchmark 'Python 3.2' 'true' "$C ./primes.py" "$C -V" 'cat'
|
||||
C='perl' ; run_benchmark 'Perl' 'true' "$C ./primes.pl" "$C -v" 'grep built'
|
||||
C='php' ; run_benchmark 'PHP' 'true' "$C ./primes.php" "$C -v" 'head -n1'
|
||||
C='javac' ; run_benchmark 'Java (std)' "$C primes.java" 'java PrimeNumbersBenchmarkApp' "$C -version" 'cat'
|
||||
rm -f PrimeNumbersBenchmarkApp.class PrimeNumbersGenerator.class
|
||||
C='javac' ; run_benchmark 'Java (non-std)' "$C primes-non-std-lib.java" 'java PrimeNumbersBenchmarkApp' "$C -version" 'cat'
|
||||
rm -f PrimeNumbersBenchmarkApp.class PrimeNumbersGenerator.class IntList.class
|
||||
C='node' ; run_benchmark 'JavaScript (nodejs)' 'true' "$C ./primes.js" "$C -v" 'cat'
|
Loading…
Reference in New Issue