Pārlūkot izejas kodu

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

Lorenz Bung 8 gadus atpakaļ
vecāks
revīzija
5f20517a5e
1 mainītis faili ar 10 papildinājumiem un 3 dzēšanām
  1. 10
    3
      hw1/task2/src/lib.rs

+ 10
- 3
hw1/task2/src/lib.rs Parādīt failu

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
 }

Notiek ielāde…
Atcelt
Saglabāt