commit a821267eda0f8ef053f8d8bd4fd06509500fd3c8 Author: YinMo19 Date: Thu Nov 20 16:04:58 2025 +0800 [initial] first edition diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..53550ba --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "course_1" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9bbd2d9 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "course_1" +version = "0.1.0" +edition = "2024" +default-run = "main" + +[[bin]] +name = "main" +path = "src/main.rs" + +[[bin]] +name = "other_binary" +path = "src/other_binary.rs" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..57328ec --- /dev/null +++ b/src/main.rs @@ -0,0 +1,32 @@ +fn main() { + // question 1 + println!( + "Sum of the reciprocal of square roots from 1 to 1,000,000 is {}", + (1..=1_000_000) + .map(|i| 1.0f64 / (i as f64).sqrt()) + .sum::() + ); + + // question 2 + let mut input = String::new(); + std::io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + println!("Counts of characters is {}", input.trim().len()); + + // question 3 + (2..=10000) + .filter(|&n| is_perfect(n)) + .for_each(|n| println!("{} is perfect number.", n)); +} + +/// find number's factors first, +/// and add them to check whether it is a perfect number +fn is_perfect(number: u64) -> bool { + // number == (1..=number / 2).filter(|i| number % i == 0).sum::() + number + == 1 + (2..=number.isqrt()) + .filter(|i| number % i == 0) + .map(|i| i + number / i * (i * i != number) as u64) + .sum::() +} diff --git a/src/other_binary.rs b/src/other_binary.rs new file mode 100644 index 0000000..2ea9649 --- /dev/null +++ b/src/other_binary.rs @@ -0,0 +1,30 @@ +use std::io; + +fn perfect(i: i32) -> bool { + let mut sum = 0; + for a in 1..i { + if i % a == 0 { + sum += a; + } + } + sum == i +} + +fn main() { + let mut sum: f32 = 0.0; + for i in 1..=10000 { + sum += 1.0 / f32::sqrt(i as f32); + } + println!("第一个问题的答案:和为{}", sum); + + println!("请输入一些字符"); + let mut input = String::new(); + io::stdin().read_line(&mut input).expect("读取失败"); + println!("你输入的字符串长度为{}", input.trim().chars().count()); + + for j in 1..=10000 { + if perfect(j) { + println!("{}", j); + } + } +}