[initial] first edition
This commit is contained in:
commit
a821267eda
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@ -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"
|
||||||
15
Cargo.toml
Normal file
15
Cargo.toml
Normal file
@ -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]
|
||||||
32
src/main.rs
Normal file
32
src/main.rs
Normal file
@ -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::<f64>()
|
||||||
|
);
|
||||||
|
|
||||||
|
// 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::<u64>()
|
||||||
|
number
|
||||||
|
== 1 + (2..=number.isqrt())
|
||||||
|
.filter(|i| number % i == 0)
|
||||||
|
.map(|i| i + number / i * (i * i != number) as u64)
|
||||||
|
.sum::<u64>()
|
||||||
|
}
|
||||||
30
src/other_binary.rs
Normal file
30
src/other_binary.rs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user