PREP Method 101!

Schamir Poliard
Strategio
Published in
4 min readFeb 6, 2023

Hello friends! 👋🏽

Today, we will be solving a code challenge from CodeWars that involves finding the sum of all multiples of 3 or 5 below a given number. CodeWars is an alternative to LeetCode and is a great resource to learn the fundamentals of coding concepts and sharpen your problem-solving skills and become the Bruce Lee of coding katas.

Here is the challenge prompt below:

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.

Finish the solution so that it returns the sum of all the multiples of
3 or 5 below the number passed in. Additionally, if the number is negative,
return 0 (for languages that do have them).

This is where the PREP method comes in. Before I write any code I like to first write my Pseudocode using this method. What is PREP?

It stands for:

  1. Parameters — What type of argument(s) is inputted, are there multiple arguments?
  2. Return — What is being returned and in what type?
  3. Examples — Use 1–2 examples to see what results I am expecting the program to return. You can copy this from the prompt itself, or create your own.
  4. Pseudocode — Write the step-by-step on how to solve the code challenge.

So for this coding challenge, I write my PREP in comment form like this:

/* Parameters: in this problem our parameter is an int

Return: in this challenge are going to be returning the
sum of the multiples of 3 or 5
Also if number is negative, return 0

Examples: all whole numbers < 10 --> 3 + 5 + 6 + 9 = 23

*/
public class Solution {

public int solution(int number) {

Pseudcode: write steps within code

// 1. Initialize a variable 'sum' to store the sum of all multiples of 3 or 5.

// 2. Use a for loop to iterate over the numbers from 0 to 'number - 1'

// 3. Check if the current number is divisible by 3 or 5

// 4. If it is, add the number to 'sum'

// 5. Return the final sum after all iterations of the for loop are complete



}
}

The PREP method is a great practice to utilize before hardcoding your answer because it helps you to take a step back and make sure that you’ve properly understood the coding prompt. It’s also a good way to practice your communication skills during a technical interview as well.

The next step is to come up with a plan for how to solve this challenge. For this solution, we will be using a for loop. Here’s how it works:

First, we will initialize a variable sum to 0. This variable will be used to store the sum of all the multiples of 3 or 5.

public class Solution {
public int solution(int number) {
int sum = 0;

}
}

Next, we will use a for loop to iterate over the numbers from 0 to the input number. For each iteration, we will check if the current number is divisible by 3 or 5.

public class Solution {
public int solution(int number) {
for (int i = 0; i < number; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}

}

To check if a number is divisible by 3 or 5, we will use the modulo operator %. The modulo operator returns the remainder of a division operation. If the remainder of a division by 3 or 5 is 0, then the number is divisible by 3 or 5.

If the current number is divisible by 3 or 5, it adds the number to the sum.

It will repeat the above steps for each iteration of the for loop till the condition is satisfied.

After all iterations of the for loop are complete, return the sum.

Here is the complete solution:

public class Solution {
public int solution(int number) {
int sum = 0; // Initialize a variable 'sum' to store the sum of all multiples of 3 or 5.
for (int i = 0; i < number; i++) { // Use a for loop to iterate over the numbers from 0 to 'number - 1'
if (i % 3 == 0 || i % 5 == 0) { // Check if the current number is divisible by 3 or 5
sum += i; // If it is, add the number to 'sum'
}
}
return sum; // Return the final sum after all iterations of the for loop are complete
}
}

Congrats! You have successfully solved the code challenge using a for loop. Bruce Lee would be so proud!

Be sure to practice the PREP method and you will continue to improve your skills and knowledge of data structures and algorithms!

Till next time! 😎✌🏽

Follow me on Twitter here!

--

--