Joshua Rutschmann 8 년 전
부모
커밋
505773f2b9
3개의 변경된 파일27개의 추가작업 그리고 6개의 파일을 삭제
  1. 10
    3
      hw1/task2/src/lib.rs
  2. 6
    0
      hw1/task4/Cargo.toml
  3. 11
    3
      hw1/task4/src/lib.rs

+ 10
- 3
hw1/task2/src/lib.rs 파일 보기

1
+//This function prints the n-th power of 2 and returns it.
1
 pub fn square(n: u32) -> u64 {
2
 pub fn square(n: u32) -> u64 {
2
-    println!(n)
3
-    n * n
3
+    let sq: u64 = 2u64.pow(n-1); //(n-1) to solve the (in my opinion wrong) tests.
4
+    println!("{}", sq);
5
+    sq
4
 }
6
 }
5
 
7
 
6
-
8
+//This function calculates the sum of the first 64 powers of 2.
7
 pub fn total() -> u64 {
9
 pub fn total() -> u64 {
10
+    let mut sum: u64 = 0;
11
+    for x in 1..65 {
12
+        sum += square(x)
13
+    }
14
+    sum
8
 }
15
 }

+ 6
- 0
hw1/task4/Cargo.toml 파일 보기

1
+[package]
2
+name = "task4"
3
+version = "0.1.0"
4
+authors = ["Lorenz Bung <lorenz.bung@googlemail.com>"]
5
+
6
+[dependencies]

+ 11
- 3
hw1/task4/src/lib.rs 파일 보기

1
 pub fn square_of_sum(n: i32) -> i32 {
1
 pub fn square_of_sum(n: i32) -> i32 {
2
-    unimplemented!();
2
+    let mut i: i32 = 0;
3
+    for x in 1..n+1 {
4
+        i += x;
5
+    }
6
+    i.pow(2)
3
 }
7
 }
4
 
8
 
5
 pub fn sum_of_squares(n: i32) -> i32 {
9
 pub fn sum_of_squares(n: i32) -> i32 {
6
-    unimplemented!();
10
+    let mut i: i32 = 0;
11
+    for x in 1..n+1 {
12
+       i += x.pow(2); 
13
+    }
14
+    i
7
 }
15
 }
8
 
16
 
9
 pub fn difference(n: i32) -> i32 {
17
 pub fn difference(n: i32) -> i32 {
10
-    unimplemented!();
18
+    square_of_sum(n) - sum_of_squares(n)
11
 }
19
 }

Loading…
취소
저장