Last active
August 29, 2015 14:23
-
-
Save eneas/70cf829bae7b4ba4832b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//Sieve of Eratosthenes | |
fn main() { | |
let primes = sieve_of_eratothenes(100000); | |
println!("{:?}",primes); | |
} | |
fn sieve_of_eratothenes(max:i32)-> Vec<i32>{ | |
let mut prime_accu :Vec<i32> = Vec::new(); | |
for i in 2..max{ | |
is_out_add(i,&mut prime_accu) | |
} | |
prime_accu | |
} | |
fn is_out_add(num: i32,v: &mut Vec<i32>){ | |
for i in v.iter(){ | |
if (num%*i)==0 {return}; | |
} | |
v.push(num); | |
return; | |
} |
This file contains hidden or 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
use std::env; | |
fn main() { | |
if env::args().len()<3 { | |
panic!("Two arguments needed"); | |
} | |
let a = get_u64_nth_arg(1); | |
let b = get_u64_nth_arg(2); | |
let mcd=euclides(a,b); | |
println!("a = {}\nb = {}", a, b); | |
if mcd==b { | |
println!("a/b = {}",a/mcd); | |
} | |
else { | |
println!("a/b= {}/{}",a/mcd,b/mcd); | |
} | |
println!("MCD(a,b) = {}",mcd); | |
println!("mcm(a,b) = {}",a*(b/mcd)); //FIXME: maybe overflow | |
} | |
fn euclides(a: u64,b: u64) ->u64 { | |
let (mut r0, mut r1) = (a,b); | |
while r1!=0 { | |
let swap =r0; | |
r0=r1; | |
r1=swap%r1; | |
} | |
r0 | |
} | |
fn get_u64_nth_arg(n: usize) -> u64 { | |
env::args() | |
.nth(n) | |
.unwrap() | |
.parse() | |
.unwrap_or_else(|_|{ | |
panic!("Arg {} must be an integer.",n) | |
} | |
) | |
} | |
~ |
This file contains hidden or 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
fn main() { | |
let q=7129u64; | |
let p=2677u64; | |
let n=p*q; | |
let e =1025u64; | |
assert_eq!(gcd(e,(p-1)*(q-1)),1); | |
let d= public_exponent(p,q,e); | |
let txt=1000; | |
let cfr=exp(txt,e,n); | |
assert_eq!(txt,exp(cfr,d,n)); | |
} | |
fn public_exponent(p: u64,q: u64,e: u64 )-> u64{ //p,q primes,e private exponent | |
let n=(p-1)*(q-1); | |
exp(e,phi_factored(p-1,q-1)-1,n) | |
} | |
fn phi_factored(x: u64,y: u64)-> u64{ | |
let cd=gcd(x,y); | |
phi(x)*phi(y)*cd/phi(cd) | |
} | |
fn phi(n :u64)-> u64 { | |
let mut acc:u64=0; | |
for i in 1..n { | |
if gcd(i,n)==1{ | |
acc+=1; | |
} | |
} | |
acc | |
} | |
fn gcd(a: u64,b: u64) ->u64 { | |
let (mut r0, mut r1) = (a,b); | |
while r1!=0 { | |
let swap =r0; | |
r0=r1; | |
r1=swap%r1; | |
} | |
r0 | |
} | |
fn exp(a: u64,b: u64,md: u64) -> u64 { //FIXME: It can be better. | |
let r=a%md; | |
if b==1{ | |
r | |
} | |
else if b%2==0 { | |
exp((r*r)%md,b/2,md) | |
} | |
else { | |
(r*(exp((r*r)%md,(b-1)/2,md)))%md | |
} | |
} |
This file contains hidden or 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
fn main() { | |
println!("phi(9) = {}",euler_totient_naive(9)); | |
} | |
fn euler_totient_naive(n :u64)-> u64 { | |
let mut acc:u64=0; | |
for i in 1..n { | |
if gcd(i,n)==1{ | |
acc+=1; | |
} | |
} | |
acc | |
} | |
fn gcd(a: u64,b: u64) ->u64 { | |
let (mut r0, mut r1) = (a,b); | |
while r1!=0 { | |
let swap =r0; | |
r0=r1; | |
r1=swap%r1; | |
} | |
r0 | |
} |
This file contains hidden or 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
fn main() { | |
let q=23u64; | |
let p=17u64; | |
let n=p*q; | |
let e =3u64; | |
let d= public_exponent(p,q,e); | |
let txt=99; | |
let cfr=exp(txt,e,n); | |
println!("{}",exp(cfr,d,n)); | |
} | |
fn public_exponent(p: u64,q: u64,e: u64 )-> u64{ //p,q primes,e private exponent | |
let n=(p-1)*(q-1); | |
exp(e,phi(n)-1,n) | |
} | |
fn phi(n :u64)-> u64 { | |
let mut acc:u64=0; | |
for i in 1..n { | |
if gcd(i,n)==1{ | |
acc+=1; | |
} | |
} | |
acc | |
} | |
fn gcd(a: u64,b: u64) ->u64 { | |
let (mut r0, mut r1) = (a,b); | |
while r1!=0 { | |
let swap =r0; | |
r0=r1; | |
r1=swap%r1; | |
} | |
r0 | |
} | |
fn product(a: u64,b: u64,md: u64) -> u64 { | |
((a%md)*(b%md))%md | |
} | |
fn exp(a: u64,b: u64,md: u64) -> u64{ | |
let mut r=a%md; | |
for _ in 1..b { | |
r=product(r,a,md); | |
} | |
r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment