mu chance or much chance ?

日々の戯れ言

Project Euler 33

  • 問題

Problem 33:Digit cancelling fractions
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.<<

  • 解答例
def gcd(m, n):
    while n:
        m, n = n, m % n
    return m

tempNumerator = 1
tempDenominator = 1
for a in range(1, 10):
    for b in range(1, 10):
        for c in range(1, 10):
            if a < b and not a == c and not b == c:
                if (10 * a + c) * b ==  (10 * c + b) * a:
                    temp1 = 10 * a + c
                    temp2 = 10 * c + b
                    tempNumerator *= temp1
                    tempDenominator *= temp2
                if (10 * c + a) * b ==  (10 * b + c) * a:
                    temp1 = 10 * c + a
                    temp2 = 10 * b + c
                    tempNumerator *= temp1
                    tempDenominator *= temp2

print(tempDenominator // gcd(tempNumerator, tempDenominator))

Project Euler 32

  • 問題

Problem 32:Pandigital products
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

  • 解答例
l = []
for i in range(1, 10):
    for j in range(1000, 10000):
        temp = ''.join(sorted(str(i) + str(j) + str(i * j)))
        if temp == "123456789":
            l.append(i * j)

for i in range(10, 100):
    for j in range(100, 1000):
        temp = ''.join(sorted(str(i) + str(j) + str(i * j)))
        if temp == "123456789":
            l.append(i * j)

listUniq = list(set(l))
print(sum(listUniq))

Project Euler 31

  • 問題

Problem 31:Coin sums
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:

1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
It is possible to make £2 in the following way:

1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
How many different ways can £2 be made using any number of coins?

  • 解答例
way = [0] * 201
way[0] = 1
coin = [1, 2, 5, 10, 20, 50, 100, 200]

for c in coin:
    for w in range(c, 201):
        way[w] += way[w - c]
print(way[200])

Processing + Python Mode + PDF Export

以下の本を読みながら,ProcessingのPythonモードを勉強しています.

その中で,ProcessingのPythonモードでPDFを出力する方法をメモします.

import processing.pdf.*;

を最初に記載する代わりに,

add_library('pdf')

を最初に記載すれば,OKです.

  • サンプルプログラム
class Particle:
    def __init__(self, diameter):
        self.diameter = diameter
        self.location = PVector(random(0, width), random(0, height))
        self.col = color(random(255), random(255), random(255))
    def draw(self):
        fill(self.col)
        ellipse(self.location.x, self.location.y, self.diameter, self.diameter)

add_library('pdf')

num = 1000
p = []

def setup():
    size(800, 600, P2D)
    frameRate(60)
    noLoop()
    noStroke()
    for i in range(num):
        p.append(Particle(random(8, 32)))

def draw():
    beginRecord(PDF, "output.pdf")
    background(0)
    for i in range(num):
        p[i].draw()
    endRecord()
  • 出力結果(PDFをpngに変えています)

f:id:muchance:20170619115701p:plain

Project Euler 30

  • 問題

Problem 30:Digit fifth powers
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 1^4 + 6^4 + 3^4 + 4^4
8208 = 8^4 + 2^4 + 0^4 + 8^4
9474 = 9^4 + 4^4 + 7^4 + 4^4
As 1 = 1^4 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

  • 解答例
sum = 0
for num in range(2, (9 ** 5) * 6 + 1):
    temp = 0
    numStr = str(num)
    for x in numStr:
        temp += (int(x) ** 5)
    if(num == temp):
        sum += num
print(sum)

Project Euler 29

  • 問題

Problem 29:Distinct powers
Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

2^2=4, 2^3=8, 2^4=16, 2^5=32
3^2=9, 3^3=27, 3^4=81, 3^5=243
4^2=16, 4^3=64, 4^4=256, 4^5=1024
5^2=25, 5^3=125, 5^4=625, 5^5=3125
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

  • 解答例
numList = []

for a in range(2, 101):
    for b in range(2, 101):
        numList.append(a ** b)

numListUniq = list(set(numList))
print(len(numListUniq))