예제 #1
0
def solve_psg(states, reqs, prob_vec_c, prob_vec_r):
    reqlen = len(reqs)
    one_step_vecs = vecprob_combined(reqs, prob_vec_c, prob_vec_r)
    a = []
    b = []
    idx = 0
    key2idx = {}
    j = states[-1]
    states1 = [s for s in states if s != j]
    print('creating matrix...')
    prog = Progress(total_iter=len(states1)**2)
    for i in states1:
        key2idx[i] = idx
        idx += 1
        row = []
        for k in states1:
            if k == i:
                row.append(1.0 - get_p(i, i, reqs, one_step_vecs, j))
            else:
                row.append(-1.0 * get_p(i, k, reqs, one_step_vecs, j))
            prog.count()
        a.append(row.copy())
    b = [1] * len(states1)
    a = np.array(a)
    b = np.array(b)
    print('solving linear system...')
    x, info = lin.bicgstab(a, b)
    # print(x)
    print('solve info: ', info)
    # x = np.linalg.solve(a,b)
    v0 = tuple([0] * reqlen)
    return x[key2idx[v0]]
예제 #2
0
def exphands(req, rounds=20000):
    iters = []
    prog = Progress(total_iter=rounds)
    for i in range(rounds):
        tmp = gethand(req)
        iters.append(tmp)
        prog.count()
    return sum(iters) / (len(iters) * 1.0)
    for line in answers:
        answers_list.append(int(line))

min_num = 0
min_quotient = 2

primes = []
for i in range(9999, 1000, -2):
    if is_prime(i):
        primes.append(i)

progress_ = Progress("Problem 070: Totient permutations", 0, len(primes))

done = 0
for a in range(len(primes)):
    progress_.count = a
    progress_.progress()
    for b in range(a, len(primes)):
        i = primes[a] * primes[b]
        totient_i = (primes[a] - 1) * (primes[b] - 1)
        if i < 10**7:
            if sorted(str(i)) == sorted(str(totient_i)):
                if i / totient_i < min_quotient:
                    min_quotient = i / totient_i
                    min_num = i
                    break

progress_.count = min_num
progress_.total = answers_list[70]
progress_.progress()
all_pentagons = []
all_hexagons = []
all_heptagons = []
all_octagons = []
all_nums = [
    all_triangles, all_squares, all_pentagons, all_hexagons, all_heptagons,
    all_octagons
]

for i in range(1000, 10000):
    for j in range(6):
        if all_functions[j](i):
            all_nums[j].append(i)

progress_ = Progress("Problem 061: Cyclical figurate numbers", 0,
                     len(all_triangles))

for triangle in all_triangles:
    progress_.count += 1
    progress_.progress()
    if find_cycle(all_nums[1:], triangle // 100, triangle % 100, triangle):
        break

progress_.count = find_cycle(all_nums[1:], triangle // 100, triangle % 100,
                             triangle)
progress_.total = answers_list[61]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #5
0
if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 029: Distinct powers", 0, answers_list[29])

result = []
for a in range(2, 101):
    progress_.count = len(list(set(result)))
    progress_.progress()
    for b in range(2, 101):
        result.append(a**b)

progress_.count = len(list(set(result)))
progress_.progress()

if __name__ == '__main__':
    input()
예제 #6
0
from functions import *

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt') as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 058: Spiral primes", 0, answers_list[58])

a = 2
n = 0
while (n/(4*a - 3) >= 0.10) or (n == 0):
    if is_prime(4*a**2 - 10*a + 7):
        n += 1
    if is_prime(4*a**2 - 8*a + 5):
        n += 1
    if is_prime(4*a**2 - 6*a + 3):
        n += 1
    #bottom-right diagonal is always an odd square
    a += 1
    if a % 11 == 0: #arbitrary number for display purposes
        progress_.count = 2*a - 1
        progress_.progress()

progress_.count = 2*a - 1
progress_.progress()

if __name__ == '__main__':
    input()
import math

if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))

progress_ = Progress("Problem 015: Lattice paths", 0, answers_list[15])

n = math.factorial(40) // (math.factorial(20)**2)

progress_.count = n
progress_.progress()

if __name__ == '__main__':
    input()
                continue
            count = 0
            first = 0
            for x in range(0, 10):
                if (a != 0) and (b != 0) and (x == 0):
                    continue
                num = i + 10**(n - a) * k + 10**(n - b) * j
                for l in range(0, n):
                    if (l == a) or (l == b):
                        continue
                    num += 10**(n - l) * x
                if is_prime(num):
                    if (first == 0):
                        first = num
                    count += 1
            if count == 8:
                break
        else:
            continue
        break
    else:
        continue
    break

progress_.count = first
progress_.total = answers_list[51]
progress_.progress()

if __name__ == '__main__':
    input()
if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 071: Ordered fractions", 0, 1000001)

min_distance = 1
min_number = 0

for d in range(2, 1000001):
    if d % 283 == 0:  #arbitrary number for display purposes
        progress_.count = d
        progress_.progress()
    i = 3 * d // 7
    if (3 / 7 - i / d > 0) and (3 / 7 - i / d < min_distance):
        min_distance = 3 / 7 - i / d
        min_number = i

progress_.count = min_number
progress_.total = answers_list[71]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #10
0
if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt') as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 056: Powerful digit sum", 0, 100)

max_digital_sum = 0

for a in range(1, 100):
    progress_.count = a
    progress_.progress()
    for b in range(1, 100):
        digits = [int(i) for i in str(a**b)]
        max_digital_sum = max(max_digital_sum, sum(digits))

progress_.count = max_digital_sum
progress_.total = answers_list[56]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #11
0
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt') as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 059: XOR decryption", 0, 26**3 - 1)

with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\data\\problem_59_data.txt') as f:
    string = f.readline()
    nums = string.split(',')
    nums_int = [int(i) for i in nums]

key = 'aaa'

with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\data\\problem_59_output.txt', 'w') as f:
    for i in range(26**3 - 1):
        progress_.count = i
        progress_.progress()
        key_nums = [ord(i) for i in key]
        new_nums = []
        for j in range(len(nums_int)):
            new_nums.append(key_nums[j % 3] ^ nums_int[j])
        new_chars = [chr(j) for j in new_nums]
        new_string = ''.join(new_chars)
        if new_string.count(' ') > 120:
            f.write(str(sum(new_nums)) + '   ' + new_string + '\n')
        key = increment(key)

progress_.count = answers_list[59]
progress_.total = answers_list[59]
progress_.progress()
예제 #12
0
if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 040: Champernowne's constant", 0,
                     answers_list[40])

number = ''
a = 1
while len(number) < 1000000:
    number += str(a)
    a += 1

progress_.count = int(number[0]) * int(number[9]) * int(number[99]) * int(
    number[999]) * int(number[9999]) * int(number[99999]) * int(number[999999])
progress_.progress()

if __name__ == '__main__':
    input()
예제 #13
0
if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from progress import Progress

answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 030: Digit fifth powers", 0, 354295)

total = 0
for n in range(2, 354295):
    if n % 1729 == 0:
        progress_.count = n
        progress_.progress()
    power_digits = 0
    for i in str(n):
        power_digits += int(i)**5
    if n == power_digits:
        total += n

progress_.count = total
progress_.total = answers_list[30]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #14
0
if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt') as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 048: Self powers", 0, 1000)

sum_power = 0
for i in range(1, 1001):
    progress_.count = i
    progress_.progress()
    sum_power += i**i

progress_.count = int(str(sum_power)[-10:])
progress_.total = answers_list[48]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #15
0
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 041: Pandigital prime", 0, 2)
progress_.progress()

all_nums = '1234567'
all_rearrangements = []
for i in range(1, 8):
    rearrangements_lists = itertools.permutations(all_nums[:i], i)
    rearrangements_str = [
        ''.join(rearrangement) for rearrangement in rearrangements_lists
    ]
    all_rearrangements.extend(rearrangements_str)

progress_.count = 1
progress_.progress()

all_rearrangements_int = [int(i) for i in all_rearrangements]
all_rearrangements_int.sort(reverse=True)

for rearrangement in all_rearrangements_int:
    if is_prime(rearrangement):
        break

progress_.count = rearrangement
progress_.total = answers_list[41]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #16
0
def product_horizontal(x, y):
    return(all_rows[y][x]*all_rows[y][x + 1]*all_rows[y][x + 2]*all_rows[y][x + 3])

def product_vertical(x, y):
    return(all_rows[y][x]*all_rows[y + 1][x]*all_rows[y + 2][x]*all_rows[y + 3][x])

def product_diagonal_forward(x, y):
    return(all_rows[y][x]*all_rows[y + 1][x + 1]*all_rows[y + 2][x + 2]*all_rows[y + 3][x + 3])

def product_diagonal_backward(x, y):
    return(all_rows[y][x]*all_rows[y + 1][x - 1]*all_rows[y + 2][x - 2]*all_rows[y + 3][x - 3])

for y_position in range(20):
    for x_position in range(20):
        if x_position < 17:
            progress_.count = max(progress_.count, product_horizontal(x_position, y_position))
            progress_.progress()
        if y_position < 17:
            progress_.count = max(progress_.count, product_vertical(x_position, y_position))
            progress_.progress()
        if (x_position < 17) and (y_position < 17):
            progress_.count = max(progress_.count, product_diagonal_forward(x_position, y_position))
            progress_.progress()
        if (x_position > 2) and (y_position < 17):
            progress_.count = max(progress_.count, product_diagonal_backward(x_position, y_position))
            progress_.progress()

if __name__ == '__main__':
    input()
예제 #17
0
if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 006: Sum square difference", 1, 101)

sum = 0
squared_sum = 0
for n in range(1, 101):
    sum += n
    squared_sum += n**2
    progress_.count = n
    progress_.progress()

progress_.count = sum**2 - squared_sum
progress_.total = answers_list[6]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #18
0
if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt') as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 009: Special Pythagorean triplet", 1, 1000)

for a in range(1, 1000):
    progress_.count = a
    progress_.progress()
    for b in range(a, 1000):
        if a**2 + b**2 == (1000 - a - b)**2:
            break
    else:
        continue
    break

progress_.count = a*b*(1000 - a - b)
progress_.total = answers_list[9]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #19
0
# Count lines
total_entries = num_lines = sum(1 for line in open(filename)) - 1

progress = Progress(total_entries)
start = time.time()
wrongs = []
with open(filename) as fd:
    data = csv.DictReader(fd, delimiter="\t", quotechar='"', escapechar='')
    for r in data:
        raw_id = r['raw_id']

        # Check if valid with regex
        match = re.match(r"^(tt)*(?P<id>\d{7,10}).*", raw_id)
        if not match:
            progress.count()
            wrongs.append(raw_id)
            continue

        imdb_id = match.group(2)
        film_node = n['Movie/tt' + imdb_id]

        # Create a node for dbpedia
        uri = r['uri']
        wiki_node = URIRef(uri)
        g.add((film_node, n['has' + source + 'Node'], wiki_node))

        progress.count()
        if progress.finished():
            break
예제 #20
0
from functions import *

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt') as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 044: Pentagon numbers", 1, 2200) #arbitrary number for display purposes

pentagon_list = [1]
while True:
    pentagon_list.append(progress_.count*(3*progress_.count - 1)//2)
    for i in range(len(pentagon_list) - 1):
        sum_ =  pentagon_list[progress_.count] + pentagon_list[i]
        diff_ = pentagon_list[progress_.count] - pentagon_list[i]
        if is_pentagon(sum_) and is_pentagon(diff_):
            break
    else:
        progress_.count += 1
        if progress_.count % 11 == 0: #arbitrary number for display purposes
            progress_.progress()
        continue
    break

progress_.count = diff_
progress_.total = answers_list[44]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #21
0
from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 074: Digit factorial chains", 0, 1000000)

chain_lengths_dict = {}
count = 0

for a in range(3, 1000000):
    chain = []
    n = a
    if a % 3871 == 0:  #arbitrary number for display purposes
        progress_.count = a
        progress_.progress()
    while True:
        if n in chain:
            chain_lengths_dict[a] = len(chain)
            break
        elif n in chain_lengths_dict:
            chain_lengths_dict[a] = len(chain) + chain_lengths_dict[n]
            break
        else:
            chain.append(n)
        n = sum_factorial_digits(n)
    if chain_lengths_dict[a] == 60:
        count += 1

progress_.count = count
예제 #22
0
if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from functions import *

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt') as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 039: Integer right triangles", 0, 1000)

max_count = 0
max_num = 0

for p in range(120, 1000, 2):
    progress_.count = p
    progress_.progress()
    count = count_triangles(p)
    if count > max_count:
        max_count = count
        max_num = p

progress_.count = max_num
progress_.total = answers_list[39]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #23
0
if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from functions import *

from progress import Progress

answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 045: Triangular, pentagonal, and hexagonal", 0,
                     answers_list[45])

n = 144

while True:
    progress_.count = n * (2 * n - 1)
    progress_.progress()
    if is_pentagon(progress_.count):
        break
    n += 1

if __name__ == '__main__':
    input()
예제 #24
0
from progress import Progress

answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))

all_lines = [
    line.rstrip('\n') for line in open(
        'C:\\Users\\James Jiang\\Documents\\Project Euler\\data\\problem_13_data.txt'
    )
]
all_nums = [int(i) for i in all_lines]

progress_ = Progress("Problem 013: Large sum", 0, len(all_nums))

sum_ = 0
for i in range(len(all_nums)):
    progress_.count = i
    progress_.progress()
    sum_ += all_nums[i]

progress_.count = int(str(sum_)[:10])
progress_.total = answers_list[13]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #25
0
import math

if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 073: Counting fractions in a range", 0, 12001)

count = 0
for n in range(2, 12001):
    progress_.count = n
    progress_.progress()
    for i in range(math.floor(n / 3) + 1, math.ceil(n / 2)):
        if math.gcd(n, i) == 1:
            count += 1

progress_.count = count
progress_.total = answers_list[73]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #26
0
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from functions import *

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 066: Diophantine equation", 0, 1000)

max_x = 0
max_D = 0

for D in range(2, 1001):
    progress_.count = D
    progress_.progress()
    if not is_square(D):
        d = minimal_solution(D)
        if d > max_x:
            max_x = d
            max_D = D

progress_.count = max_D
progress_.total = answers_list[66]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #27
0
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 050: Consecutive prime sum", 0, 1000000)

prime_sum = 0
primes = []
num = 2

while True:
    if is_prime(num):
        primes.append(num)
        if prime_sum + num >= 1000000:
            break
        prime_sum += num
        progress_.count = prime_sum
        progress_.progress()
    num += 1

while not is_prime(prime_sum):
    prime_sum -= primes[0]
    del primes[0]

progress_.count = prime_sum
progress_.total = answers_list[50]
progress_.progress()

if __name__ == '__main__':
    input()
예제 #28
0
if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from functions import *

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 065: Convergents of e", 0, answers_list[65])
progress_.progress()

progress_.count = sum_numerator_fraction_expansion_e(99)
progress_.progress()

if __name__ == '__main__':
    input()
예제 #29
0
from functions import *

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))
progress_ = Progress("Problem 049: Prime permutations", 1000,
                     int(str(answers_list[49])[:4]))

done = 0
n = 1488
while True:
    if is_prime(n):
        progress_.count = n
        progress_.progress()
        for a in range(1000, 5000):
            if is_prime(n + a) and is_prime(n + 2 * a):
                digits_1 = [int(i) for i in str(n)]
                digits_2 = [int(i) for i in str(n + a)]
                digits_3 = [int(i) for i in str(n + 2 * a)]
                if (sorted(digits_1)
                        == sorted(digits_2)) and (sorted(digits_1)
                                                  == sorted(digits_3)):
                    done = 1
                    break
    if done:
        break
    n += 1
예제 #30
0
import math

if __name__ == '__main__':
    import sys
    sys.path.insert(0, 'C:\\Users\\James Jiang\\Documents\\Project Euler')

from progress import Progress
answers_list = ['dummy']
with open('C:\\Users\\James Jiang\\Documents\\Project Euler\\answers.txt'
          ) as answers:
    for line in answers:
        answers_list.append(int(line))

digits = [int(i) for i in str(math.factorial(100))]

progress_ = Progress("Problem 020: Factorial digit sum", 0, len(digits))

sum_ = 0
for i in range(len(digits)):
    progress_.count = i
    progress_.progress()
    sum_ += digits[i]

progress_.count = sum_
progress_.total = answers_list[20]
progress_.progress()

if __name__ == '__main__':
    input()