Skip to content

Instantly share code, notes, and snippets.

@nobishino
Created April 21, 2020 12:00
Show Gist options
  • Save nobishino/de549d6f166e00100e9ec90e457be836 to your computer and use it in GitHub Desktop.
Save nobishino/de549d6f166e00100e9ec90e457be836 to your computer and use it in GitHub Desktop.
use std::collections::HashSet;
fn main() {
let mut candidate_count = 0;
for i in 1..100000 {
if satisfy(i) {
candidate_count += 1;
println!("Candidate: {}", i.to_string());
}
}
println!("{}", candidate_count.to_string());
}
fn satisfy(x: i32) -> bool {
return digits_sum(x) == 22 && digits(x) == 5 && phi(x) == 15;
}
fn digits_sum(x: i32) -> i32 {
let mut result: i32 = 0;
let mut x = x;
while x > 0 {
result += x % 10;
x /= 10;
}
return result;
}
fn digits(x: i32) -> i32 {
if x == 0 {
return 1;
}
let mut result: i32 = 0;
let mut x = x;
while x > 0 {
result += 1;
x /= 10;
}
return result;
}
fn phi(x: i32) -> i32 {
let bound = (x as f64).sqrt().ceil() as i32 + 1;
let mut dividers = HashSet::new();
for i in 1..bound {
if x % i == 0 {
dividers.insert(i);
dividers.insert(x / i);
}
}
return dividers.len() as i32;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment