mu chance or much chance ?

日々の戯れ言

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))

Project Euler 28

  • 問題

Problem 28:Number spiral diagonals
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

  • 解答例
def spiralDiagonals(num):
    sum = 1
    for i in range(2, num + 1):
        for j in range(0, 4):
            sum += ((2 * i - 1) * (2 * i - 1) - j * (2 * i - 2))
    return sum

num = 501 # 1001 = 2 * num - 1

print(spiralDiagonals(num))