Browse Source

012 begin

master
Coin de Gamma 2 years ago
parent
commit
57ba9d2f04
  1. 3
      012/.gitignore
  2. 5
      012/README.md
  3. 36
      012/compute_primes.rs
  4. 34
      012/main.rs
  5. 26
      012/main_basic.rs

3
012/.gitignore vendored

@ -0,0 +1,3 @@
primes.txt
compute_primes
main_basic

5
012/README.md

@ -0,0 +1,5 @@
Generate primes list before execution
```
rustc compute_primes.rs && ./compute_primes > primes.txt
```

36
012/compute_primes.rs

@ -0,0 +1,36 @@
const SIZE: usize = 1500000;
type Rs = [bool; SIZE + 1];
fn remove_divs(m: usize, xs: &mut Rs) {
let mut i = m + m;
while i <= SIZE {
xs[i] = false;
i += m;
}
}
fn get_next_p(m_from: usize, xs: &Rs) -> Option<usize> {
for k in (m_from + 1)..=SIZE {
if xs[k] { return Some(k); }
}
return None;
}
fn main() {
let mut xs: Rs = [true; SIZE + 1];
let mut q: usize = 1;
let mut count = 0;
loop {
match get_next_p(q, &xs) {
Some(p) => {
q = p;
count += 1;
println!("{}", p);
if count == 500 { break; }
},
None => break
}
remove_divs(q, &mut xs);
}
// println!("[{}]: {}", count, q);
}

34
012/main.rs

@ -0,0 +1,34 @@
use std::fs::File;
use std::io::{ self, BufRead, BufReader };
const PS_SIZE: usize = 500usize;
fn get_ps() -> Vec<u32> {
let file = File::open("primes.txt").unwrap();
let lines = io::BufReader::new(file).lines();
return lines.map(|line| line.unwrap().parse::<u32>().unwrap())
.collect();
}
fn divs_num_test(n: u32) -> u32 {
let mut res = 0;
for i in 1..n {
if n % i == 0 { res += 1; }
}
return res;
}
fn main() {
let ps = get_ps();
let mut n = 1;
let mut best_dn = 1;
for k in 2..50000 {
n += k;
let dn = 500; //500; //divs_num(n);
//if dn > best_dn {
// best_dn = dn;
//println!("[{}]{}:{}",k, n, dn);
//}
}
}

26
012/main_basic.rs

@ -0,0 +1,26 @@
use std::fs::File;
use std::io::{ self, BufRead, BufReader };
const PS_SIZE: usize = 500usize;
fn divs_num(n: u32) -> u32 {
let mut res = 0;
for i in 1..n {
if n % i == 0 { res += 1; }
}
return res;
}
fn main() {
let mut n = 1;
let mut best_dn = 1;
for k in 2..50000 {
n += k;
let dn = divs_num(n);
if dn > best_dn {
best_dn = dn;
println!("[{}]{}:{}",k, n, dn);
}
}
}
Loading…
Cancel
Save