data codes through eyeglasses
Programming Python

AMAZING CODING CHALLENGE IN PYTHON TO TRY TODAY (2021)

I started my journey with Python a couple of months ago and here I am – ready for my first coding challenge.

WHAT IS PROJECT EULER?

Today, we are exploring a Project Euler – Amazing Coding Challenge which allows us to increase our ability to solve math problems using the most popular programming language: Python. We will try to solve problems, at our own pace.

 Every new post connected to this challenge will contain only one, my own solution based on mathematical analysis and coding difficulties in Python language.  So, do not feel intimidating if you do this slow than me, or be patient for me if I will be struggling with this and you can do much quicker and better than me. I will do my best, I promise.

 For those, who are don’t know what Project Euler exactly is, it is a set of problems that every new dev (my personal belief), should solve using your own programming language. For more information, I encourage you to visit the main site of the project: Project Euler

 I am totally a novice in the programming world, so forgive me any mistake I will be made, but this is the only way to make huge progress in mastering a new skill. Also, if you note any mistake of typo, grammar error or bugs in my own code, please free to comment down below.

So, let’s dive right in.

DEFINE PROBLEM 1

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.

projecteuler.net

How to find an own solution?

Everyone knows the natural numbers which are one of the many ways to describe our beautiful world. We came across them in the very early stage of life when we want to count something in our life. Let’s begin amazing coding challenge by defining natural numbers in terms of sets numbers:

N={ 0, 1,2, 3, …}

N+= {1,2,3,…}

And defining integers numbers, set of natural numbers and negative natural numbers:

Z={…,-3,-2,-1,0,1,2,3,…}

When, we can define what a natural number is, the next thing will be to think what exactly means to be multiplied by one number which will be an integer. We say that one integer n divide integer m, mark as n|m if exists any k integer such that: m=n*k

So, if we consider all numbers from problems that are below 10, and those which are integers, but positive natural numbers. We can write down them all : 1,2,3,4,5,6,7,8,9

Then, we are looking for a number that is multiplied by 3 or 5, so we can consider all form of numbers like 3*k or 5*k, for any k (it need to be a positive integer because our number are positive). We can, of course, substitute every our number, like:

1=3*k or 1=5*k,  so k = {1/3, 1/5}

2=3*k or 2=5*k, so k= {2/3,2/5}

3=3*k or 3=5*k, so k={1,3/5}

4=3*k or 4=5*k, so k={4/3,4/5}

5=3*k or 5=5*k, so k={5/3, 1}

6=3*k or 6=5*k, so k={2,6/5}

7=3*k or 7=5*k, so k={7/3, 7/5}

8=3*k or 8=5*k, so k={8/3,8/5}

9=3*k or 9=5*k, so k={3,9/5}

As we can see, the only numbers in greenfield are correct: 3,5,6,9, because all have a positive integer like k, for the rest k doesn’t exist.

When we add them all, we have:

3+5+6+9= 23

Now, we know how exactly to solve this problem for a few numbers below a low number, but, of course, in the task, we have a much more advanced problem and we need to determine some specifications to see what exactly the program will be doing. We need to decide what exactly we want to accomplish, some of our inputs and outputs will be extremely important.

Firstly, for input, we need all natural numbers below 1000. Then we need to check whether they are divisible by 3 or 5 in the process.  And for output, give the user back certain numbers which are correct and satisfy the term.

How to find all-natural numbers below 1000?

We can write all the numbers below 1000 as list comprehension, which allow us to write down all numbers from 0 to 999:

number=[i for i in range(1000)]

natural numbers below 1000

Then we need to add variables to keep our all correct numbers inside:

num=[]

It will be of course an empty list. You can call it as you like. I call it num. Then using for loop we will get all natural numbers from 1 to 999 which are divisible by 3 or 5. In python, we use operation modulo with remainder 0 to check out if statement. If it is right, we append our number to list num:

for i in number[1:]:

    if i%3==0 or i%5==0:

        num.append(i)

And print all results on the screen using the built-in sum function.

print(sum(num))   

sum fibbonacci numbers
coding-challenge-to-try-today

Yeah, we did it! The first problem is complete. Amazing Coding Challenge time to begin. See you in the next post soon. Don’t hesitate to comment below if you have your own better solution or see any bug here.

Leave a Reply