Sorted. Added 023-026 in Python.
This commit is contained in:
62
001.hs
62
001.hs
@@ -6,42 +6,46 @@ import qualified Data.Set as S
|
||||
problem_1 :: Int
|
||||
problem_1 = sum . nub $ [3,6..999] ++ [5,10..999]
|
||||
problem_1' = sum $ union [3,6..999] [5,10..999]
|
||||
main = problem_1'
|
||||
|
||||
-- bcs.h
|
||||
import Prelude hiding ((>>=), return)
|
||||
|
||||
-- 2 - all even primes lower 4 million
|
||||
problem_2 :: Int
|
||||
problem_2 = sum [x | x <- (takeWhile (<= 4000000) ((fix (\f x y -> x:f y (x+y))) 1 1)), even x]
|
||||
type Choice a = [a]
|
||||
|
||||
choose :: [a] -> Choice a
|
||||
choose xs = xs
|
||||
|
||||
-- 3 - largest prime factor
|
||||
-- problem_3 :: Integer -> Integer
|
||||
prims = fix (\f (x:xs) -> x:f (filter (\y -> rem y x /= 0) xs)) [2..]
|
||||
get_prims :: Integer -> [Integer]
|
||||
get_prims 1 = []
|
||||
get_prims n = let p = head $ dropWhile (\x -> rem n x /= 0) prims
|
||||
in p:get_prims (div n p)
|
||||
--next_prim xs = head $ dropWhile (\y -> any (\e -> rem y e == 0) xs) [last xs + 1..]
|
||||
--prims = filter (\x -> not $ any (\n -> rem x n == 0) [2..(x-1)]) [1..]
|
||||
--pfactors n = [x | x <- takeWhile (\y -> y*y <= n) [2..], n `mod` x == 0, null $ pfactors x]
|
||||
problem_3 :: Integer -> Integer
|
||||
problem_3 x = last $ get_prims x
|
||||
pair456 :: Int -> Choice (Int, Int)
|
||||
pair456 x = choose [(x, 4), (x, 5), (x, 6)]
|
||||
|
||||
join :: Choice (Choice a) -> Choice a
|
||||
join = concat
|
||||
|
||||
-- 10 - sum prime smaller 2 million
|
||||
eres :: Integral a => [a] -> [a]
|
||||
eres (x:xs) = if x*x < last xs then x:eres (filter (\y -> rem y x /= 0) xs) else (x:xs)
|
||||
problem_10 = sum $ eres [2..2000000]
|
||||
-- join $ map pair456 [1,2,3]
|
||||
-- [1, 2, 3] >>= pair456
|
||||
(>>=) :: Choice a -> (a -> Choice b) -> Choice b
|
||||
choices >>= f = join (map f choices)
|
||||
|
||||
return :: a -> Choice a
|
||||
return x = [x]
|
||||
|
||||
-- 11 - biggest multiple in 2d field
|
||||
parseSquare :: String -> [[Integer]]
|
||||
parseSquare = map (\line -> map read $ words line) . lines
|
||||
makePairs :: Choice (Int, Int)
|
||||
makePairs =
|
||||
choose [1, 2, 3] >>= (\x ->
|
||||
choose [4, 5, 6] >>= (\y ->
|
||||
return (x, y)))
|
||||
|
||||
columns :: [[a]] -> [[a]]
|
||||
columns [[]] = []
|
||||
columns xs = map
|
||||
mzero :: Choice a
|
||||
mzero = choose []
|
||||
|
||||
exercise_11 :: IO ()
|
||||
exercise_11 = do
|
||||
fileString <- readFile "problem_11.txt"
|
||||
putStrLn . show $ parseSquare fileString
|
||||
guard :: Bool -> Choice ()
|
||||
guard True = return ()
|
||||
guard False = mzero
|
||||
|
||||
makePairs' :: Choice (Int,Int)
|
||||
makePairs' = do
|
||||
x <- choose [1,2,3]
|
||||
y <- choose [4,5,6]
|
||||
guard (x*y == 8)
|
||||
return (x,y)
|
||||
|
||||
3
002.hs
Normal file
3
002.hs
Normal file
@@ -0,0 +1,3 @@
|
||||
-- 2 - all even primes lower 4 million
|
||||
problem_2 :: Int
|
||||
problem_2 = sum [x | x <- (takeWhile (<= 4000000) ((fix (\f x y -> x:f y (x+y))) 1 1)), even x]
|
||||
12
003.hs
Normal file
12
003.hs
Normal file
@@ -0,0 +1,12 @@
|
||||
-- 3 - largest prime factor
|
||||
-- problem_3 :: Integer -> Integer
|
||||
prims = fix (\f (x:xs) -> x:f (filter (\y -> rem y x /= 0) xs)) [2..]
|
||||
get_prims :: Integer -> [Integer]
|
||||
get_prims 1 = []
|
||||
get_prims n = let p = head $ dropWhile (\x -> rem n x /= 0) prims
|
||||
in p:get_prims (div n p)
|
||||
--next_prim xs = head $ dropWhile (\y -> any (\e -> rem y e == 0) xs) [last xs + 1..]
|
||||
--prims = filter (\x -> not $ any (\n -> rem x n == 0) [2..(x-1)]) [1..]
|
||||
--pfactors n = [x | x <- takeWhile (\y -> y*y <= n) [2..], n `mod` x == 0, null $ pfactors x]
|
||||
problem_3 :: Integer -> Integer
|
||||
problem_3 x = last $ get_prims x
|
||||
4
010.hs
Normal file
4
010.hs
Normal file
@@ -0,0 +1,4 @@
|
||||
-- 10 - sum prime smaller 2 million
|
||||
eres :: Integral a => [a] -> [a]
|
||||
eres (x:xs) = if x*x < last xs then x:eres (filter (\y -> rem y x /= 0) xs) else (x:xs)
|
||||
problem_10 = sum $ eres [2..2000000]
|
||||
12
011.hs
Normal file
12
011.hs
Normal file
@@ -0,0 +1,12 @@
|
||||
-- 11 - biggest multiple in 2d field
|
||||
parseSquare :: String -> [[Integer]]
|
||||
parseSquare = map (\line -> map read $ words line) . lines
|
||||
|
||||
columns :: [[a]] -> [[a]]
|
||||
columns [[]] = []
|
||||
columns xs = map
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
fileString <- readFile "problem_11.txt"
|
||||
putStrLn . show $ parseSquare fileString
|
||||
40
023.py
Normal file
40
023.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import math
|
||||
|
||||
|
||||
def get_proper_divisors(n):
|
||||
proper_divisors = set([1])
|
||||
for i in range(2, int(math.sqrt(n)) + 1):
|
||||
if n % i == 0:
|
||||
proper_divisors.add(i)
|
||||
proper_divisors.add(n / i)
|
||||
return proper_divisors
|
||||
|
||||
|
||||
def is_abundant(n):
|
||||
return sum(get_proper_divisors(n)) > n
|
||||
|
||||
|
||||
def get_abundant_numbers_smaller(n):
|
||||
ret = []
|
||||
for i in range(1, n):
|
||||
if is_abundant(i):
|
||||
ret.append(i)
|
||||
return ret
|
||||
|
||||
|
||||
def is_sum_of_two_abundant(n, abundant_numbers):
|
||||
abundant_numbers_set = set(abundant_numbers)
|
||||
for a1 in abundant_numbers:
|
||||
if a1 > n:
|
||||
return False
|
||||
elif (n - a1) in abundant_numbers_set:
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
abundant_numbers = get_abundant_numbers_smaller(30000)
|
||||
cannot_be_written_as_sum_of_abundant = []
|
||||
for i in range(28129):
|
||||
if not is_sum_of_two_abundant(i, abundant_numbers):
|
||||
cannot_be_written_as_sum_of_abundant.append(i)
|
||||
print(sum(cannot_be_written_as_sum_of_abundant))
|
||||
2
024.py
Normal file
2
024.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from itertools import permutations
|
||||
print("".join(list(permutations("0123456789"))[1000000-1]))
|
||||
109
025.py
Normal file
109
025.py
Normal file
@@ -0,0 +1,109 @@
|
||||
from copy import deepcopy
|
||||
from itertools import islice
|
||||
|
||||
|
||||
def primes(n):
|
||||
""" Nice way to calculate primes. Should be fast. """
|
||||
l = range(2, n + 1)
|
||||
_l = []
|
||||
while True:
|
||||
p = l[0]
|
||||
if p * p > n:
|
||||
return _l + l
|
||||
l = [i for i in l if i % p != 0]
|
||||
_l.append(p)
|
||||
|
||||
|
||||
def calculate_decimal_places(numerator, denominator):
|
||||
numerator = (numerator - (numerator / denominator) * denominator) * 10
|
||||
digits = []
|
||||
while True:
|
||||
digit = numerator / denominator
|
||||
numerator = (numerator - digit * denominator) * 10
|
||||
digits.append(digit)
|
||||
if digits[-3:] == [0, 0, 0]:
|
||||
raise StopIteration
|
||||
yield digit
|
||||
|
||||
|
||||
def has_cycle(decimal_places, n):
|
||||
d = decimal_places
|
||||
return list(islice(d, 0, n)) == list(islice(d, 0, n)) and \
|
||||
list(islice(d, 0, n)) == list(islice(d, 0, n))
|
||||
|
||||
|
||||
def f_025_1():
|
||||
""" Second try. I realized that only primes must be
|
||||
checked. Therefore, my brute force approach worked. """
|
||||
l = []
|
||||
for d in primes(1000):
|
||||
for i in range(5, 10000):
|
||||
decimal_places = calculate_decimal_places(1, d)
|
||||
if has_cycle(decimal_places, i):
|
||||
l.append((i, d))
|
||||
break
|
||||
print(max(l))
|
||||
|
||||
|
||||
def calculate_cycle(numerator, denominator):
|
||||
numerator = (numerator - (numerator / denominator) * denominator) * 10
|
||||
remainders = set([])
|
||||
while True:
|
||||
digit = numerator / denominator
|
||||
remainder = (numerator - digit * denominator)
|
||||
if remainder in remainders:
|
||||
raise StopIteration
|
||||
remainders.add(remainder)
|
||||
numerator = remainder * 10
|
||||
yield digit
|
||||
|
||||
|
||||
def f_025_2():
|
||||
""" Understood trick with remembering remainder... """
|
||||
s = [(len(list(calculate_cycle(1, d))), d)
|
||||
for d in range(1, 1001)]
|
||||
print(max(s))
|
||||
|
||||
|
||||
def f_025_3():
|
||||
""" Only testing primes... """
|
||||
s = [(len(list(calculate_cycle(1, d))), d)
|
||||
for d in primes(10000)]
|
||||
print(max(s))
|
||||
|
||||
|
||||
f_025_3()
|
||||
|
||||
|
||||
#print([(find_cycle_count(calculate_decimal_places(1, d)), d)
|
||||
# for d in range(1, 100)])
|
||||
|
||||
#print(find_cycle_count(calculate_decimal_places(22, 7)))
|
||||
|
||||
#l = []
|
||||
#for d in range(1, 1000):
|
||||
# for i in range(5, 1000):
|
||||
# decimal_places = calculate_decimal_places(1, d)
|
||||
# if has_cycle_one_off(decimal_places, i):
|
||||
# l.append((i, d))
|
||||
# break
|
||||
# decimal_places = calculate_decimal_places(1, d)
|
||||
# if has_cycle(decimal_places, i):
|
||||
# l.append((i, d))
|
||||
# break
|
||||
|
||||
#def find_cycle_count(decimal_places):
|
||||
# cycles = []
|
||||
# for digit in decimal_places:
|
||||
# new_cycles = []
|
||||
# for cycle in cycles:
|
||||
# digits, length = cycle
|
||||
# if digits[0] == digit:
|
||||
# if len(digits[1:]) == 0:
|
||||
# return length
|
||||
# new_cycles.append((digits[1:], length))
|
||||
# new_cycles.append((digits + [digit], length + 1))
|
||||
# new_cycles.append(([digit], 1))
|
||||
# cycles = new_cycles
|
||||
|
||||
|
||||
38
026.py
Normal file
38
026.py
Normal file
@@ -0,0 +1,38 @@
|
||||
def primes(n):
|
||||
""" Nice way to calculate primes. Should be fast. """
|
||||
l = range(2, n + 1)
|
||||
_l = []
|
||||
while True:
|
||||
p = l[0]
|
||||
if p * p > n:
|
||||
return _l + l
|
||||
l = [i for i in l if i % p != 0]
|
||||
_l.append(p)
|
||||
|
||||
|
||||
def produce_prime(a, b, n, primes):
|
||||
x = n*n + a*n + b
|
||||
return x in primes
|
||||
|
||||
|
||||
|
||||
def f_027():
|
||||
""" n^2 + a*n + b
|
||||
1) b must be prime
|
||||
"""
|
||||
p6 = set(primes(1000000))
|
||||
p3 = primes(1000)
|
||||
options = [(a, b)
|
||||
for a in range(1, 1000, 2)
|
||||
for b in p3]
|
||||
|
||||
print(len(options))
|
||||
for n in range(100):
|
||||
options = [(a, b)
|
||||
for a, b in options
|
||||
if produce_prime(a, b, n, p6)]
|
||||
print(options)
|
||||
print(len(options))
|
||||
|
||||
|
||||
f_027()
|
||||
40
bcs.hs
40
bcs.hs
@@ -1,40 +0,0 @@
|
||||
import Prelude hiding ((>>=), return)
|
||||
|
||||
type Choice a = [a]
|
||||
|
||||
choose :: [a] -> Choice a
|
||||
choose xs = xs
|
||||
|
||||
pair456 :: Int -> Choice (Int, Int)
|
||||
pair456 x = choose [(x, 4), (x, 5), (x, 6)]
|
||||
|
||||
join :: Choice (Choice a) -> Choice a
|
||||
join = concat
|
||||
|
||||
-- join $ map pair456 [1,2,3]
|
||||
-- [1, 2, 3] >>= pair456
|
||||
(>>=) :: Choice a -> (a -> Choice b) -> Choice b
|
||||
choices >>= f = join (map f choices)
|
||||
|
||||
return :: a -> Choice a
|
||||
return x = [x]
|
||||
|
||||
makePairs :: Choice (Int, Int)
|
||||
makePairs =
|
||||
choose [1, 2, 3] >>= (\x ->
|
||||
choose [4, 5, 6] >>= (\y ->
|
||||
return (x, y)))
|
||||
|
||||
mzero :: Choice a
|
||||
mzero = choose []
|
||||
|
||||
guard :: Bool -> Choice ()
|
||||
guard True = return ()
|
||||
guard False = mzero
|
||||
|
||||
makePairs' :: Choice (Int,Int)
|
||||
makePairs' = do
|
||||
x <- choose [1,2,3]
|
||||
y <- choose [4,5,6]
|
||||
guard (x*y == 8)
|
||||
return (x,y)
|
||||
Reference in New Issue
Block a user