Kaynağa Gözat

INIT: task2

Michael Mächtel 8 yıl önce
ebeveyn
işleme
8ed7ea6a09
3 değiştirilmiş dosya ile 73 ekleme ve 0 silme
  1. 24
    0
      hw1/task2/README.md
  2. 8
    0
      hw1/task2/src/lib.rs
  3. 41
    0
      hw1/task2/tests/task2.rs

+ 24
- 0
hw1/task2/README.md Dosyayı Görüntüle

@@ -0,0 +1,24 @@
1
+# Homework hw1 task 2
2
+
3
+## prepare your task
4
+
5
+Run `cargo init` in your `task2/` directory.
6
+
7
+## task
8
+
9
+Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.
10
+
11
+There once was a wise servant who saved the life of a prince. The king
12
+promised to pay whatever the servant could dream up. Knowing that the
13
+king loved chess, the servant told the king he would like to have grains
14
+of wheat. One grain on the first square of a chess board. Two grains on
15
+the next. Four on the third, and so on.
16
+
17
+There are 64 squares on a chessboard.
18
+
19
+Write code that shows:
20
+
21
+- how many grains were on each square (`fn square(n: u32) -> u64`), and
22
+- the total number of grains (`fn total() -> u64`)
23
+
24
+Use the given file `lib.rs` for your solution.

+ 8
- 0
hw1/task2/src/lib.rs Dosyayı Görüntüle

@@ -0,0 +1,8 @@
1
+pub fn square(n: u32) -> u64 {
2
+    unimplemented!()
3
+}
4
+
5
+
6
+pub fn total() -> u64 {
7
+    unimplemented!();
8
+}

+ 41
- 0
hw1/task2/tests/task2.rs Dosyayı Görüntüle

@@ -0,0 +1,41 @@
1
+extern crate task2;
2
+
3
+#[test]
4
+fn square_one() {
5
+    assert_eq!(task2::square(1), 1);
6
+}
7
+
8
+#[test]
9
+fn square_two() {
10
+    assert_eq!(task2::square(2), 2);
11
+}
12
+
13
+#[test]
14
+fn square_three() {
15
+    assert_eq!(task2::square(3), 4);
16
+}
17
+
18
+#[test]
19
+fn square_four() {
20
+    assert_eq!(task2::square(4), 8);
21
+}
22
+
23
+#[test]
24
+fn square_sixteen() {
25
+    assert_eq!(task2::square(16), 32_768);
26
+}
27
+
28
+#[test]
29
+fn square_thirty_two() {
30
+    assert_eq!(task2::square(32), 2_147_483_648);
31
+}
32
+
33
+#[test]
34
+fn square_sixty_four() {
35
+    assert_eq!(task2::square(64), 9_223_372_036_854_775_808);
36
+}
37
+
38
+#[test]
39
+fn total_sums_all_squares() {
40
+    assert_eq!(task2::total(), 18_446_744_073_709_551_615);
41
+}

Loading…
İptal
Kaydet