소스 검색

Made the function total() runnable and checked for tests.

Lorenz Bung 8 년 전
부모
커밋
5f20517a5e
1개의 변경된 파일10개의 추가작업 그리고 3개의 파일을 삭제
  1. 10
    3
      hw1/task2/src/lib.rs

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

@@ -1,8 +1,15 @@
1
+//This function prints the n-th power of 2 and returns it.
1 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 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
 }

Loading…
취소
저장