@@ -16,46 +16,48 @@ The modulus operator `%` is the key to solving fizz buzz.
1616
1717The modulus operator returns the remainder after an integer division. Here is an example of the modulus operator:
1818
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 |
2424
2525A common approach to determine if a number is even or odd is to use the modulus operator:
2626
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
3133
3234## Solving fizz buzz
3335
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.
3537
3638Finding numbers divisible by three:
3739
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 ` |
4446
4547Finding numbers divisible by five:
4648
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 ` |
5557
5658## The code
5759
58- Here is a simple implementation in Swift:
60+ Here is a simple implementation in Swift using Modulus approach
5961
6062``` swift
6163func fizzBuzz (_ numberOfTurns : Int ) {
@@ -79,18 +81,57 @@ func fizzBuzz(_ numberOfTurns: Int) {
7981}
8082```
8183
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:
83109
84110``` swift
85111fizzBuzz (15 )
86112```
87113
88114This will output:
89115
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
91131
92132## See also
93133
94134[ Fizz buzz on Wikipedia] ( https://en.wikipedia.org/wiki/Fizz_buzz )
95135
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