Programming, Math, Algorithms and such.
Took a break from programming my game to practice on some math (a definite weak point of mine) and problem-solving.
So I thought I'd share some of it because fuckyouwhatever.
This first one is a common problem based off of some schoolkid number game. It requires you to print the numbers 1- 50 (or whatever), but when a number is a multiple of 3 print "Fizz", a multiple of 5 print "Buzz" or a multiple of 3 and 5 print "FizzBuzz".
So here is how I did it (in C#):
Well actually not really. That is kind of the easy/longer way to do it. I'll show another way later. For now lets talk about this code. First off, I printed the entire program above, so if you pasted that into a C# compiler (
like this one) you can see the program at work.
But, the real part of the code we need to focus on is this bit:
The program starts by initiating a for loop:
Which essentially reads: given (A variable we are declaring; meets a criteria; perform a function)
So in this instance we are declaring a variable (using the "var" declaration, which is an implicit declaration that basically says "a variable whose type I am going to declare right now") called i, which is an integer equal to 1. We declare it as 1 because this is where we want our loop to begin.
Next we say if the variable i is less-than-or-equal-to 50 (the highest number in our range), perform the function below; furthermore, each time the function is performed increment the variable i by one (using the short-hand operator i++) and restart the loop.
The function we want the loop to perform is a series of checks. Each of these checks is an if statement, such as:
This specific statement reads: if (the remainder of variable i divided by 3 is-equal-to 0) perform the below function.
So we check to see if the number represented by variable i is divisible by 3, and if so we print "Fizz" (without continuing to the next line in the console). We perform a similar check to see if the number is divisible by 5, and if so we print "Buzz".
If neither of the above is true, we simply print the number.
Once we have finished all of the above checks, we use "Console.WriteLine();" to continue to the next line.
Each time the function is completed, the number represented by variable by i is incremented and we start the function over again checking the next number. Once we finish checking the number 50, the loop is completed and we carry on.
Since we want to see the results of the checks, we use "Console.ReadKey();" to hold the program from completing until we press any key.
The results should be:
A shorter solution I came up with uses a series of 3 tertiary statements. Tertiary statements are formatted as so:
if statement ? true function : false function
Or can be read as: if this ? do this : or else do this.
Here is the solution:
Within the loop, our checks can be read as:
if the remainder of i divided by 15 is-equal-to 0 ? Print "Fizz Buzz" : or else if the remainder of i divided by 3 is-equal-to 0 ? Print "Fizz" : or else if the remainder of i is divisible by 5 ? Print "Buzz" : or else print the number (converted to a string of text).
Without the comments and formatting for the sake of demonstration, the actual solution can be written as:
And that is how we can check the a range of numbers for specific multiples using C#.