Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Happy-numbers/Rust/happy-numbers.rust
Normal file
36
Task/Happy-numbers/Rust/happy-numbers.rust
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#![feature(core)]
|
||||
|
||||
fn sumsqd(mut n: i32) -> i32 {
|
||||
let mut sq = 0;
|
||||
while n > 0 {
|
||||
let d = n % 10;
|
||||
sq += d*d;
|
||||
n /= 10
|
||||
}
|
||||
sq
|
||||
}
|
||||
|
||||
use std::num::Int;
|
||||
fn cycle<T: Int>(a: T, f: fn(T) -> T) -> T {
|
||||
let mut t = a;
|
||||
let mut h = f(a);
|
||||
|
||||
while t != h {
|
||||
t = f(t);
|
||||
h = f(f(h))
|
||||
}
|
||||
t
|
||||
}
|
||||
|
||||
fn ishappy(n: i32) -> bool {
|
||||
cycle(n, sumsqd) == 1
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let happy = std::iter::count(1, 1)
|
||||
.filter(|&n| ishappy(n))
|
||||
.take(8)
|
||||
.collect::<Vec<i32>>();
|
||||
|
||||
println!("{:?}", happy)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue