Learning Python with examples from Project Euler - Multiples of 3 and 5, and Even Fibonacci Numbers

in #programming9 years ago

A lot of people that I know are really interested in learning programming, but just aren't sure how to start. Python is a great user friendly language to help learn the basics, and Project Euler provides an excellent way to hone your skills with math based problems. For example, 

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.

For those of you following along at home, I highly recommend Notepad++

Also make sure you have Python

To solve this problem, theoretically, we're going to divide every number from 3 to 1000 by 3 and 5, if EITHER gives a remainder of 0, we're going to add the number to a running total. Here's how to write that in python:

The # makes it a comment that doesn't affect the output of the code. In the next line, we initialize the variable for our running total called, well, total. If we wanted to call it sum, it would simply be sum = 0, no data types. In other languages variables are often initialized as int n= 0 or string str="Hello" (strings are words) but in Python, by initializing it as str="String", it recognizes it as string and treats it accordingly. Moving on, we have a FOR loop. The variable x is initialized in the loop itself, we don't have to initialize x outside of the loop unless we want to. What the loop does for x in range(i,j,k), is it will take x, at starting position i, run it through the code (indentation perpetrates order in python, see above code as example) increase it by  k if we so desired, until it reached j. In this example, we use the modulo operator % which divides the number and returns the remainder. So in if x % 3 == 0 or x  % 5 == 0: the current value of x is taken and divided by 3 and 5, and if it is evenly divided by either, we add the value of x to our running total, total = x + total, which can also be written as total =+ x for simplicity. The operator or means that only one of the conditions has to be true to add x to the total.


Next, we're going to find the sum of all the even numbers in the Fibonacci Sequence.

 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. 

Once again, we're first going to break it down into a conceptual solution. First we need to generate the Fibonacci Sequence in our code, c is going to be the particular term that we divide by 2 to see if it qualifies for the running total.

to run the code, save as .py, navigate the the directory in cmd (win+r, type cmd, enter) if you saved it to your desktop, type in cd desktop (cd being change directory, going from the starting directory of C:\Users\<You>\ to C:\Users\<You>\Desktop) from here, type in <filename>.py to run the code, filename being whichever name you gave it.


Come back tomorrow to find the largest prime factor of 148912734!

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.035
BTC 111360.81
ETH 4309.79
SBD 0.84