Problem 63 "Powerful digit counts"
The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power.
How many n-digit positive integers exist which are also an nth power?
問 63 「べき乗の桁の個数」
5桁の数 16807 = 75は自然数を5乗した数である. 同様に9桁の数 134217728 = 89も自然数を9乗した数である.
自然数を n 乗して得られる n 桁の正整数は何個あるか?
fn main() { let mut count = 0u8; for a in 1..=9 { let exp = 1f32 / (1f32 - (a as f32).log10()); println!("a: {} \t exp: {}", a, exp); count += exp as u8; } println!("{}", count); assert_eq!(count, 49); }
fn main() { let mut count = 0u8; for a in 1u128..=9 { for exp in 1.. { let n = a.pow(exp); if n < 10u128.pow(exp - 1) { break; } println!("{} ^ {} \t = {}", a, exp, n); count += 1; } } println!("{}", count); assert_eq!(count, 49); }