Browse Source

058 with bugs

master
Coin de Gamma 10 months ago
parent
commit
2575982240
  1. 100
      058/main.rs

100
058/main.rs

@ -1,66 +1,102 @@
struct Step {
i: i8,
j: i8
use std::collections::HashSet;
// from../ 027/main.rs
fn er() -> HashSet<u32> {
let mut nums = Vec::new();
let size = 600_000_000;
for _n in 0..size+1 { nums.push(true); }
let mut p = 1;
loop {
let mut found = false;
for i in p+1..size {
if nums[i] {
found = true;
p = i;
break;
}
}
if !found { break; }
for i in (2 * p .. size).step_by(p) {
nums[i] = false;
}
}
let mut res = HashSet::new();
for i in 2..size {
if nums[i] { res.insert(i as u32); }
}
return res;
}
struct Steps {
l: usize,
i: usize,
j: usize,
k: usize,
s: usize,
}
impl Steps {
fn new(n: usize) -> Steps {
fn new() -> Steps {
return Steps {
i: n / 2,
j: n / 2,
l: 1,
k: 1,
s: 0
};
}
fn ads(&mut self, s: &Step) {
if s.i < 0 { self.i -= 1; }
if s.i > 0 { self.i += 1; }
if s.j < 0 { self.j -= 1; }
if s.j > 0 { self.j += 1; }
}
fn next(&mut self, n: u32) {
//self.corner = false;
let mut sts = Vec::new();
sts.push(Step { i: 0, j: 1 });
sts.push(Step { i: -1, j: 0 });
sts.push(Step { i: 0, j: -1 });
sts.push(Step { i: 1, j: 0 });
fn next(&mut self, n: u32) -> Option<u32> {
if self.k > self.l {
//self.corner = true;
self.k = 1;
self.s += 1;
if self.s == sts.len() {
if self.s == 4 {
self.s = 0;
self.l += 1;
}
if self.s == 2 {
self.l += 1;
}
println!("{}", n);
}
let st = &sts[self.s];
self.k += 1;
self.ads(st);
if self.s == 0 && (self.k - 1 == self.l || self.k == 2) {
return Some(n);
}
if self.s == 2 && self.k == 2 {
return Some(n);
}
if self.s == 3 && self.k == 2 {
return Some(n);
}
return None;
}
}
fn main() {
let mut st = Steps::new(7);
for n in 1..20 {
st.next(n);
let mut st = Steps::new();
let ps = er();
println!("er");
return;
let mut sq = 1;
let mut corner_count = 0;
let mut p_count = 0;
for n in 1..900_000_000 {
if n > sq * sq {
sq += 1;
}
match st.next(n) {
Some(k) => {
let is_prime = ps.contains(&k);
corner_count += 1;
if is_prime {
p_count += 1;
}
let ratio = 100.0 * (p_count as f64) /(corner_count as f64);
println!("{}.{} \t({})\t[{}]", k, is_prime, sq, ratio);
if ratio < 10.0 {
break;
}
},
None => ()
}
}
}

Loading…
Cancel
Save