Selaa lähdekoodia

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

Lorenz Bung 8 vuotta sitten
vanhempi
commit
5f20517a5e
1 muutettua tiedostoa jossa 10 lisäystä ja 3 poistoa
  1. 10
    3
      hw1/task2/src/lib.rs

+ 10
- 3
hw1/task2/src/lib.rs Näytä tiedosto

@@ -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…
Peruuta
Tallenna