pub fn is_perfect_square(num: i32) -> bool { | |||||
for x in 1..46341 { | |||||
let res = (x as i32).pow(2); | |||||
if res > num { | |||||
return false; | |||||
} | |||||
if res == num { | |||||
return true; | |||||
} | |||||
} | |||||
return false; | |||||
} | |||||
fn main() { | |||||
println!("Expected: true"); | |||||
println!("Got: {}", is_perfect_square(16)); | |||||
} |
Given a positive integer num, write a function which returns True if num is a perfect square else False. | |||||
Note: Do not use any built-in library function such as sqrt. | |||||
Example 1: | |||||
Input: 16 | |||||
Output: true | |||||
Example 2: | |||||
Input: 14 | |||||
Output: false | |||||
#!/bin/bash | |||||
rustc main.rs | |||||
./main | |||||
rm main |