Last active
September 18, 2015 13:52
-
-
Save jkgiesler/4f104279eecc2f0d3d8c to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#include <math.h> | |
#include <vector> | |
bool is_prime(int n){ | |
if(n<2) return false; | |
if(n==2) return true; | |
if (!(n & 1)) return false; | |
int stop_n = floor(sqrt(n)); | |
for(int i=3;i < stop_n;i+=2){ | |
if(n%i==0) return false; | |
} | |
return true; | |
} | |
//this implemention found here: http://www.geeksforgeeks.org/sieve-of-eratosthenes/ | |
void markMultiples(int * arr,int a,int n){ | |
int i=2,num; | |
while ((num = i*a)<=n) | |
{ | |
arr[num-1]=1; | |
i++; | |
} | |
} | |
std::vector<int> sieve(int n){ | |
int *arr = new int[n]; | |
std::vector<int> solution; | |
std::memset(arr,0,n*(sizeof(int))); | |
for (int i=1;i<n;i++){ | |
if (arr[i]==0) | |
{ | |
solution.push_back(i+1); | |
markMultiples(arr,i+1,n); | |
} | |
} | |
delete[] arr; | |
return solution; |
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
bool is_prime(int n); | |
void markMultiples(int * arr,int a,int n); | |
std::vector<int> sieve(int n); |
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
%module jkg_prime | |
%{ | |
#include "jkg_prime.h" | |
%} | |
%include "jkg_prime.h" | |
%include "typemaps.i" | |
%include "std_vector.i" | |
%template(IntVector) std::vector<int>; |
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
from distutils.core import setup, Extension | |
import numpy | |
import os | |
os.environ['CC'] = 'g++'; | |
setup(name='jkg_prime', version='1.0', ext_modules =[Extension('_jkg_prime', | |
['jkg_prime.cpp', 'jkg_prime.i'], swig_opts=['-c++'], include_dirs = [numpy.get_include(),'.'])]) |
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
import jkg_prime | |
print(list(jkg_prime.sieve(30))) | |
if jkg_prime.is_prime(29): | |
print('yay') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment