DIVSUM - Divisor Summation
Given a natural number n (1 <= n <= 500000), please output the summation of all its proper divisors.
Definition: A proper divisor of a natural number is the divisor that is strictly less than the number.
e.g. number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + 2 + 4 + 5 + 10 = 22.
Input
An integer stating the number of test cases (equal to about 200000), and that many lines follow, each containing one integer between 1 and 500000 inclusive.
Output
One integer each line: the divisor summation of the integer given respectively.
Example
Sample Input: 3 2 10 20 Sample Output: 1 8 22
Warning: large Input/Output data, be careful with certain languages
解题:第一道用RUST写的水题,RUST的效率真心不敢恭维,尤其是IO效率
1 use std::io; 2 use std::io::prelude::*; 3 fn main(){ 4 let stdin = io::stdin(); 5 let mut lines = stdin.lock().lines(); 6 let n = lines.next().unwrap().unwrap().parse::<i32>().unwrap(); 7 for _ in 0..n { 8 let mut x = 1; 9 let mut sum = 0; 10 let y = lines.next().unwrap().unwrap().parse::<i32>().unwrap(); 11 while x*x <= y { 12 if y%x == 0{ 13 let tmp = y/x; 14 if x != y { 15 sum += x; 16 } 17 if tmp != x && tmp != y { 18 sum += tmp; 19 } 20 } 21 x = x + 1; 22 } 23 println!("{}",sum); 24 } 25 }