Every now and then, you get this assignment where you have to resolve a logical mathematical question.

I don’t know for you but for me, it’s always a challenge…

Here was a question on the https://projecteuler.net website for the first question:

*“**If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. **Find the sum of all the multiples of 3 or 5 below 1000”.*

It’s like whaaaat???… Oh, boy!

What is a natural number by the way? The first thing that came into my mind… Sorry, I was at school but that was a long time ago…

Hopefully, Google came to the rescue… Of course! How could I forget about it? Shame on me! 😦

But how do you translate this logical mathematical question into code?

“*Baby, I need coffee! And make it strong! Just keep the aspirin not too far…”*

For a definition of the natural number, see this site: https://www.mathsisfun.com/numbers/evolution-of-numbers.html

Natural number below 10: which means 1, 2, 3, 4, 5, 6, 7, 8 and 9.

Multiple of 3 or 5, which means:

1 x 1 = 1

1 x 2 = 2

1 x **3 = 3 ==>**** good**

1 x 4 = 4

1 x **5 = 5 ==> ****good**

1 x **6 = 6 ==>**** 3 x 2 ==>**** good**

1 x 7 = 7

1 x 8 = 8

1 x **9 = 9 ==>**** 3 x 3 ==>**** good**

** **The sum of these multiples is 23.

3 + 5 + 6 + 9 = 23

Pfff! One thing done! Coding is like trying to solve one thing and then jumping onto the next one…

Ok! Here I need a kind of loop to go through from 1 to less than 10.

*“MAYDAY! MAYDAY! MAYDAY!”*

Swift language to the rescue, or any other programming languages will do the job.

For the loop, I am going with the “*for.. in”* loop, foundation of the programming language.

**for** i **in** 1..<10 {

// do something

}

So translate like: *for each index from 1 to less than 10*, do the following inside the curly braces.

“i” is the name of the variable ( “i” like index) that will change every time you reiterate through the loop until you hit the wall of less than 10.

1..<10 ==> range from 1 to less than 10.

Ok, I need 3, 5, 6, and 9. How can I tell the code to highlight those numbers between 1 and less than 10?

There is a trick, and it is called the modulo (see YouTube example at: https://www.youtube.com/watch?v=b5cb_nfDyyM)

In other words, you’re looking to have a remainder of 0.

3/ 3 = 1.0

5 / 5 = 1.0

6 / 3 = 2.0

9 / 3 = 3.0

Multiples of 3 and 5; I’ll need to check those 2 numbers within the loop.

% is the symbol for modulo.

i % 3 or i % 5 must equal to 0, where “i” is the index I pass on every time the loop goes through, yeah? So i = 1 the first time, then 2, 3, etc. until it hits the less than 10 condition.

if (i % 3 == 0) || (i % 5 == 0) {

// if one condition or the other is true, do something.

}

|| = “or” in this language.

== is the equal we know.

if (i % 3 == 0) || (i % 5 == 0) {

sum += i

}

“sum” is the variable I have declared on line 5 in my playground (var sum = 0).

“var” stands for “variable”, a drawer from my closet that I have reserved to contain my variable, so to speak.

The “+=” is a short cut for “sum = sum + i”. Easier to write “sum += i”.

Like you would say “FYI”, for “For Your Information”. Just a question to get used to it…

Then on line 12, I just print the result of my variable “sum”.

*“Open the drawer baby and show me what you’ve got!”*

Pfff! Brainteaser…

Like the iron man, you need to train your brain to resolve problems. 😦

Same in aviation, we train a lot. Actually, I found a lot of similarities between piloting and computing. Pilots have a real potential of becoming good coders (problem solvers) 😉

See one solution here below. All roads go to Rome, so there is more than one solution 😉

I’ve used Xcode IDE, the IDE for building apps for Mac, iPhone, iPad, Apple Watch, and Apple TV.

For more information please visit https://developer.apple.com/xcode/ide/