2017-06-26

Stock Screener Update 1

The current status of the program is that it can make a valuation of a company based on the data sheet it's fed with. It doesn't have input for growth yet and it only outputs money making potential in compounded annual return. It's time for an update of the ideas-of-improvements list.

Ideas of improvement

As I was writing the above three steps, an explosion of ideas took place in my head. Although some of the ideas were existing prior to this post, many new came to mind. When I came up with the idea of the program, I wanted to make it come with a GUI, but due to complexity I'll start without GUI.

  • Time horizon is set by a date instead of a number of years.
  • Output can be saved as csv.
  • Output can be saved to a database.
  • Calculate future value based on return on equity.
  • Allow user to enter a stock ticker and retrieve data automatically (API or scraping).
  • Growth rate calculated on historical data instead of arbitrary.
  • Make a GUI for the program.
  • Calculation with growth rate.
  • Checking company's financial health from data sheet.

 Some thoughts

I find it challenging to get a picture of which methods should be a part of which class. As I've understood it, it's good practise to divide the code into methods can classes depending on how many times they are supposed to be used and also if they are program specific or if they can be used in other programs aswell. The code can be rearranged in almost indefinite different ways, but there should be some ways that are better than other. I hope to learn this as my Python experience grows.

At the moment, the data sheet is scraped into a list, but maybe I should use a tuple instead? The data sheet info shouldn't be modified in the program.

/Ludvig

2017-06-25

Stock Screener Programming 2

The program now calculates three year average net income and the lowest historical price/book key figure and saves it in a Company object.

main.py


def open_datasheet(file=""):
    file = open(file, "r", encoding="utf-8")
    lines = [[text for text in line.split()] for line in file]
    return lines


def get_earnings(parsedList=""):
    """This method returns the last three year average net income"""
    for line in range(0, len(parsedData)):
        for item in range(0, len(parsedData[line])):
            if "Net" in parsedData[line][item]:
                if "Income" in parsedData[line][1]:
                    if "(Mil)" in parsedData[line][2]:
                        average = (
                                    int(parsedData[line][3].replace(",", "")) +
                                    int(parsedData[line][4].replace(",", "")) +
                                    int(parsedData[line][5].replace(",", ""))
                                  ) / 3
    return int(average)


def get_lowest_pb(parsedList=""):
    """This method returns the lowest historical Price/Book value"""    pb_list = []

    for line in range(0, len(parsedData)):
        for item in range(0, len(parsedData[line])):
            #if "www.nasdaqomxnordic.com/" in parsedData[line][item]:            if "Price/Book" in parsedData[line][item]:
                for x in range(item + 1, item + 6):
                    try:
                        pb_list.append(float(parsedData[line][x]))
                    except:
                        continue
    min_pb = min(pb_list)

    return min_pb


def create_company():
    """This function creates a Company object"""    import company
    new_company = company.Company(companyName=filename,                                  avg_earnings=avg_net_earnings,                                  lowest_pb=lowest_pb,                                  datasheet=open_datasheet(filename))
    return new_company

filename = input("Text file name:")
parsedData = open_datasheet(file=filename)
avg_net_earnings = get_earnings(parsedList=parsedData)
lowest_pb = get_lowest_pb(parsedList=parsedData)

current_company = create_company()

print(current_company.company_name, current_company.avg_earnings, current_company.lowest_pb)


company.py


class Company:
    """The Company() object hold information of a company"""
    def __init__(self, companyName="NoName", avg_earnings = 0, lowest_pb = 0, datasheet=""):
        self._companyName = companyName.title()[:-4]
        self._avg_earnings = avg_earnings
        self._lowest_pb = lowest_pb
        self._datasheet = datasheet

    @property    def avg_earnings(self):
        return self._avg_earnings

    @property    def company_name(self):
        return self._companyName

    @property    def lowest_pb(self):
        return self._lowest_pb

    @property    def datasheet(self):
        return self._datasheet

    if __name__ == "__main__":
        print("Company class is run as main!")


Stock Screener Programming 1

There are a lot of things to do, and since I'm going to have a iterated development approach in this project, my aim for every coding session is to have a runable code which is improved from last session.

For PDF reading I'm using the module PyPDF2. When I try to open the PDF with PyPDF2 I get just nonsense output so I'm going to give pdfminer3k a shot. I found a very new documentation on pdfminer3k HERE. After fiddling around with pdfminer3k also that library was ratified. Next library up for shaving is one called textract. This time the problem was already in the installation of the package. Slate3k is another one to try and it atleast installs successfully. Wow this problem doesn't seem to be an easy to solve. The best solution I find that I don't feel like doing is to install Python 2 and redirect all pdfminer commands to there.

What I will do for now is to save the pdf as a text file manually and read the text file instead. If I come up with a way to scrape PDF files later on, I'll implement it then.



def openDatasheet(file=""):
    file = open(file, "r", encoding="utf-8")
    lines = [[text for text in line.split()] for line in file]
    print("Number of lines read: ", len(lines))
    return lines


def getEarnings(parsedList=""):
    """This method returns the last three year average net income"""
    for line in range(0, len(parsedData)):
        for item in range(0, len(parsedData[line])):
            if "Net" in parsedData[line][item]:
                if "Income" in parsedData[line][1]:
                    if "(Mil)" in parsedData[line][2]:
                        average = (
                                    int(parsedData[line][3].replace(",", "")) +
                                    int(parsedData[line][4].replace(",", "")) +
                                    int(parsedData[line][5].replace(",", ""))
                                  ) / 3

    return int(average)

filename = "pandora.txt" #input("Text file name:")parsedData = openDatasheet(file=filename)
avg_net_earnings = getEarnings(parsedList=parsedData)

print("Average net earnings of", filename, avg_net_earnings)

This program takes a textfile (I've used the data sheet for the Danish jewellery company Pandora), reads it and prints out the average net earnings. Next step should be to create an object Company() that the read data can be saved to.

/Ludvig

2017-06-24

Stock Screener from PDF Data Sheet

I want to build a program that can read data from a PDF file and make some calculations on the data which it will print out. This is to speed up the quantitative analysis made on companies which stocks are traded publicly. The program won't produce pages of processed information to be used as a complete quantitative analysis, but as an advanced stock screener I hope it'll do the trick. The aim is to solve the problem of manual data harvesting for stock screening.

In a book am currently reading, "Lindblad, Erik. 2006. Programmering i Python" (about Python 2, but good enough for a beginner at my level), the author writes that just as in every other scientific area in which I've been in touch, planning the work is just as important as executing it. The author writes about three "steps of OOP".
  1. Object oriented analysis
  2. Object oriented design
  3. Programming

1. Object Oriented Analysis

This step is about identifying the objects which will be used, which attributes they will have and which methods they need. By describing what the program is supposed to do in writted text, the nouns in can be used as objects and the verbs as methods in the code.

"The user uses the program to open a locally saved PDF company data sheet file from which all data is read. Compounded future earnings on an arbitrary time horizon are calculated with an arbitrary growth rate. The compounded earnings are then added to the company's equity so that a figure of what the equity (excluding shareholder paybacks such as dividends and share buy-backs) will be in the given time. By multiplying the future equity by the lowest historical price/book valuation, a future valuation is calculated. The current valuation is calculated and compared to the current price of the stock. The program will tell the user what is the up/down side in the company both in percent and in money."

Objects (nouns)
  • Company data sheet
  • Current company
    • Compounded future earnings
    • Current equity
    • Compounded future equity
    • Lowest historical P/B
    • Future valuation of compounded equity
  • Arbitrary time horizon
  • Arbitrary growth rate
  • Current stock price
Methods  (verbs)
  • Open a PDF and read data.
  • Read time horizon from user input.
  • Read growth rate from user input.
  • Read current stock price from user input.
  • Read lowest P/B from data sheet.
  • Read last three years net earnings from data sheet. 
  • Read equity from data sheet.
  • Calculate compounded future earnings.
  • Calculate future valuation of compounded equity.
  • Calculate current valuation of future compounded valuation.
  • Calculate up/down side.
This definately grew larger than expected. I'm thinking that "Current company" will be a superclass and the indented points below it will be subclasses of it. The relationship between the superclass and the subclasses will be a strong "has-a" composition relationship... I'll postpone the UML modelling for later.

2. Object Oriented Design

The design step is intended to concretize the model made in the analysis step by setting attribute names and types. Also methods are named and arguments and return types are decided. The relationships between objects are described in detail (maybe UML is good help?). I'm going to make a kind of flow description to visualize the process.
  1. User opens PDF file in program. datasheet = openDatasheet()
  2. A module for PDF reading is imported (PyPDF2).
  3. A method for parsing the PDF data is run. parsePdf(datasheet)
    1. Return last three years net earnings as a list
    2. Return lowest P/B as an integer
    3. Return equity as an integer
  4.  Time horizon input is read. time
  5. Growth rate input is read. growth
  6. Current stock price input is read. currentStockPrice 
  7. An instance of Company() is created. Company() has five arguments.
    1. earningsList=""
    2. pb=""
    3. equity=""
    4. time="" 
    5. growth=""
  8. Calculator(Company()) is a class that takes a Company object and currentStockPrice and returns two values.
    1. Return upDownSidePercent as float
    2. Return upDownSideCash as float
Now that I'm doing all this planning, my mind brings me back to what I've heard and read about iterated development (XP, scrum etc) and that planning is good, but the result probably won't be anything like what was thought from the beginning.

3. Programming

I'm going to write this program in Python 3.6 and I'm going to try to have an iterated process in which I will have a working prototype as often as possible.

Ideas of improvement 

As I was writing the above three steps, an explosion of ideas took place in my head. Although some of the ideas were existing prior to this post, many new came to mind. When I came up with the idea of the program, I wanted to make it come with a GUI, but due to complexity I'll start without GUI.

  • Time horizon is set by a date instead of a number of years.
  • Output can be saved as csv.
  • Output can be saved to a database.
  • Calculate future value based on return on equity.
  • Allow user to enter a stock ticker and retrieve data automatically (API or scraping).
  • Growth rate calculated on historical data instead of arbitrary.
  • Make a GUI for the program.
/Ludvig

2017-06-22

Project Euler Problem 67

Since problem 67 was just a "harder" version of problem 18, I figured I'd try my problem 18 solution on it, and voila, it worked!


with open('pe67.txt', 'r') as file:
    numbers = [[int(num) for num in line.split()] for line in file]

numbersRev = reversed(numbers)


for line in numbersRev:
    for item in range(0, len(line)):
        if item < len(line) - 1:
            a = line[item]
            b = line[item + 1]
            c = max(a, b)
            numbers[numbers.index(line) - 1][item] += c

print(numbers[0][0])

2017-06-21

Project Euler Problem 18

with open('pe18.txt', 'r') as file:
    numbers = [[int(num) for num in line.split()] for line in file]

numbersRev = reversed(numbers)


for line in numbersRev:
    for item in range(0, len(line)):
        if item < len(line) - 1:
            a = line[item]
            b = line[item + 1]
            c = max(a, b)
            numbers[numbers.index(line) - 1][item] += c

print(numbers[0][0])

2017-06-19

Project Euler Problem 17

For this problem I want to use a dictionary

This problem took me way too long time to solve. All because of a mistake I made caused by naming my dictionaries too similarly. When I thought I was adding "hundred_and_text", I really was adding "hundreds_text". What made it really annoying is that I made my code output all the thousand numbers in words, which worked great. For troubleshooting I copied the output into excel and counted the letters in the cells which gave the correct answer, but when I used my code for counting, I missed ~300 letters. Next time I'm planning on doing something like this, I'm going to give dictionaries more distinguishable names.


In this program I also worked in multiple files, i.e. I had my dictionaries in a file called "dicts.py" and my main program in "main.py" (mostly because I could).

Dictionary code:

singulars_text = {  # 36    1: 'one',    2: 'two',    3: 'three',    4: 'four',    5: 'five',    6: 'six',    7: 'seven',    8: 'eight',    9: 'nine',}

ten_to_nineteen_text = {  # 70    1: 'ten',    2: 'eleven',    3: 'twelve',    4: 'thirteen',    5: 'fourteen',    6: 'fifteen',    7: 'sixteen',    8: 'seventeen',    9: 'eighteen',    10: 'nineteen'}

tens_text = {  # 46    2: 'twenty',    3: 'thirty',    4: 'forty',    5: 'fifty',    6: 'sixty',    7: 'seventy',    8: 'eighty',    9: 'ninety'}

hundreds_text = {  # 107    1: 'onehundred',  # onehundred    2: 'twohundred',  # twohundred    3: 'threehundred',  # threehundred    4: 'fourhundred',    5: 'fivehundred',    6: 'sixhundred',    7: 'sevenhundred',    8: 'eighthundred',  # eighthundred    9: 'ninehundred',  # ninehundred}

hundred_and_text = {  # 126    1: 'onehundredand',  # onehundredand    2: 'twohundredand',  # twohundredand    3: 'threehundredand',  # threehundredand    4: 'fourhundredand',  # fourhundredand    5: 'fivehundredand',    6: 'sixhundredand',    7: 'sevenhundredand',    8: 'eighthundredand',    9: 'ninehundredand',}

Program code:

import dicts


def number_counter_text():

    tot = 0
    for k, v in dicts.singulars_text.items():
        # print(dicts.singulars_text[k])        tot += len(dicts.singulars_text[k])

    for k, v in dicts.ten_to_nineteen_text.items():
        # print(dicts.ten_to_nineteen_text[k])        tot += len(dicts.ten_to_nineteen_text[k])

    for k, v in dicts.tens_text.items():
        # print(dicts.tens_text[k])        tot += len(dicts.tens_text[k])
        for a, b in dicts.singulars_text.items():
            # print(dicts.tens_text[k], end="")            # print(dicts.singulars_text[a])            tot += len(dicts.tens_text[k]) + len(dicts.singulars_text[a])

    for k, v in dicts.hundred_and_text.items():
        # print(dicts.hundreds_text[k])        tot += len(dicts.hundreds_text[k])
        for x, y in dicts.hundreds_text.items():
            # print(dicts.hundred_and_text[k], end="")            # print(dicts.singulars_text[x])            tot += len(dicts.hundred_and_text[k]) + len(dicts.singulars_text[x])
        for a, b in dicts.ten_to_nineteen_text.items():
            # print(dicts.hundred_and_text[k], end="")            # print(dicts.ten_to_nineteen_text[a])            tot += len(dicts.hundred_and_text[k]) + len(dicts.ten_to_nineteen_text[a])
        for c, d in dicts.tens_text.items():
            # print(dicts.hundred_and_text[k], end="")            # print(dicts.tens_text[c])            tot += len(dicts.hundred_and_text[k]) + len(dicts.tens_text[c])
            for e, f in dicts.singulars_text.items():
                # print(dicts.hundred_and_text[k], end="")                # print(dicts.tens_text[c], end="")                # print(dicts.singulars_text[e])                tot += len(dicts.hundred_and_text[k]) + len(dicts.tens_text[c]) + len(dicts.singulars_text[e])
    # print("onethousand")    tot += len("onethousand")

    print("----- result -----")
    print(tot)

    pass
number_counter_text()

/Ludvig

2017-06-18

Project Euler Problem 16

Easiest to far. I take the number, make it into a string, during for-loop through the characters in the string, I make them into integers and add them to tot.








number = 2 ** 1000tot = 0
for x in range(0, len(str(number))):
    digit = str(number)[x:x + 1]
    tot += int(digit)

print("tot:", tot)


A slimmer version of the same code:


tot = 0
for x in range(0, len(str(2 ** 1000))): tot += int(str(2 ** 1000)[x:x + 1])

print("tot:", tot)


/Ludvig

2017-06-17

Project Euler Problem 15

I think this problem is about combinatorics, i.e. finding out in how many ways a series of discrete values can be combined. For this problem I'm going to use factorials, mathematically expressed as "!".

2x2 has 3! combinations: 3x2x1=6
3x3 has 4! combinations: 4x3x2x1=24
4x4 has 5! combinations: 5x4x3x2x1=120

etc.

What I want to do is to write a code that calculated factorials and tells me what 21! is.


def calculate_factorial(val):
    result = 1    for x in range(1, val + 1):
        result *= x
    return result

print(calculate_factorial(21))
  
Outputs: 51090942171709440000 which is WRONG. I'm guessing that it's because this way of doing it includes duplicates of routes. Another approach is to write all routes as directions. I chose R for right and D for down. On a 2x2 grid, going from upper left corner to lower right corner, it takes four directions: RRDD. As long as these four directions are included in the route, it will make the route complete. So I want to make a program that checks all the combinations of the directions and removes all duplicates. When I google for help on these problems I try to avoid websites that addresses the very problems that I'm working on (there's a lot of help to get on Project Euler problems already).

I found a video about this type of problem and I was on the way to finding the solution, what I missed though was that I need to divide the factorial with the number of directions that I'll take (20xR and 20xD). So I get: c(40,(20,20)) which is 40!/(20!20!). Voila, I got it to work. This code works for any grid sized ROW*COLUMN.








def factorial(val):
    result = 1    for x in range(1, val + 1):
        result *= x
    return result


def routes(down, right):
    result = factorial(down + right) / (factorial(down) * factorial(right))
    return int(result)

print("Routes: ", routes(20, 20))

To make this problem a little bit more interesting I'm going to let the user input values in the console instead of having it hard coded. By the way, when googling around, I found an interesting quote:

"The classical Python mentality, though, is that it's easier to ask forgiveness than permission. In other words, don't check whether x is an integer; assume that it is and catch the exception results if it isn't:
try:
    x += 1
except TypeError:
    ..."


So I'm also going to give try-except a shot.


def factorial(val):
    result = 1
    for x in range(1, val + 1):
        result *= x
    return result


def routes(down, right):
    result = factorial(down + right) / (factorial(down) * factorial(right))
    return int(result)

rowInput = input("Rows:")
colInput = input("Columns:")
try:
    print("Routes: ", routes(int(rowInput), int(colInput)))
except:
    print("Invalid input")

Aaaaand it works!

/Ludvig

2017-06-16

Project Euler Problem 14



I first ran a code that went through all the 1M starting values, and when it was finished, it printed the chain length instead of what starting number the chain had -.-. When I modified the code to solve the problem I made it stop at that chain length instead of continuing searching.

chain = []
newChain = []
val = 0

def odd_val(val):
    odd = 3 * val + 1    return int(odd)


def even_val(val):
    even = val / 2    return int(even)


for value in range(10 ** 6, 1, -1):
    print(value, len(chain))
    while value != 1:
        if value % 2 == 0:
            value = even_val(value)
            newChain.append(value)
        else:
            value = odd_val(value)
            newChain.append(value)
    if len(newChain) > len(chain):
        chain = newChain
        print("New chain!")
    if len(chain) == 524:
        break    newChain = []

print(len(chain), val)

Hmmm now that I look through the print out there seems to be something weird in the code. I'm going to make it look nicer before heading on to the next problem.

I ran out of energy for fixing it. Hopefully I'll turn back sometime in the future.


/Ludvig

2017-06-15

Project Euler Problem 13


tot = 0
with open('pe13.txt') as file:
    lines = file.readlines()

lines = [x.strip() for x in lines]

for x in range(0, len(lines)):
    line = lines[x]
    tot += int(line)


tot = str(tot)
tot = tot[:10]
print(tot)

Above is what I first did to solve the problem. Below is how I refined my solution:

tot = 0lines = []

with open('pe13.txt') as file:
    lines = file.readlines()

lines = [x.strip() for x in lines]

lines = list(map(int, lines))

tot = str(sum(lines))
tot = tot[:10]
print(tot)

and even more:

with open('pe13.txt') as file:
    lines = file.readlines()

lines = [x.strip() for x in lines]
lines = list(map(int, lines))

print(str(sum(lines))[:10])

Quite compact if I may say so.

/Ludvig

2017-06-14

Project Euler Problem 12

Finding divisors of summed natural numbers. This time I'm creating a function for doing the divisor checking. Just as in atleast one previous problem, this is a problem which can be solved with a mathematical sieve. Since my math skills obviously aren't good enough I took help from google and found a way to, by using the square root of the number to be checked, calculate the number of divisors of a triangle number. As so often in math, when you have the solution it's all so obvious.






 My math skills and patience weren't as good as they should have been for this problem. Programatically it worked out though!



number = 0

def number_of_divisors(val):
    divisors = 0    for y in range(1, int(val ** 0.5)):
        if val % y == 0:
            divisors += 2
    return divisors


for x in range(0, 10 ** 10):
    if number_of_divisors(number) > 500:
        print(number)
        break    number += x


 /Ludvig

2017-06-13

Project Euler Problem 11

First let's format the grid to fit in a list. I do this by copying it into Excel and replace all " " with "," and then copy it into a list. Since the position in the grid matters, actually I might need a two dimensional list. How do they work? Stack overflow helps out.

Now that I've though about it, there must be a better way of reading data into a Python list. And there is! I copy the data into a textfile and then read the textfile.


lines = []

with open('pe11.txt') as file:
    for line in file:
        lines = file.read().splitlines()

This is not enough though, I want a two dimensional matrix. I need to know how to add space separated values from a textfile to a Python list.







with open('pe11.txt', 'r') as file:
    lines = [[int(num) for num in line.split()] for line in file]

product = 1rowProduct = 1colProduct = 1diaDownProduct = 1diaUpProduct = 1
for row in range(0, 20):
    for col in range(0, 17):
        newProduct = lines[row][col] * lines[row][col + 1] * lines[row][col + 2] * lines[row][col + 3]
        if newProduct > rowProduct:
            rowProduct = newProduct
            rowResult = [lines[row][col], lines[row][col + 1], lines[row][col + 2], lines[row][col + 3], 'row:', row, 'col', col, 'row product', rowProduct]

for col in range(0, 20):
    for row in range(0, 17):
        newProduct = lines[row][col] * lines[row + 1][col] * lines[row + 2][col] * lines[row + 3][col]
        if newProduct > colProduct:
            colProduct = newProduct
            colResult = [lines[row][col], lines[row + 1][col], lines[row + 2][col], lines[row + 3][col], 'row:', row, 'col', col, 'col product', colProduct]

for row in range(0, 17):
    for col in range(0, 17):
        newProduct = lines[row][col] * lines[row + 1][col + 1] * lines[row + 2][col + 2] * lines[row + 3][col + 3]
        if newProduct > diaDownProduct:
            diaDownProduct = newProduct
            diaDownResult = [lines[row][col], lines[row + 1][col + 1], lines[row + 2][col + 2], lines[row + 3][col + 3], 'row:', row, 'col', col, 'dia down product', diaDownProduct]

for row in range(0, 17):
    for col in range(0, 17):
        newProduct = lines[row + 3][col] * lines[row + 2][col + 1] * lines[row + 1][col + 2] * lines[row][col + 3]
        if newProduct > diaUpProduct:
            diaUpProduct = newProduct
            diaUpResult = [lines[row + 3][col], lines[row + 2][col + 1], lines[row + 1][col + 2], lines[row][col + 3], 'row:', row, 'col', col, 'dia up product', diaUpProduct]

print(rowResult, "\n", colResult, "\n", diaDownResult, "\n", diaUpResult)

resultList = [rowProduct, colProduct, diaDownProduct, diaUpProduct]
print(max(resultList))


The codes are growing! It feels like this problem should be solvable in a more elegant way both mathematically and programatically. I'll go with this solution for now and maybe turn back in the future to try a new way (if I learn one).

/Ludvig

2017-06-12

Project Euler Problem 10

For this problem I'm going to try to reuse the code I wrote for problem 7. Problem 7 was about finding the 10001th prime number, so this is more or less a prime number calculator and should be usable for problem 10.

t = 0listOfPrimes = [2]
for numerator in range(3, 200000, 2):
    if numerator % 2 != 0:
        for x in range(0, len(listOfPrimes)):
            if numerator % listOfPrimes[x] == 0:
                t += 1        if t == 0:
            listOfPrimes.append(numerator)
        else:
            t = 0    print(len(listOfPrimes), numerator)
    if len(listOfPrimes) == 10001:
        break
print("10001th prime: ", listOfPrimes[10000])

My intuition says that this problem will take quite some running time to complete, let's see!

I've done some modifications of the code above to make it work theoretically. What annoys me is that it takes so long time to run the algorithm, so I think I'm gonna see how other people solved problem 7 to maybe get some inspiration for a better way. This takes way too long, so I'm going to program the sieve of Eratosthenes algorithm for prime number calculation. On the Euler Project forum, the people who use this algorithm solves problem 7 in (milli)seconds.



It annoys me only a little, but what I did to solve this problem was to find a fast solution for problem 7 from the Project Euler forum and then modify it. The code I chose was posted by onoki_li in May 2017. I then modified it to add all found primes to a list until the next prime was larger than 2,000,000. What bothers me is that I didn't come up with the code myself, but I rationalize it with that this is for Python and not math learning.



from math import sqrt

def is_prime(x):
    for j in range(2, int(sqrt(x)) + 1):
        if x % j == 0:
            return False            break    return True
i = 0m = 1listOfPrimes = []


while m < 2000001:
    m += 1    if is_prime(m):
        listOfPrimes.append(m)

print(sum(listOfPrimes))

/Ludvig

2017-06-11

Project Euler Problem 9

Hola!

Another day, another problem. What's new in this solution is the built-in function is_integer() which I use to check if a float number has no decimal value. For example when c = 5.0, c.is_integer() returns true and when c = 8.3, c.is_integer() returns false. It was a good help since most square roots aren't integers.

product = 1c = 0
for a in range(1, 1000):
    for b in range(1, 1000):
        c2 = a ** 2 + b ** 2        c = c2 ** 0.5        abcSum = a + b + c
        if c.is_integer() and abcSum == 1000:
            c = int(c)
            print("a: ", a, "b: ", b, "c:", c, "sum", abcSum)
            product = a * b * c
            break    if abcSum == 1000:
        break
print("product:", product)



/Ludvig

2017-06-10

Project Euler Problem 8

In the last post I wrote about finding a feature for measuring the run time for an algorithm. It seems as though I either need to install plugins for PyCharm or I need to upgrade to a commercial version of it. I'll leave it for now.

Now it's time for problem 8:


What I'm going to do is to cut out 13 digits from the number, multiply them, save the product, and then doing the same thing after shifting the 13 digits one to the right. Instead of saving every product, I'll compare the new one and the current one and only save the larger one.


From now on, I'll stop posting the problems here on the blog and let you read the problem yourself on ProjectEuler.net instead.

The code I used for this problem is:

fullNumber = '731671765313306249192....' # insert numbers hereproduct = 1newProduct = 1
for x in range(0, 987):
    for y in range(x, x+13):
        newProduct *= int(fullNumber[y])
    if newProduct > product:
        product = newProduct
    newProduct = 1
print("product: ", product)

2017-06-09

Project Euler Problem 7

The problem:

"By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?"

Time to make a for loop that checks if a number is a prime or not.With this code, I seem to be able to calculate prime numbers in a correct way. The problem is that it is incredibly slow.

t = 0listOfPrimes = [2]
print("length", len(listOfPrimes))
for numerator in range(3, 200000, 2):
    for x in range(2, numerator):
        if numerator % x != 0:
            for y in range(0, len(listOfPrimes)):
                if numerator % listOfPrimes[y] == 0:
                    t += 1            if t == 0:
                listOfPrimes.append(numerator)
                break            else:
                t = 0    print(len(listOfPrimes))
    if len(listOfPrimes) == 10001:
        break

print("prime no 10001: ", listOfPrimes[10000])

I'm going to rearrange is order of the for-loops to increase the speed of calculation. This didn't make it faster... Now I'm going to try to see if I can limit the modulus checks even more. I've read something somewhere about prime numbers are not dividable by 2 (except the prime number 2) or by any prime numbers that are smaller than the square of the number the is being checked. Wow, I've made a huge miss. I though 10001 was one hundred thousand and one instead of ten thousand and one. Well well, here is my code for finding the 10001th prime:

t = 0listOfPrimes = [2]
for numerator in range(3, 200000, 2):
    if numerator % 2 != 0:
        for x in range(0, len(listOfPrimes)):
            if numerator % listOfPrimes[x] == 0:
                t += 1        if t == 0:
            listOfPrimes.append(numerator)
        else:
            t = 0    print(len(listOfPrimes), numerator)
    if len(listOfPrimes) == 10001:
        break
print("10001th prime: ", listOfPrimes[10000])

I'd like to have a feature in PyCharm that shows the time it took to run an algorithm.

/Ludvig

2017-06-08

Project Euler Problem 6

The problem:

"The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum."

To do a square in Python, two * is used: "**". This time I'm going to be a little more detailed about how I produce the correct answer. First I reproduce the example given:

sumOfSquares = 0sumToSquare = 0squareOfSum = 0difference = 0
for x in range(1,11):
    sumOfSquares += x ** 2    print(sumOfSquares)

for y in range(1,11):
    sumToSquare += y
    print(sumToSquare)

squareOfSum = sumToSquare ** 2print(squareOfSum)
difference = squareOfSum - sumOfSquares
print(difference)

Not a very nice code, but it works. Then I change the upper limits in the two for-loops to 101 instead of 11 and put it into the website to verify that I've got it right.

Then I try to make the code look better. This I do because I think it's a good way to learn. Maybe I can do it all in just one for-loop? Let's try. As I've read somewhere, it seems that it's good programming practise to make the code easy to read instead of just doing it on as few lines as possible, so here we go with a more compact, and probably more effective, version:

sumOfSquares = 0sumToSquare = 0squareOfSum = 0difference = 0
for x in range(1,101):
    sumOfSquares += x ** 2    sumToSquare += x

squareOfSum = sumToSquare ** 2difference = squareOfSum - sumOfSquares
print(difference)

/Ludvig