Code Monkey home page Code Monkey logo

lincas's Introduction

LinCas

.net Language Integrated Computer algebra system

lincas's People

Contributors

bugbit avatar

Watchers

 avatar  avatar

lincas's Issues

Is number primer?

https://cosas.wordpress.com/2010/10/25/la-criba-de-sundaram/

https://es.wikibooks.org/wiki/Implementaci%C3%B3n_de_algoritmos_de_teor%C3%ADa_de_n%C3%BAmeros/Criba_de_Erat%C3%B3stenes

http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=primalityTesting

https://stackoverflow.com/questions/2468412/finding-a-prime-number-after-a-given-number/2473188#2473188

#include
using namespace std;

/* This function calculates (ab)%c /
int modulo(int a,int b,int c){
long long x=1,y=a; // long long is taken to avoid overflow of intermediate results
while(b > 0){
if(b%2 == 1){
x=(x
y)%c;
}
y = (y*y)%c; // squaring the base
b /= 2;
}
return x%c;
}

/* this function calculates (ab)%c taking into account that ab might overflow /
long long mulmod(long long a,long long b,long long c){
long long x = 0,y=a%c;
while(b > 0){
if(b%2 == 1){
x = (x+y)%c;
}
y = (y
2)%c;
b /= 2;
}
return x%c;
}

/* Miller-Rabin primality test, iteration signifies the accuracy of the test */
bool Miller(long long p,int iteration){
if(p<2){
return false;
}
if(p!=2 && p%2==0){
return false;
}
long long s=p-1;
while(s%2==0){
s/=2;
}
for(int i=0;i<iteration;i++){
long long a=rand()%(p-1)+1,temp=s;
long long mod=modulo(a,temp,p);
while(temp!=p-1 && mod!=1 && mod!=p-1){
mod=mulmod(mod,mod,p);
temp *= 2;
}
if(mod!=p-1 && temp%2==0){
return false;
}
}
return true;
}

int main(int argc, char* argv[])
{

int input = 1000;
int i = 0;

if(input%2==0)
    i = input+1;
else i = input;

for(;i<2*input;i+=2) // from Rajendra's answer
    if(Miller(i,20)) // 18-20 iterations are enough for most of the applications.
        break;
cout<<i<<endl;

return 0;

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.