mu chance or much chance ?

日々の戯れ言

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

Project Euler 27

  • 問題

Problem 27:Quadratic primes
Euler discovered the remarkable quadratic formula:
n^2+n+41
It turns out that the formula will produce 40 primes for the consecutive integer values 0≤n≤39. However, when n=40, 40^2+40+41=40(40+1)+41 is divisible by 41, and certainly when n=41, 41^2+41+41 is clearly divisible by 41.

The incredible formula n^2−79n+1601 was discovered, which produces 80 primes for the consecutive values 0≤n≤79. The product of the coefficients, −79 and 1601, is −126479.

Considering quadratics of the form:

n^2+an+b, where |a|<1000 and |b|≤1000

where |n| is the modulus/absolute value of n
e.g. |11|=11 and |−4|=4
Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.

  • 解答例
def sieveEratosthenes(num):
    isPrime = [True] * (num + 1)
    isPrime[0] = False
    isPrime[1] = False
    primes = []
    for i in range(2, (num + 1)):
        if isPrime[i]:
            primes.append(i)
            for j in range(2 * i, (num + 1), i):
                isPrime[j] = False
    return primes
    
def isPrime(num):
    if num <= 1:
        return False
    elif num == 2 or num == 3:
        return True
    if num % 2 == 0 or num % 3 == 0:
        return False
    if not(num % 6 == 1) and not(num % 6 == 5):
        return False
    i = 5
    while i * i <= num:
        if num % i == 0 or num % (i + 2) == 0:
            return False
        i += 6
    
    return True

def equation(n, a, b):
    return n ** 2 + a * n + b

b = sieveEratosthenes(1000)
del b[0]

maxA = 0
maxB = 0
maxCount = 0

for y in b:
    for x in range(-y, 1000, 2):
        c = 0
        while isPrime(equation(c, x, y)):
            c += 1
        if maxCount < c:
            maxA = x
            maxB = y
            maxCount = c
            
print(maxA * maxB)

Project Euler 26

  • 問題

Problem 26:Reciprocal cycles
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
1/2 = 0.5
1/3 = 0.(3)
1/4 = 0.25
1/5 = 0.2
1/6 = 0.1(6)
1/7 = 0.(142857)
1/8 = 0.125
1/9 = 0.(1)
1/10 = 0.1
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.

  • 解答例
maxLen = 0
index = 0

for i in range(2, 1000):
    l = 0
    r = 1
    rList = []
    while True:
        r = (r * 10) % i
        l += 1
        if r == 0 or r in rList:
            break
        rList.append(r)
    if maxLen < l:
        maxLen = l
        index = i

print(index)