Euler Problems 11 to 22

Problem 11.

#! /usr/bin/python

'''In the 20×20 grid below, four numbers along a diagonal line have
 been marked in red.

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

The product of these numbers is 26 × 63 × 78 × 14 = 1788696.

What is the greatest product of four adjacent numbers in any direction
 (up, down, left, right, or diagonally) in the 20×20 grid?'''


__author__="rog"
__date__ ="$29-Jul-2010 12:44:04$"
import random
#from numpy import *
import sys
sys.path.append("../")
from rogutil import *

numstr = """08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"""
def findindexerror(mylist, y, x):
        result = False
        try:
                a = mylist[y][x]
        except IndexError:
                result = True
        return result

def main():
        # Convert the string of numbers into an array of arrays called ylist
        numy = numstr.split("\n")
        ylist = []
        for line in numy:
                numx = line.split(" ")
                numx = [int(integral) for integral in list(numx)]
                ylist.append(numx)



        # Iterate all items individually
        xpos = 0
        ypos = 0
        highscore = 0
        results = []
        for y in ylist:
                xpos = 0
                for x in y:
                        # 1
                        if not findindexerror(ylist, ypos, xpos + 3):
                                resultlist = []
                                resultlist.append(ylist[ypos][xpos])
                                resultlist.append(ylist[ypos][xpos + 1])
                                resultlist.append(ylist[ypos][xpos + 2])
                                resultlist.append(ylist[ypos][xpos + 3])
                                results.append(multiplylist(resultlist))

                        # 2
                        if not findindexerror(ylist, ypos, xpos - 3):
                                resultlist = []
                                resultlist.append(ylist[ypos][xpos])
                                resultlist.append(ylist[ypos][xpos - 1])
                                resultlist.append(ylist[ypos][xpos - 2])
                                resultlist.append(ylist[ypos][xpos - 3])
                                results.append(multiplylist(resultlist))

                        # 3
                        if not findindexerror(ylist, ypos - 3, xpos):
                                resultlist = []
                                resultlist.append(ylist[ypos][xpos])
                                resultlist.append(ylist[ypos - 1][xpos])
                                resultlist.append(ylist[ypos - 2][xpos])
                                resultlist.append(ylist[ypos - 3][xpos])
                                results.append(multiplylist(resultlist))

                        # 4
                        if not findindexerror(ylist, ypos + 3, xpos):
                                resultlist = []
                                resultlist.append(ylist[ypos][xpos])
                                resultlist.append(ylist[ypos + 1][xpos])
                                resultlist.append(ylist[ypos + 2][xpos])
                                resultlist.append(ylist[ypos + 3][xpos])
                                results.append(multiplylist(resultlist))

                        # 5
                        if not findindexerror(ylist, ypos + 3, xpos + 3):
                                resultlist = []
                                resultlist.append(ylist[ypos][xpos])
                                resultlist.append(ylist[ypos + 1][xpos + 1])
                                resultlist.append(ylist[ypos + 2][xpos + 2])
                                resultlist.append(ylist[ypos + 3][xpos + 3])
                                results.append(multiplylist(resultlist))

                        # 6
                        if not findindexerror(ylist, ypos - 3, xpos - 3):
                                resultlist = []
                                resultlist.append(ylist[ypos][xpos])
                                resultlist.append(ylist[ypos - 1][xpos - 1])
                                resultlist.append(ylist[ypos - 2][xpos - 2])
                                resultlist.append(ylist[ypos - 3][xpos - 3])
                                results.append(multiplylist(resultlist))

                        # 7
                        if not findindexerror(ylist, ypos - 3, xpos + 3):
                                resultlist = []
                                resultlist.append(ylist[ypos][xpos])
                                resultlist.append(ylist[ypos - 1][xpos + 1])
                                resultlist.append(ylist[ypos - 2][xpos + 2])
                                resultlist.append(ylist[ypos - 3][xpos + 3])
                                results.append(multiplylist(resultlist))

                        # 8
                        if not findindexerror(ylist, ypos + 3, xpos - 3):
                                resultlist = []
                                resultlist.append(ylist[ypos][xpos])
                                resultlist.append(ylist[ypos + 1][xpos - 1])
                                resultlist.append(ylist[ypos + 2][xpos - 2])
                                resultlist.append(ylist[ypos + 3][xpos - 3])
                                results.append(multiplylist(resultlist))


                        xpos += 1 # Keep me at end of loop
                        results = list(set(results))
                ypos += 1 # Keep me at end of loop

        print(len(results))
        for result in results:
                if result > highscore:
                        highscore = result

        print ("Highscore is", highscore)








if __name__ == "__main__":
        main()
# Problem 12

#!/usr/bin/env python
'''The sequence of triangle numbers is generated by adding the natural
   numbers.
 So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.
 The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

         1: 1
         3: 1,3
         6: 1,2,3,6
        10: 1,2,5,10
        15: 1,3,5,15
        21: 1,3,7,21
        28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five
divisors.

What is the value of the first triangle number to have
 over five hundred divisors?'''

from functools import reduce
import math
import sys
sys.path.append("../")
from rogutil import *
import time
tt = time.time()
def main():
        pass
        count = 0
#------------------------------------------
        '''generator for triangle numbers'''

        def gen():
                n = 2
                while n < 100000000:
                        n += 1
                        numb = n * (n + 1) / 2
                        yield numb
        g = gen()
        trinum = next(g)
#------------------------------------------
        kounter = 0     # errr... counter
        prim = 0        # The Prime Number for division of the
                                        #Triangle number
        i, z = 0, 0
        prims = gen_x(10000, gen_primes)     # prime number genny
        prim = prims[z]              # For incrementing the Prime Number
        div_lst = []            #Divisor list
        power_lst = []           #Prime number (power) list
        sums_divslst = []
        dd = trinum
        total_divs = 0

#------------------Calculate divisors-----------------------
        while dd < 1000000000:

                if not trinum % prim == 0: # Is there a modulus, if so, increase     the Prime Number
                        if count > 0: #Divisor counter
                                kounter = count + 1
                                power_lst.append(kounter)
                        count = 0
                        z += 1
                        prim = prims[z]
                        continue

                trinum = trinum / prim
                if not prim in div_lst:
                        div_lst.append(prim)
                count += 1
                if trinum == 1:  #Divide down to 1, test the amount of divisors and
                        kounter = count + 1   # if too low, increase the Triangle Number
                        power_lst.append(kounter)
#--------------work out divisor count-----------------------
                        div_product = reduce(lambda x,y : x * y, power_lst)
                        if div_product > total_divs:
                                total_divs = div_product
                        if total_divs > 500:
                                break
#--------------reset variables---------------------------
                        trinum = 0
                        count = 0
                        z = 0
                        prim = prims[z]
                        trinum = next(g)
                        dd = trinum
                        div_lst = []
                        power_lst = []

                        continue

        print("Triangle number : ", dd," Divisors = ",total_divs)

if __name__ == '__main__':
        main()
        print(time.time() - tt)
Problem 13.

#--------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      rog
#
# Created:     31/07/2010
# Copyright:   (c) rog 2010
# Licence:     <your licence>
#--------------------------------------------------------------------------
#!/usr/bin/env python

# Work out the first ten digits of the sum of the following
# one-hundred 50-digit numbers.


import math
import sys
sys.path.append("../")
from rogutil import *
numstr = """37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
70386486105843025439939619828917593665686757934951
62176457141856560629502157223196586755079324193331
64906352462741904929101432445813822663347944758178
92575867718337217661963751590579239728245598838407
58203565325359399008402633568948830189458628227828
80181199384826282014278194139940567587151170094390
35398664372827112653829987240784473053190104293586
86515506006295864861532075273371959191420517255829
71693888707715466499115593487603532921714970056938
54370070576826684624621495650076471787294438377604
53282654108756828443191190634694037855217779295145
36123272525000296071075082563815656710885258350721
45876576172410976447339110607218265236877223636045
17423706905851860660448207621209813287860733969412
81142660418086830619328460811191061556940512689692
51934325451728388641918047049293215058642563049483
62467221648435076201727918039944693004732956340691
15732444386908125794514089057706229429197107928209
55037687525678773091862540744969844508330393682126
18336384825330154686196124348767681297534375946515
80386287592878490201521685554828717201219257766954
78182833757993103614740356856449095527097864797581
16726320100436897842553539920931837441497806860984
48403098129077791799088218795327364475675590848030
87086987551392711854517078544161852424320693150332
59959406895756536782107074926966537676326235447210
69793950679652694742597709739166693763042633987085
41052684708299085211399427365734116182760315001271
65378607361501080857009149939512557028198746004375
35829035317434717326932123578154982629742552737307
94953759765105305946966067683156574377167401875275
88902802571733229619176668713819931811048770190271
25267680276078003013678680992525463401061632866526
36270218540497705585629946580636237993140746255962
24074486908231174977792365466257246923322810917141
91430288197103288597806669760892938638285025333403
34413065578016127815921815005561868836468420090470
23053081172816430487623791969842487255036638784583
11487696932154902810424020138335124462181441773470
63783299490636259666498587618221225225512486764533
67720186971698544312419572409913959008952310058822
95548255300263520781532296796249481641953868218774
76085327132285723110424803456124867697064507995236
37774242535411291684276865538926205024910326572967
23701913275725675285653248258265463092207058596522
29798860272258331913126375147341994889534765745501
18495701454879288984856827726077713721403798879715
38298203783031473527721580348144513491373226651381
34829543829199918180278916522431027392251122869539
40957953066405232632538044100059654939159879593635
29746152185502371307642255121183693803580388584903
41698116222072977186158236678424689157993532961922
62467957194401269043877107275048102390895523597457
23189706772547915061505504953922979530901129967519
86188088225875314529584099251203829009407770775672
11306739708304724483816533873502340845647058077308
82959174767140363198008187129011875491310547126581
97623331044818386269515456334926366572897563400500
42846280183517070527831839425882145521227251250327
55121603546981200581762165212827652751691296897789
32238195734329339946437501907836945765883352399886
75506164965184775180738168837861091527357929701337
62177842752192623401942399639168044983993173312731
32924185707147349566916674687634660915035914677504
99518671430235219628894890102423325116913619626622
73267460800591547471830798392868535206946944540724
76841822524674417161514036427982273348055556214818
97142617910342598647204516893989422179826088076852
87783646182799346313767754307809363333018982642090
10848802521674670883215120185883543223812876952786
71329612474782464538636993009049310363619763878039
62184073572399794223406235393808339651327408011116
66627891981488087797941876876144230030984490851411
60661826293682836764744779239180335110989069790714
85786944089552990653640447425576083659976645795096
66024396409905389607120198219976047599490197230297
64913982680032973156037120041377903785566085089252
16730939319872750275468906903707539413042652315011
94809377245048795150954100921645863754710598436791
78639167021187492431995700641917969777599028300699
15368713711936614952811305876380278410754449733078
40789923115535562561142322423255033685442488917353
44889911501440648020369068063960672322193204149535
41503128880339536053299340368006977710650566631954
81234880673210146739058568557934581403627822703280
82616570773948327592232845941706525094512325230608
22918802058777319719839450180888072429661980811197
77158542502016545090413245809786882778948721859617
72107838435069186155435662884062257473692284509516
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690"""

def main():
        result = 0
        # split at newline
        for y in numstr.split("\n"):
                numx = int(y)
                # add the numbers (numx)
                result += numx
        # print first 10 digits
        print (str(result)[:10])
if __name__ == '__main__':
        main()
#Problem 14.

'''The following iterative sequence is defined for the set of positive
integers:

n ? n/2 (n is even)
n ? 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following
sequence:
13 ? 40 ? 20 ? 10 ? 5 ? 16 ? 8 ? 4 ? 2 ? 1

It can be seen that this sequence (starting at 13 and finishing at 1)
contains 10 terms. Although it has not been proved yet (Collatz Problem),
it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one
million.'''




cache = {1: 1}

def collatz(n):
    if n not in cache:
        if n % 2 == 0:
            cache[n] = 1 + collatz(n // 2)
        else:
            cache[n] = 1 + collatz(3 * n + 1)
    return cache[n]

if __name__ == '__main__':

    for x in range(3, 1000000):
        ans = collatz(x)

    ans2 = max(cache.keys(), key = (lambda key: cache[key]))
    print(ans2)
# Problem 15

#!/usr/bin/env python

'''Starting in the top left corner of a 2×2 grid, there are 6 routes
 (without backtracking) to the bottom right corner.

How many routes are there through a 20×20 grid?'''

# The answer = 40! / (20! * 20!)

def main():
        pass
        n = 1
#----------------------------
        ans = 1

        for a in range(1,20,1):

                ans = ans * (n + a)
#-----------------------------
        ans2 = 1

        for a in range(1,40, 1):
                ans2 = ans2 * (n + a)
#-----------------------------

        print(ans2 / (ans*ans))


if __name__ == '__main__':
        main()
#Problem 16.

#! /usr/bin/python

# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="rog"
__date__ ="$29-Jul-2010 12:44:04$"

import sys
from rogutil import multiplylist
sys.path.append("../")
#What is the sum of the digits of the number 2^1000?

def main():
        print(sum(list(map(int, list(str(2**1000))))))

if __name__ == "__main__":
        main()
# Problem 17.

#-------------------------------------------------------------------------------
# Name:        module2
# Purpose:
#
# Author:      rog
#
# Created:     02/08/2010
# Copyright:   (c) rog 2010
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
"""
How many letters would be needed to write all the numbers in words
from 1 to 1000?
"""

import sys
sys.path.append("../")
from rogutil import *

dict_thou = {1:11,0:0}
dict_hund = {9:14,8:15,7:15,6:13,5:14,4:14,3:15,2:13,1:13,0:0}
dict_tens = {9:6,8:6,7:7,6:5,5:5,4:5,3:6,2:6,0:0}
dict_teens = {19:8,18:8,17:9,16:7,15:7,14:8,13:8,12:6,11:6,10:3}
dict_singl = {9:4,8:5,7:5,6:3,5:4,4:4,3:5,2:3,1:3,0:0}
dict_hundreds = {9:11,8:12,7:12,6:10,5:11,4:11,3:12,2:10,1:10,0:0}

def main():
                cnt = 0
                total = 0
                for number_for_test in range(1000,0,-1):

                        number_for_test = zeromaker(str(number_for_test), 4)


                        ''' Is this 100, 200 etc? '''
                        if int(number_for_test [2]) == 0 and \
                           int(number_for_test [3]) == 0 and \
                           int(number_for_test [1]) > 0:
                                hundreds = (number_for_test[1])
                                hundred = int(hundreds)
                                hh = dict_hundreds[hundred]
                                y = hh
                                total = total + y
                                hh,hundreds,hundred,y = 0,0,0,0
                                continue


                        ''' is it between ten and 19? '''
                        if int(number_for_test [2]) == 1:
                                teens = (number_for_test[2])+(number_for_test[3])
                                teen = int(teens)
                                s = int(number_for_test [1])
                                teenager = dict_teens[teen]
                                v = dict_hund[s]
                                y = v + teenager
                                total = total + y
                                r,s,v,y = 0,0,0,0
                                continue

                        '''If statement required to cut out 200, 300 etc'''
                        if number_for_test [2] != 0 and \
                           number_for_test [3] != 0:
                                r = int(number_for_test [0])
                                s = int(number_for_test [1])
                                t = int(number_for_test [2])
                                u = int(number_for_test [3])
                                z = dict_thou[r]
                                v = dict_hund[s]
                                w = dict_tens[t]
                                x = dict_singl[u]
                                y = v + w + x + z
                                total = total + y
                                r,s,t,u,v,w,x,y,z = 0,0,0,0,0,0,0,0,0
                                y = 0
                                continue

                print("Total = ", total,"Count = ",cnt)



if __name__ == '__main__':
        import time
        t = time.time()
        main()
        print(time.time() - t)
# Problem 18.

'''By starting at the top of the triangle below and moving to adjacent
 numbers on the row below, the maximum total from top to bottom is 23.

3
7 4
2 4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

NOTE: As there are only 16384 routes, it is possible to solve this
problem by trying every route. However, Problem 67, is the same
 challenge with a triangle containing one-hundred rows; it cannot
  be solved by brute force, and requires a clever method! ;o)'''


#!/usr/bin/env python
import sys
sys.path.append("../")
from rogutil import *
from pprint import pprint

def doif(lst, a,b,c,str_upper,str_lower):
        mylist = list(lst)
        if mylist [str_lower] [b] > mylist [str_lower] [c]:
                mylist[str_upper][a] = mylist[str_upper][a] + mylist[str_lower][b]
        else:
                mylist[str_upper][a] = mylist[str_upper][a] + mylist[str_lower][c]
return mylist



mystr = '''75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23'''



def main():
        pass

        """ changes the above string into a list"""
        mylist = []
        for item in mystr.split("\n"):
                mylist.append(list(map(int,item.split(" "))))

        pprint(mylist)

        #import pdb; pdb.set_trace()

        '''start at the bottom two rows'''
        str_upper, str_lower = len(mylist[-1])-2,len(mylist[-1])-1
        e = len(mylist[str_upper])

        a, b, c, d = 0, 0, 1, 0
        while e != 0:
                while d in range(0,e,1):
                        mylist = doif(mylist,a,b,c, str_upper, str_lower)
                        a += 1
                        b += 1
                        c += 1
                        d += 1
                d, a, b, c = 0, 0, 0, 1
                str_upper -=1
                str_lower -=1
                e -= 1

        print(mylist[0])

        exit()

if __name__ == '__main__':
        main()
#Problem 19.

#!/usr/bin/env python

'''You are given the following information, but you may prefer to do some
 research for yourself.

        * 1 Jan 1900 was a Monday.
        * Thirty days has September,
          April, June and November.
          All the rest have thirty-one,
          Saving February alone,
          Which has twenty-eight, rain or shine.
          And on leap years, twenty-nine.
        * A leap year occurs on any year evenly divisible by 4, but not on a
         century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century
 (1 Jan 1901 to 31 Dec 2000)?'''

'''To find the ordinal position of the day within the week for any date
 between Jan 1, 1901 and Dec 31 2099

          1.  Find the year_ordinal using the table of days before the
          first of the month

          2.  Calculate week_ordinal(1/1) as follows

                  a = (year - 1901) modulo 28
                  b = floor (a/4)
                  week_ordinal(1/1) = (2 + a + b) modulo 7 + 1

          3.  Day of the Week = ((year_ordinal - 1) + (week_ordinal(1/1) - 1))
           modulo 7 + 1'''

import math

def main():
        pass

lst1 = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] #std
lst2 = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335] #leap

sunday = 0
y = 0

while y < 12:

        for x in range(2000,1900,-1):
                a = (x - 1901) % 28
                z = x
                if z % 4 == 0:
                        year_ordinal = lst2[y]

                else:
                        year_ordinal = lst1[y]

                b = math.floor(a / 4)
                week_ordinal = (2 + a + b) % 7 + 1
                day_of_the_week = ((year_ordinal-1) + (week_ordinal - 1)) % 7 + 1

                if day_of_the_week == 7:
                        sunday += 1
                if x == 1901:   #increment to next month
                        y += 1
                        continue
print("Year =  ", x," Day ",day_of_the_week,"month = ", y,"Sundays = ", sunday)
exit()

if __name__ == '__main__':
        main()
#Problem 20


''''n! means n × (n ? 1) × ... × 3 × 2 × 1

For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!'''


#!/usr/bin/env python

import sys
sys.path.append("../")
from rogutil import *
#import pdb; pdb.set_trace()

 def factorial_loop(num):
     accum = 1
     while num > 1:
         accum *= num
         num -= 1

     total = 0
     accum = str(accum)
     for items in accum:
         total += int(items)
     return total

 ans = factorial_loop(100)
 print(ans)




if __name__ == '__main__':
        #pdb.runcall(main)
        main()
# Problem 21.
#!/usr/bin/env python

'''Let d(n) be defined as the sum of proper divisors
 of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a is not equal to b, then
 a and b are an amicable pair and each of a and b are
 called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4,
 5, 10, 11, 20, 22, 44, 55 and 110; therefore
 d(220) = 284. The proper divisors of 284 are 1, 2, 4,
 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.'''

import time
tt = time.time()
def main():
        pass

'''Empty list'''
g = []

def divsum(n):
        return sum(i for i in range(1, n) if not n % i)


'''work out the divisors from 10000 to 2'''
for x in range(10000, 2, -1):
        ''' check if amicable pair'''
        c = divsum(x)
        v = divsum(c)

        if v == x and v != c:
                g.append(divsum(x))
        else:
                continue

print(sum(g))



if __name__ == '__main__':
        main()
        print(time.time() - tt, " secs.")
# Problem 22.

 #!/usr/bin/env python

 '''Using names.txt (right click and 'Save Link/Target As...'),
 a 46K text file containing over five-thousand first names,
  begin by sorting it into alphabetical order. Then working
 out the alphabetical value for each name, multiply this
 value by its alphabetical position in the list to obtain
 a name score.

 For example, when the list is sorted into alphabetical order,
  COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th
 name in the list. So, COLIN would obtain a score
 of 938 × 53 = 49714.

 What is the total of all the name scores in the file?'''


 import re

 def main():
         pass


 f = open("names.txt",'r')

 names = []

 names = sorted(f.read().replace('"','').split(','),key=str)

 i = 0
 sum = 0
 ans = 0

 for string in names:
         i += 1
         chars = list(string)

         for x in chars:

                 if x == 'A':
                         t = 1
                 elif x == 'B':
                         t = 2
                 elif x == 'C':
                         t = 3
                 elif x == 'D':
                         t = 4
                 elif x == 'E':
                         t = 5
                 elif x == 'F':
                         t = 6
                 elif x == 'G':
                         t = 7
                 elif x == 'H':
                         t = 8
                 elif x == 'I':
                         t = 9
                 elif x == 'J':
                         t = 10
                 elif x == 'K':
                         t = 11
                 elif x == 'L':
                         t = 12
                 elif x == 'M':
                         t = 13
                 elif x == 'N':
                         t = 14
                 elif x == 'O':
                         t = 15
                 elif x == 'P':
                         t = 16
                 elif x == 'Q':
                         t = 17
                 elif x == 'R':
                         t = 18
                 elif x == 'S':
                         t = 19
                 elif x == 'T':
                         t = 20
                 elif x == 'U':
                         t = 21
                 elif x == 'V':
                         t = 22
                 elif x == 'W':
                         t = 23
                 elif x == 'X':
                         t = 24
                 elif x == 'Y':
                         t = 25
                 else:
                         t = 26
                 sum += t
                 ans += sum * i

                 sum = 0

 print(ans)
 if __name__ == '__main__':
         main()

Comments | Gallery