@@ -16,46 +16,48 @@ The modulus operator `%` is the key to solving fizz buzz.
16
16
17
17
The modulus operator returns the remainder after an integer division. Here is an example of the modulus operator:
18
18
19
- | Division | Division Result | Modulus | Modulus Result |
20
- | ------------- | -------------------------- | --------------- | ---------------: |
21
- | 1 / 3 | 0 with a remainder of 3 | 1 % 3 | 1 |
22
- | 5 / 3 | 1 with a remainder of 2 | 5 % 3 | 2 |
23
- | 16 / 3 | 5 with a remainder of 1 | 16 % 3 | 1 |
19
+ | Division | Division Result | Modulus | Modulus Result|
20
+ | ----------- | --------------------- | ------------- | : -----------: |
21
+ | 1 / 3 | 0 with a remainder of 3 | 1 % 3 | 1 |
22
+ | 5 / 3 | 1 with a remainder of 2 | 5 % 3 | 2 |
23
+ | 16 / 3 | 5 with a remainder of 1 | 16 % 3 | 1 |
24
24
25
25
A common approach to determine if a number is even or odd is to use the modulus operator:
26
26
27
- | Modulus | Result | Swift Code | Swift Code Result | Comment |
28
- | ------------- | ---------------:| ------------------------------- | -----------------:| --------------------------------------------- |
29
- | 6 % 2 | 0 | ` let isEven = (number % 2 == 0) ` | ` true ` | If a number is divisible by 2 it is * even* |
30
- | 5 % 2 | 1 | ` let isOdd = (number % 2 != 0) ` | ` true ` | If a number is not divisible by 2 it is * odd* |
27
+ | Modulus | Result | Swift Code | Swift Code<br >Result | Comment |
28
+ | -------- | :-----:| -------------------------------- | :----------------:| --------------------------------------------- |
29
+ | 6 % 2 | 0 | ` let isEven = (number % 2 == 0) ` | ` true ` | If a number is divisible by 2 it is * even* |
30
+ | 5 % 2 | 1 | ` let isOdd = (number % 2 != 0) ` | ` true ` | If a number is not divisible by 2 it is * odd* |
31
+
32
+ Alternatively, Swift's built in function .isMultiple(of:) can be used, i.e. 6.isMultiple(of: 2) will return true, 5.isMultiple(of: 2) will return false
31
33
32
34
## Solving fizz buzz
33
35
34
- Now we can use the modulus operator ` % ` to solve fizz buzz.
36
+ Now we can use the modulus operator ` % ` or .isMultiple(of:) method to solve fizz buzz.
35
37
36
38
Finding numbers divisible by three:
37
39
38
- | Modulus | Modulus Result | Swift Code | Swift Code Result |
39
- | ------- | --------------: | ------------- | ------------------: |
40
- | 1 % 3 | 1 | ` 1 % 3 == 0 ` | ` false ` |
41
- | 2 % 3 | 2 | ` 2 % 3 == 0 ` | ` false ` |
42
- | 3 % 3 | 0 | ` 3 % 3 == 0 ` | ` true ` |
43
- | 4 % 3 | 1 | ` 4 % 3 == 0 ` | ` false ` |
40
+ | Modulus | Modulus< br > Result | Swift Code< br >using Modulo | Swift Code< br >using .isMultiple(of:) | Swift Code< br > Result |
41
+ | ------- | : ---------------: | -------------------------- | ------------------------------------ | ------------------- |
42
+ | 1 % 3 | 1 | ` 1 % 3 == 0 ` | ` 1.isMultiple(of: 3) ` | ` false ` |
43
+ | 2 % 3 | 2 | ` 2 % 3 == 0 ` | ` 2.isMultiple(of: 3) ` | ` false ` |
44
+ | 3 % 3 | 0 | ` 3 % 3 == 0 ` | ` 3.isMultiple(of: 3) ` | ` true ` |
45
+ | 4 % 3 | 1 | ` 4 % 3 == 0 ` | ` 4.isMultiple(of: 3) ` | ` false ` |
44
46
45
47
Finding numbers divisible by five:
46
48
47
- | Modulus | Modulus Result | Swift Code | Swift Code Result |
48
- | ------- | --------------: | ------------- | ------------------: |
49
- | 1 % 5 | 1 | ` 1 % 5 == 0 ` | ` false ` |
50
- | 2 % 5 | 2 | ` 2 % 5 == 0 ` | ` false ` |
51
- | 3 % 5 | 3 | ` 3 % 5 == 0 ` | ` false ` |
52
- | 4 % 5 | 4 | ` 4 % 5 == 0 ` | ` false ` |
53
- | 5 % 5 | 0 | ` 5 % 5 == 0 ` | ` true ` |
54
- | 6 % 5 | 1 | ` 6 % 5 == 0 ` | ` false ` |
49
+ | Modulus | Modulus< br > Result | Swift Code< br >using Modulo | Swift Code< br >using .isMultiple(of:) | Swift Code< br > Result |
50
+ | ------- | : ---------------: | -------------------------- | ------------------------------------ | -------------------- |
51
+ | 1 % 5 | 1 | ` 1 % 5 == 0 ` | ` 1.isMultiple(of: 5) ` | ` false ` |
52
+ | 2 % 5 | 2 | ` 2 % 5 == 0 ` | ` 2.isMultiple(of: 5) ` | ` false ` |
53
+ | 3 % 5 | 3 | ` 3 % 5 == 0 ` | ` 3.isMultiple(of: 5) ` | ` false ` |
54
+ | 4 % 5 | 4 | ` 4 % 5 == 0 ` | ` 4.isMultiple(of: 5) ` | ` false ` |
55
+ | 5 % 5 | 0 | ` 5 % 5 == 0 ` | ` 5.isMultiple(of: 5) ` | ` true ` |
56
+ | 6 % 5 | 1 | ` 6 % 5 == 0 ` | ` 6.isMultiple(of: 5) ` | ` false ` |
55
57
56
58
## The code
57
59
58
- Here is a simple implementation in Swift:
60
+ Here is a simple implementation in Swift using Modulus approach
59
61
60
62
``` swift
61
63
func fizzBuzz (_ numberOfTurns : Int ) {
@@ -79,18 +81,57 @@ func fizzBuzz(_ numberOfTurns: Int) {
79
81
}
80
82
```
81
83
82
- Put this code in a playground and test it like so:
84
+ Here is simple implementation in Swift using .isMultiple(of:) and switch statement
85
+
86
+ ``` swift
87
+ func fizzBuzz (_ numberOfTurns : Int ) {
88
+ guard numberOfTurns >= 1 else {
89
+ print (" Number of turns must be >= 1" )
90
+ return
91
+ }
92
+
93
+ for i in 1 ... numberOfTurns {
94
+ switch (i.isMultiple (of : 3 ), i.isMultiple (of : 5 )) {
95
+ case (false , false ):
96
+ print (" \( i ) " )
97
+ case (true , false ):
98
+ print (" Fizz" )
99
+ case (false , true ):
100
+ print (" Buzz" )
101
+ case (true , true ):
102
+ print (" Fizz Buzz" )
103
+ }
104
+ }
105
+ }
106
+ ```
107
+
108
+ Put either code in a playground and test it like so:
83
109
84
110
``` swift
85
111
fizzBuzz (15 )
86
112
```
87
113
88
114
This will output:
89
115
90
- 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz
116
+ 1
117
+ 2
118
+ Fizz
119
+ 4
120
+ Buzz
121
+ Fizz
122
+ 7
123
+ 8
124
+ Fizz
125
+ Buzz
126
+ 11
127
+ Fizz
128
+ 13
129
+ 14
130
+ Fizz Buzz
91
131
92
132
## See also
93
133
94
134
[ Fizz buzz on Wikipedia] ( https://en.wikipedia.org/wiki/Fizz_buzz )
95
135
96
- * Written by [ Chris Pilcher] ( https://github.com/chris-pilcher ) *
136
+ * Originally written by [ Chris Pilcher] ( https://github.com/chris-pilcher ) * <br >
137
+ * Updated by [ Lance Rettberg] ( https://github.com/l-rettberg ) *
0 commit comments