import java . util . Arrays ; public final class p225 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p225 ( ) . run ( ) ) ; } private static final int INDEX = 124 ; public String run ( ) { int count = 0 ; for ( int i = 1 ; ; i += 2 ) { if ( ! hasTribonacciMultiple ( i ) ) { count ++ ; if ( count == INDEX ) return Integer . toString ( i ) ; } } } private static boolean hasTribonacciMultiple ( int modulus ) { int [ ] slow = { 1 , 1 , 1 } ; int [ ] fast = slow . clone ( ) ; for ( boolean head = true ; ; head = false ) { if ( slow [ 0 ] % modulus == 0 ) return true ; if ( ! head && Arrays . equals ( slow , fast ) ) return false ; tribonacci ( slow , modulus ) ; tribonacci ( fast , modulus ) ; tribonacci ( fast , modulus ) ; } } private static void tribonacci ( int [ ] state , int mod ) { int a = state [ 0 ] ; int b = state [ 1 ] ; int c = state [ 2 ] ; state [ 0 ] = b ; state [ 1 ] = c ; state [ 2 ] = ( a + b + c ) % mod ; } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT INDEX = 124 NEW_LINE stream = ( i for i in itertools . count ( 1 , 2 ) if not has_tribonacci_multiple ( i ) ) NEW_LINE ans = next ( itertools . islice ( stream , INDEX - 1 , None ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def has_tribonacci_multiple ( i ) : NEW_LINE INDENT seen = set ( ) NEW_LINE a , b , c = 1 , 1 , 1 NEW_LINE while True : NEW_LINE INDENT key = ( a , b , c ) NEW_LINE if key in seen : NEW_LINE INDENT return False NEW_LINE DEDENT seen . add ( key ) NEW_LINE if a % i == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT a , b , c = b , c , ( a + b + c ) % i NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . ArrayList ; import java . util . Arrays ; import java . util . Collections ; import java . util . HashMap ; import java . util . List ; import java . util . Map ; public final class p151 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p151 ( ) . run ( ) ) ; } public String run ( ) { List < Integer > startState = Arrays . asList ( 1 ) ; return String . format ( " % .6f " , getExpectedSingles ( startState ) - 2 ) ; } private Map < List < Integer > , Double > expectedSingles = new HashMap < > ( ) ; private double getExpectedSingles ( List < Integer > state ) { if ( expectedSingles . containsKey ( state ) ) return expectedSingles . get ( state ) ; double result = 0 ; if ( ! state . isEmpty ( ) ) { for ( int i = 0 ; i < state . size ( ) ; i ++ ) { List < Integer > newState = new ArrayList < > ( state ) ; int sheet = state . get ( i ) ; newState . remove ( i ) ; for ( int j = sheet + 1 ; j <= 5 ; j ++ ) newState . add ( j ) ; Collections . sort ( newState ) ; result += getExpectedSingles ( newState ) ; } result /= state . size ( ) ; if ( state . size ( ) == 1 ) result ++ ; } expectedSingles . put ( state , result ) ; return result ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT ans = get_expected_singles ( ( 1 , ) ) - 2 NEW_LINE return f " { ans : .6f } " NEW_LINE DEDENT @ eulerlib . memoize NEW_LINE def get_expected_singles ( state ) : NEW_LINE INDENT result = 0.0 NEW_LINE if len ( state ) > 0 : NEW_LINE INDENT for i in range ( len ( state ) ) : NEW_LINE INDENT tempstate = list ( state ) NEW_LINE sheet = state [ i ] NEW_LINE del tempstate [ i ] NEW_LINE for j in range ( sheet + 1 , 6 ) : NEW_LINE INDENT tempstate . append ( j ) NEW_LINE DEDENT tempstate . sort ( ) NEW_LINE result += get_expected_singles ( tuple ( tempstate ) ) NEW_LINE DEDENT result /= len ( state ) NEW_LINE if len ( state ) == 1 : NEW_LINE INDENT result += 1.0 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . Arrays ; public final class p187 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p187 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 8 ) - 1 ; public String run ( ) { int count = 0 ; int [ ] primes = Library . listPrimes ( LIMIT / 2 ) ; for ( int i = 0 , sqrt = Library . sqrt ( LIMIT ) ; i < primes . length && primes [ i ] <= sqrt ; i ++ ) { int end = Arrays . binarySearch ( primes , LIMIT / primes [ i ] ) ; if ( end >= 0 ) end ++ ; else end = - end - 1 ; count += end - i ; } return Integer . toString ( count ) ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 8 - 1 NEW_LINE ans = 0 NEW_LINE primes = eulerlib . list_primes ( LIMIT // 2 ) NEW_LINE sqrt = eulerlib . sqrt ( LIMIT ) NEW_LINE for ( i , p ) in enumerate ( primes ) : NEW_LINE INDENT if p > sqrt : NEW_LINE INDENT break NEW_LINE DEDENT end = binary_search ( primes , LIMIT // p ) NEW_LINE ans += ( end + 1 if end >= 0 else - end - 1 ) - i NEW_LINE DEDENT return str ( ans ) NEW_LINE DEDENT def binary_search ( lst , x ) : NEW_LINE INDENT start = 0 NEW_LINE end = len ( lst ) NEW_LINE while start < end : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE if x < lst [ mid ] : NEW_LINE INDENT end = mid NEW_LINE DEDENT elif x > lst [ mid ] : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT elif x == lst [ mid ] : NEW_LINE INDENT return mid NEW_LINE DEDENT else : NEW_LINE INDENT raise AssertionError ( ) NEW_LINE DEDENT DEDENT return - start - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; public final class p133 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p133 ( ) . run ( ) ) ; } public String run ( ) { long sum = 0 ; for ( int p : Library . listPrimes ( 100000 ) ) { if ( p == 2 || p == 5 || ! hasDivisibleRepunit ( p ) ) sum += p ; } return Long . toString ( sum ) ; } private static final BigInteger EXPONENT = BigInteger . TEN . pow ( 16 ) ; private static boolean hasDivisibleRepunit ( int p ) { return ( BigInteger . TEN . modPow ( EXPONENT , BigInteger . valueOf ( p * 9 ) ) . intValue ( ) - 1 ) / 9 % p == 0 ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT primes = eulerlib . list_primes ( 100000 ) NEW_LINE ans = sum ( p for p in primes if p == 2 or p == 5 or not has_divisible_repunit ( p ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def has_divisible_repunit ( p ) : NEW_LINE INDENT return ( pow ( 10 , 10 ** 16 , p * 9 ) - 1 ) // 9 % p == 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . PriorityQueue ; import java . util . Queue ; public final class p500 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p500 ( ) . run ( ) ) ; } private static final int TARGET = 500500 ; private static final long MODULUS = 500500507 ; public String run ( ) { Queue < Long > queue = new PriorityQueue < > ( ) ; int nextPrime = 2 ; queue . add ( ( long ) nextPrime ) ; long product = 1 ; for ( int i = 0 ; i < TARGET ; i ++ ) { long item = queue . remove ( ) ; product *= item % MODULUS ; product %= MODULUS ; queue . add ( item * item ) ; if ( item == nextPrime ) { do nextPrime ++ ; while ( ! Library . isPrime ( nextPrime ) ) ; queue . add ( ( long ) nextPrime ) ; } } return Long . toString ( product ) ; } }

import eulerlib , heapq NEW_LINE def compute ( ) : NEW_LINE INDENT TARGET = 500500 NEW_LINE MODULUS = 500500507 NEW_LINE isprime = eulerlib . list_primality ( 7376507 ) NEW_LINE queue = [ ] NEW_LINE nextprime = 2 NEW_LINE heapq . heappush ( queue , nextprime ) NEW_LINE ans = 1 NEW_LINE for _ in range ( TARGET ) : NEW_LINE INDENT item = heapq . heappop ( queue ) NEW_LINE ans *= item NEW_LINE ans %= MODULUS NEW_LINE heapq . heappush ( queue , item ** 2 ) NEW_LINE if item == nextprime : NEW_LINE INDENT nextprime += 1 NEW_LINE while not isprime [ nextprime ] : NEW_LINE INDENT nextprime += 1 NEW_LINE DEDENT heapq . heappush ( queue , nextprime ) NEW_LINE DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p429 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p429 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 8 ) ; private static final int MODULUS = 1000000009 ; public String run ( ) { int [ ] primes = Library . listPrimes ( LIMIT ) ; long sum = 1 ; for ( int p : primes ) { int power = countFactors ( LIMIT , p ) ; sum *= 1 + Library . powMod ( p , power * 2 , MODULUS ) ; sum %= MODULUS ; } return Long . toString ( sum ) ; } private static int countFactors ( int n , int p ) { if ( n == 0 ) return 0 ; else return n / p + countFactors ( n / p , p ) ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 8 NEW_LINE MOD = 1000000009 NEW_LINE ans = 1 NEW_LINE for p in eulerlib . prime_generator ( LIMIT ) : NEW_LINE INDENT power = count_factors ( LIMIT , p ) NEW_LINE ans *= 1 + pow ( p , power * 2 , MOD ) NEW_LINE ans %= MOD NEW_LINE DEDENT return str ( ans ) NEW_LINE DEDENT def count_factors ( n , p ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return n // p + count_factors ( n // p , p ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p007 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p007 ( ) . run ( ) ) ; } public String run ( ) { for ( int i = 2 , count = 0 ; ; i ++ ) { if ( Library . isPrime ( i ) ) { count ++ ; if ( count == 10001 ) return Integer . toString ( i ) ; } } } }

import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT ans = next ( itertools . islice ( filter ( eulerlib . is_prime , itertools . count ( 2 ) ) , 10000 , None ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . Arrays ; public final class p052 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p052 ( ) . run ( ) ) ; } public String run ( ) { for ( int i = 1 ; ; i ++ ) { if ( i > Integer . MAX_VALUE / 6 ) throw new ArithmeticException ( " Overflow " ) ; if ( multiplesHaveSameDigits ( i ) ) return Integer . toString ( i ) ; } } private static boolean multiplesHaveSameDigits ( int x ) { for ( int i = 2 ; i <= 6 ; i ++ ) { if ( ! Arrays . equals ( toSortedDigits ( x ) , toSortedDigits ( i * x ) ) ) return false ; } return true ; } private static char [ ] toSortedDigits ( int x ) { char [ ] result = Integer . toString ( x ) . toCharArray ( ) ; Arrays . sort ( result ) ; return result ; } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT cond = lambda i : all ( sorted ( str ( i ) ) == sorted ( str ( j * i ) ) for j in range ( 2 , 7 ) ) NEW_LINE ans = next ( i for i in itertools . count ( 1 ) if cond ( i ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p043 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p043 ( ) . run ( ) ) ; } private static int [ ] DIVISIBILITY_TESTS = { 2 , 3 , 5 , 7 , 11 , 13 , 17 } ; public String run ( ) { long sum = 0 ; int [ ] digits = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; outer : do { for ( int i = 0 ; i < DIVISIBILITY_TESTS . length ; i ++ ) { if ( toInteger ( digits , i + 1 , 3 ) % DIVISIBILITY_TESTS [ i ] != 0 ) continue outer ; } sum += toInteger ( digits , 0 , digits . length ) ; } while ( Library . nextPermutation ( digits ) ) ; return Long . toString ( sum ) ; } private static long toInteger ( int [ ] digits , int off , int len ) { long result = 0 ; for ( int i = off ; i < off + len ; i ++ ) result = result * 10 + digits [ i ] ; return result ; } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT ans = sum ( int ( " " . join ( map ( str , num ) ) ) for num in itertools . permutations ( list ( range ( 10 ) ) ) if is_substring_divisible ( num ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT DIVISIBILITY_TESTS = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 ] NEW_LINE def is_substring_divisible ( num ) : NEW_LINE INDENT return all ( ( num [ i + 1 ] * 100 + num [ i + 2 ] * 10 + num [ i + 3 ] ) % p == 0 for ( i , p ) in enumerate ( DIVISIBILITY_TESTS ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . HashSet ; import java . util . Set ; public final class p075 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p075 ( ) . run ( ) ) ; } private static final int LIMIT = 1500000 ; public String run ( ) { Set < IntTriple > triples = new HashSet < > ( ) ; for ( int s = 3 ; s * s <= LIMIT ; s += 2 ) { for ( int t = s - 2 ; t > 0 ; t -= 2 ) { if ( Library . gcd ( s , t ) == 1 ) { int a = s * t ; int b = ( s * s - t * t ) / 2 ; int c = ( s * s + t * t ) / 2 ; if ( a + b + c <= LIMIT ) triples . add ( new IntTriple ( a , b , c ) ) ; } } } byte [ ] ways = new byte [ LIMIT + 1 ] ; for ( IntTriple triple : triples ) { int sum = triple . a + triple . b + triple . c ; for ( int i = sum ; i < ways . length ; i += sum ) ways [ i ] = ( byte ) Math . min ( ways [ i ] + 1 , 2 ) ; } int count = 0 ; for ( int x : ways ) { if ( x == 1 ) count ++ ; } return Integer . toString ( count ) ; } private static final class IntTriple { public final int a ; public final int b ; public final int c ; public IntTriple ( int a , int b , int c ) { this . a = a ; this . b = b ; this . c = c ; } public boolean equals ( Object obj ) { if ( ! ( obj instanceof IntTriple ) ) return false ; else { IntTriple other = ( IntTriple ) obj ; return a == other . a && b == other . b && c == other . c ; } } public int hashCode ( ) { return a + b + c ; } } }

import eulerlib , fractions NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 1500000 NEW_LINE triples = set ( ) NEW_LINE for s in range ( 3 , eulerlib . sqrt ( LIMIT ) + 1 , 2 ) : NEW_LINE INDENT for t in range ( s - 2 , 0 , - 2 ) : NEW_LINE INDENT if fractions . gcd ( s , t ) == 1 : NEW_LINE INDENT a = s * t NEW_LINE b = ( s * s - t * t ) // 2 NEW_LINE c = ( s * s + t * t ) // 2 NEW_LINE if a + b + c <= LIMIT : NEW_LINE INDENT triples . add ( ( a , b , c ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT ways = [ 0 ] * ( LIMIT + 1 ) NEW_LINE for triple in triples : NEW_LINE INDENT sigma = sum ( triple ) NEW_LINE for i in range ( sigma , len ( ways ) , sigma ) : NEW_LINE INDENT ways [ i ] += 1 NEW_LINE DEDENT DEDENT ans = ways . count ( 1 ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p205 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p205 ( ) . run ( ) ) ; } private static final int [ ] PYRAMIDAL_DIE_PDF = { 0 , 1 , 1 , 1 , 1 } ; private static final int [ ] CUBIC_DIE_PDF = { 0 , 1 , 1 , 1 , 1 , 1 , 1 } ; public String run ( ) { int [ ] ninePyramidalPdf = { 1 } ; for ( int i = 0 ; i < 9 ; i ++ ) ninePyramidalPdf = convolve ( ninePyramidalPdf , PYRAMIDAL_DIE_PDF ) ; int [ ] sixCubicPdf = { 1 } ; for ( int i = 0 ; i < 6 ; i ++ ) sixCubicPdf = convolve ( sixCubicPdf , CUBIC_DIE_PDF ) ; long numer = 0 ; for ( int i = 0 ; i < ninePyramidalPdf . length ; i ++ ) numer += ( long ) ninePyramidalPdf [ i ] * sum ( sixCubicPdf , 0 , i ) ; long denom = ( long ) sum ( ninePyramidalPdf , 0 , ninePyramidalPdf . length ) * sum ( sixCubicPdf , 0 , sixCubicPdf . length ) ; return String . format ( " % .7f " , ( double ) numer / denom ) ; } private static int [ ] convolve ( int [ ] a , int [ ] b ) { int [ ] c = new int [ a . length + b . length - 1 ] ; for ( int i = 0 ; i < a . length ; i ++ ) { for ( int j = 0 ; j < b . length ; j ++ ) c [ i + j ] += a [ i ] * b [ j ] ; } return c ; } private static int sum ( int [ ] array , int start , int end ) { int sum = 0 ; for ( int i = start ; i < end ; i ++ ) sum += array [ i ] ; return sum ; } }

def compute ( ) : NEW_LINE INDENT nine_pyramidal_pdf = [ 1 ] NEW_LINE PYRAMIDAL_DIE_PDF = [ 0 , 1 , 1 , 1 , 1 ] NEW_LINE for i in range ( 9 ) : NEW_LINE INDENT nine_pyramidal_pdf = convolve ( nine_pyramidal_pdf , PYRAMIDAL_DIE_PDF ) NEW_LINE DEDENT six_cubic_pdf = [ 1 ] NEW_LINE CUBIC_DIE_PDF = [ 0 , 1 , 1 , 1 , 1 , 1 , 1 ] NEW_LINE for i in range ( 6 ) : NEW_LINE INDENT six_cubic_pdf = convolve ( six_cubic_pdf , CUBIC_DIE_PDF ) NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( len ( nine_pyramidal_pdf ) ) : NEW_LINE INDENT ans += nine_pyramidal_pdf [ i ] * sum ( six_cubic_pdf [ : i ] ) NEW_LINE DEDENT ans = float ( ans ) / ( sum ( nine_pyramidal_pdf ) * sum ( six_cubic_pdf ) ) NEW_LINE return f " { ans : .7f } " NEW_LINE DEDENT def convolve ( a , b ) : NEW_LINE INDENT c = [ 0 ] * ( len ( a ) + len ( b ) - 1 ) NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT for j in range ( len ( b ) ) : NEW_LINE INDENT c [ i + j ] += a [ i ] * b [ j ] NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p348 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p348 ( ) . run ( ) ) ; } private static final int TARGET_WAYS = 4 ; private static final int TARGET_COUNT = 5 ; static { assert 0 <= TARGET_COUNT ; assert 0 <= TARGET_WAYS && TARGET_WAYS <= Byte . MAX_VALUE - 1 ; } public String run ( ) { for ( long limit = 1 ; ; limit *= 10 ) { if ( limit > Integer . MAX_VALUE ) throw new AssertionError ( " Overflow " ) ; long answer = trySearch ( ( int ) limit ) ; if ( answer != - 1 ) return Long . toString ( answer ) ; } } private static long trySearch ( int limit ) { byte [ ] ways = new byte [ limit ] ; for ( int i = cbrt ( limit - 1 ) ; i > 1 ; i -- ) { int cube = i * i * i ; for ( int j = Library . sqrt ( limit - 1 - cube ) ; j > 1 ; j -- ) { int index = cube + j * j ; ways [ index ] = ( byte ) Math . min ( ways [ index ] + 1 , TARGET_WAYS + 1 ) ; } } long result = 0 ; int count = 0 ; for ( int i = 0 ; i < ways . length ; i ++ ) { if ( ways [ i ] == TARGET_WAYS && Library . isPalindrome ( i ) ) { result += i ; count ++ ; if ( count == TARGET_COUNT ) return result ; } } return - 1 ; } private static int cbrt ( int x ) { if ( x < 0 ) throw new IllegalArgumentException ( " Not ▁ implemented " ) ; int y = 0 ; for ( int i = 1 << 10 ; i != 0 ; i >>>= 1 ) { y |= i ; if ( y > 1290 || y * y * y > x ) y ^= i ; } return y ; } }

import eulerlib , itertools NEW_LINE TARGET_WAYS = 4 NEW_LINE TARGET_COUNT = 5 NEW_LINE def compute ( ) : NEW_LINE INDENT for i in itertools . count ( ) : NEW_LINE INDENT limit = 10 ** i NEW_LINE ans = try_search ( limit ) NEW_LINE if ans is not None : NEW_LINE INDENT return str ( ans ) NEW_LINE DEDENT DEDENT DEDENT def try_search ( limit ) : NEW_LINE INDENT ways = { } NEW_LINE for i in itertools . count ( 2 ) : NEW_LINE INDENT cube = i ** 3 NEW_LINE if cube >= limit : NEW_LINE INDENT break NEW_LINE DEDENT for j in range ( 2 , eulerlib . sqrt ( limit - 1 - cube ) + 1 ) : NEW_LINE INDENT index = cube + j ** 2 NEW_LINE ways [ index ] = ways . get ( index , 0 ) + 1 NEW_LINE DEDENT DEDENT result = 0 NEW_LINE count = 0 NEW_LINE for i in sorted ( ways . keys ( ) ) : NEW_LINE INDENT if ways [ i ] == TARGET_WAYS and is_palindrome ( i ) : NEW_LINE INDENT result += i NEW_LINE count += 1 NEW_LINE if count == TARGET_COUNT : NEW_LINE INDENT return result NEW_LINE DEDENT DEDENT DEDENT return None NEW_LINE DEDENT def is_palindrome ( x ) : NEW_LINE INDENT s = str ( x ) NEW_LINE return s == s [ : : - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p078 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p078 ( ) . run ( ) ) ; } private static final int MODULUS = Library . pow ( 10 , 6 ) ; public String run ( ) { for ( int limit = 1 ; ; limit *= 2 ) { int result = search ( limit ) ; if ( result != - 1 ) return Integer . toString ( result ) ; } } private static int search ( int limit ) { int [ ] partitions = new int [ limit ] ; partitions [ 0 ] = 1 ; for ( int i = 1 ; i < limit ; i ++ ) { for ( int j = i ; j < limit ; j ++ ) partitions [ j ] = ( partitions [ j ] + partitions [ j - i ] ) % MODULUS ; } for ( int i = 0 ; i < limit ; i ++ ) { if ( partitions [ i ] == 0 ) return i ; } return - 1 ; } }

import itertools NEW_LINE MODULUS = 10 ** 6 NEW_LINE def compute ( ) : NEW_LINE INDENT partitions = [ 1 ] NEW_LINE for i in itertools . count ( len ( partitions ) ) : NEW_LINE INDENT item = 0 NEW_LINE for j in itertools . count ( 1 ) : NEW_LINE INDENT sign = - 1 if j % 2 == 0 else + 1 NEW_LINE index = ( j * j * 3 - j ) // 2 NEW_LINE if index > i : NEW_LINE INDENT break NEW_LINE DEDENT item += partitions [ i - index ] * sign NEW_LINE index += j NEW_LINE if index > i : NEW_LINE INDENT break NEW_LINE DEDENT item += partitions [ i - index ] * sign NEW_LINE item %= MODULUS NEW_LINE DEDENT if item == 0 : NEW_LINE INDENT return str ( i ) NEW_LINE DEDENT partitions . append ( item ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p173 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p173 ( ) . run ( ) ) ; } private static final int TILES = 1000000 ; public String run ( ) { int count = 0 ; for ( int n = 3 ; n <= TILES / 4 + 1 ; n ++ ) { for ( int k = n - 2 ; k >= 1 ; k -= 2 ) { if ( ( long ) n * n - ( long ) k * k > TILES ) break ; count ++ ; } } return Integer . toString ( count ) ; } }

def compute ( ) : NEW_LINE INDENT TILES = 10 ** 6 NEW_LINE ans = 0 NEW_LINE for n in range ( 3 , TILES // 4 + 2 ) : NEW_LINE INDENT for k in range ( n - 2 , 0 , - 2 ) : NEW_LINE INDENT if n * n - k * k > TILES : NEW_LINE INDENT break NEW_LINE DEDENT ans += 1 NEW_LINE DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . HashMap ; import java . util . Map ; public final class p026 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p026 ( ) . run ( ) ) ; } public String run ( ) { int bestNumber = 0 ; int bestLength = 0 ; for ( int i = 1 ; i <= 1000 ; i ++ ) { int len = getCycleLength ( i ) ; if ( len > bestLength ) { bestNumber = i ; bestLength = len ; } } return Integer . toString ( bestNumber ) ; } private static int getCycleLength ( int n ) { Map < Integer , Integer > stateToIter = new HashMap < > ( ) ; int state = 1 ; for ( int iter = 0 ; ; iter ++ ) { if ( stateToIter . containsKey ( state ) ) return iter - stateToIter . get ( state ) ; else { stateToIter . put ( state , iter ) ; state = state * 10 % n ; } } } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT ans = max ( range ( 1 , 1000 ) , key = reciprocal_cycle_len ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def reciprocal_cycle_len ( n ) : NEW_LINE INDENT seen = { } NEW_LINE x = 1 NEW_LINE for i in itertools . count ( ) : NEW_LINE INDENT if x in seen : NEW_LINE INDENT return i - seen [ x ] NEW_LINE DEDENT else : NEW_LINE INDENT seen [ x ] = i NEW_LINE x = x * 10 % n NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p135 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p135 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 6 ) ; public String run ( ) { int [ ] solutions = new int [ LIMIT ] ; for ( int m = 1 ; m < LIMIT * 2 ; m ++ ) { for ( int k = m / 5 + 1 ; k * 2 < m ; k ++ ) { long temp = ( long ) ( m - k ) * ( k * 5 - m ) ; if ( temp >= solutions . length ) break ; solutions [ ( int ) temp ] ++ ; } } int count = 0 ; for ( int x : solutions ) { if ( x == 10 ) count ++ ; } return Integer . toString ( count ) ; } }

def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 6 NEW_LINE solutions = [ 0 ] * LIMIT NEW_LINE for m in range ( 1 , LIMIT * 2 ) : NEW_LINE INDENT for k in range ( m // 5 + 1 , ( m + 1 ) // 2 ) : NEW_LINE INDENT temp = ( m - k ) * ( k * 5 - m ) NEW_LINE if temp >= LIMIT : NEW_LINE INDENT break NEW_LINE DEDENT solutions [ temp ] += 1 NEW_LINE DEDENT DEDENT ans = solutions . count ( 10 ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p139 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p139 ( ) . run ( ) ) ; } private static final int LIMIT = 100000000 ; public String run ( ) { int count = 0 ; for ( int s = 3 ; s * s / 2 < LIMIT ; s += 2 ) { for ( int t = 1 ; t < s ; t += 2 ) { int a = s * t ; int b = ( s * s - t * t ) / 2 ; int c = ( s * s + t * t ) / 2 ; int p = a + b + c ; if ( p >= LIMIT ) break ; if ( c % ( a - b ) == 0 && Library . gcd ( s , t ) == 1 ) count += ( LIMIT - 1 ) / p ; } } return Integer . toString ( count ) ; } }

import eulerlib , fractions NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 100000000 NEW_LINE ans = 0 NEW_LINE for s in range ( 3 , eulerlib . sqrt ( LIMIT * 2 ) , 2 ) : NEW_LINE INDENT for t in range ( 1 , s , 2 ) : NEW_LINE INDENT a = s * t NEW_LINE b = ( s * s - t * t ) // 2 NEW_LINE c = ( s * s + t * t ) // 2 NEW_LINE p = a + b + c NEW_LINE if p >= LIMIT : NEW_LINE INDENT break NEW_LINE DEDENT if c % ( a - b ) == 0 and fractions . gcd ( s , t ) == 1 : NEW_LINE INDENT ans += ( LIMIT - 1 ) // p NEW_LINE DEDENT DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p073 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p073 ( ) . run ( ) ) ; } public String run ( ) { return Integer . toString ( sternBrocotCount ( 1 , 3 , 1 , 2 ) ) ; } private static int sternBrocotCount ( int leftN , int leftD , int rightN , int rightD ) { int n = leftN + rightN ; int d = leftD + rightD ; if ( d > 12000 ) return 0 ; else return 1 + sternBrocotCount ( leftN , leftD , n , d ) + sternBrocotCount ( n , d , rightN , rightD ) ; } }

def compute ( ) : NEW_LINE INDENT ans = 0 NEW_LINE stack = [ ( 1 , 3 , 1 , 2 ) ] NEW_LINE while len ( stack ) > 0 : NEW_LINE INDENT leftn , leftd , rightn , rightd = stack . pop ( ) NEW_LINE d = leftd + rightd NEW_LINE if d <= 12000 : NEW_LINE INDENT n = leftn + rightn NEW_LINE ans += 1 NEW_LINE stack . append ( ( n , d , rightn , rightd ) ) NEW_LINE stack . append ( ( leftn , leftd , n , d ) ) NEW_LINE DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; import java . util . Arrays ; import java . util . HashMap ; import java . util . Map ; public final class p062 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p062 ( ) . run ( ) ) ; } public String run ( ) { int numDigits = 0 ; Map < String , Integer > lowest = new HashMap < > ( ) ; Map < String , Integer > counts = new HashMap < > ( ) ; for ( int i = 0 ; ; i ++ ) { String numClass = getCubeNumberClass ( i ) ; if ( numClass . length ( ) > numDigits ) { int min = Integer . MAX_VALUE ; for ( String nc : counts . keySet ( ) ) { if ( counts . get ( nc ) == 5 ) min = Math . min ( lowest . get ( nc ) , min ) ; } if ( min != Integer . MAX_VALUE ) return cube ( min ) . toString ( ) ; lowest . clear ( ) ; counts . clear ( ) ; numDigits = numClass . length ( ) ; } if ( ! lowest . containsKey ( numClass ) ) { lowest . put ( numClass , i ) ; counts . put ( numClass , 0 ) ; } counts . put ( numClass , counts . get ( numClass ) + 1 ) ; } } private static String getCubeNumberClass ( int x ) { char [ ] digits = cube ( x ) . toString ( ) . toCharArray ( ) ; Arrays . sort ( digits ) ; return new String ( digits ) ; } private static BigInteger cube ( int x ) { return BigInteger . valueOf ( x ) . pow ( 3 ) ; } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT numdigits = 0 NEW_LINE data = { } NEW_LINE for i in itertools . count ( ) : NEW_LINE INDENT digits = [ int ( c ) for c in str ( i ** 3 ) ] NEW_LINE digits . sort ( ) NEW_LINE numclass = " " . join ( str ( d ) for d in digits ) NEW_LINE if len ( numclass ) > numdigits : NEW_LINE INDENT candidates = [ lowest for ( lowest , count ) in data . values ( ) if count == 5 ] NEW_LINE if len ( candidates ) > 0 : NEW_LINE INDENT return str ( min ( candidates ) ** 3 ) NEW_LINE DEDENT data = { } NEW_LINE numdigits = len ( numclass ) NEW_LINE DEDENT lowest , count = data . get ( numclass , ( i , 0 ) ) NEW_LINE data [ numclass ] = ( lowest , count + 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p214 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p214 ( ) . run ( ) ) ; } private static final int LIMIT = 40000000 ; public String run ( ) { int [ ] totient = Library . listTotients ( LIMIT - 1 ) ; int [ ] totientChainLength = new int [ totient . length ] ; totientChainLength [ 0 ] = 0 ; long sum = 0 ; for ( int i = 1 ; i < totient . length ; i ++ ) { int chainlen = totientChainLength [ totient [ i ] ] + 1 ; totientChainLength [ i ] = chainlen ; if ( chainlen == 25 && totient [ i ] == i - 1 ) sum += i ; } return Long . toString ( sum ) ; } }

import array NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 40000000 NEW_LINE totient = list_totients ( LIMIT - 1 ) NEW_LINE totientchainlen = array . array ( " L " , [ 0 , 1 ] ) NEW_LINE ans = 0 NEW_LINE for i in range ( len ( totientchainlen ) , len ( totient ) ) : NEW_LINE INDENT chainlen = totientchainlen [ totient [ i ] ] + 1 NEW_LINE totientchainlen . append ( chainlen ) NEW_LINE if chainlen == 25 and totient [ i ] == i - 1 : NEW_LINE INDENT ans += i NEW_LINE DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT def list_totients ( n ) : NEW_LINE INDENT assert n < ( 1 << 32 ) NEW_LINE result = array . array ( " L " , range ( n + 1 ) ) NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if result [ i ] == i : NEW_LINE INDENT for j in range ( i , n + 1 , i ) : NEW_LINE INDENT result [ j ] = result [ j ] // i * ( i - 1 ) NEW_LINE DEDENT DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p041 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p041 ( ) . run ( ) ) ; } public String run ( ) { for ( int n = 9 ; n >= 1 ; n -- ) { int [ ] digits = new int [ n ] ; for ( int i = 0 ; i < digits . length ; i ++ ) digits [ i ] = i + 1 ; int result = - 1 ; do { if ( Library . isPrime ( toInteger ( digits ) ) ) result = toInteger ( digits ) ; } while ( Library . nextPermutation ( digits ) ) ; if ( result != - 1 ) return Integer . toString ( result ) ; } throw new RuntimeException ( " Not ▁ found " ) ; } private static int toInteger ( int [ ] digits ) { int result = 0 ; for ( int x : digits ) result = result * 10 + x ; return result ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT for n in reversed ( range ( 2 , 10 ) ) : NEW_LINE INDENT arr = list ( reversed ( range ( 1 , n + 1 ) ) ) NEW_LINE while True : NEW_LINE INDENT if arr [ - 1 ] not in NONPRIME_LAST_DIGITS : NEW_LINE INDENT n = int ( " " . join ( str ( x ) for x in arr ) ) NEW_LINE if eulerlib . is_prime ( n ) : NEW_LINE INDENT return str ( n ) NEW_LINE DEDENT DEDENT if not prev_permutation ( arr ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT raise AssertionError ( ) NEW_LINE DEDENT NONPRIME_LAST_DIGITS = { 0 , 2 , 4 , 5 , 6 , 8 } NEW_LINE def prev_permutation ( arr ) : NEW_LINE INDENT i = len ( arr ) - 1 NEW_LINE while i > 0 and arr [ i - 1 ] <= arr [ i ] : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT if i <= 0 : NEW_LINE INDENT return False NEW_LINE DEDENT j = len ( arr ) - 1 NEW_LINE while arr [ j ] >= arr [ i - 1 ] : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT arr [ i - 1 ] , arr [ j ] = arr [ j ] , arr [ i - 1 ] NEW_LINE arr [ i : ] = arr [ len ( arr ) - 1 : i - 1 : - 1 ] NEW_LINE return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p019 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p019 ( ) . run ( ) ) ; } public String run ( ) { int count = 0 ; for ( int y = 1901 ; y <= 2000 ; y ++ ) { for ( int m = 1 ; m <= 12 ; m ++ ) { if ( dayOfWeek ( y , m , 1 ) == 0 ) count ++ ; } } return Integer . toString ( count ) ; } private static int dayOfWeek ( int year , int month , int day ) { if ( year < 0 || year > 10000 || month < 1 || month > 12 || day < 1 || day > 31 ) throw new IllegalArgumentException ( ) ; int m = ( month - 3 + 4800 ) % 4800 ; int y = ( year + m / 12 ) % 400 ; m %= 12 ; return ( y + y / 4 - y / 100 + ( 13 * m + 2 ) / 5 + day + 2 ) % 7 ; } }

import datetime NEW_LINE def compute ( ) : NEW_LINE INDENT ans = sum ( 1 for y in range ( 1901 , 2001 ) for m in range ( 1 , 13 ) if datetime . date ( y , m , 1 ) . weekday ( ) == 6 ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . HashSet ; import java . util . Set ; public final class p095 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p095 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 6 ) ; public String run ( ) { int [ ] divisorSum = new int [ LIMIT + 1 ] ; for ( int i = 1 ; i <= LIMIT ; i ++ ) { for ( int j = i * 2 ; j <= LIMIT ; j += i ) divisorSum [ j ] += i ; } int maxChainLen = 0 ; int minChainElem = - 1 ; for ( int i = 0 ; i <= LIMIT ; i ++ ) { Set < Integer > visited = new HashSet < > ( ) ; for ( int count = 1 , cur = i ; ; count ++ ) { visited . add ( cur ) ; int next = divisorSum [ cur ] ; if ( next == i ) { if ( count > maxChainLen ) { minChainElem = i ; maxChainLen = count ; } break ; } else if ( next > LIMIT || visited . contains ( next ) ) break ; else cur = next ; } } return Integer . toString ( minChainElem ) ; } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 6 NEW_LINE divisorsum = [ 0 ] * ( LIMIT + 1 ) NEW_LINE for i in range ( 1 , LIMIT + 1 ) : NEW_LINE INDENT for j in range ( i * 2 , LIMIT + 1 , i ) : NEW_LINE INDENT divisorsum [ j ] += i NEW_LINE DEDENT DEDENT maxchainlen = 0 NEW_LINE ans = - 1 NEW_LINE for i in range ( LIMIT + 1 ) : NEW_LINE INDENT visited = set ( ) NEW_LINE cur = i NEW_LINE for count in itertools . count ( 1 ) : NEW_LINE INDENT visited . add ( cur ) NEW_LINE next = divisorsum [ cur ] NEW_LINE if next == i : NEW_LINE INDENT if count > maxchainlen : NEW_LINE INDENT ans = i NEW_LINE maxchainlen = count NEW_LINE DEDENT break NEW_LINE DEDENT elif next > LIMIT or next in visited : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT cur = next NEW_LINE DEDENT DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p092 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p092 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 7 ) ; public String run ( ) { int count = 0 ; for ( int i = 1 ; i < LIMIT ; i ++ ) { if ( isClass89 ( i ) ) count ++ ; } return Integer . toString ( count ) ; } private static boolean isClass89 ( int x ) { while ( true ) { switch ( x ) { case 1 : return false ; case 89 : return true ; default : x = nextNumber ( x ) ; } } } private static int nextNumber ( int x ) { int sum = 0 ; while ( x != 0 ) { sum += ( x % 10 ) * ( x % 10 ) ; x /= 10 ; } return sum ; } }

def compute ( ) : NEW_LINE INDENT ans = sum ( 1 for i in range ( 1 , 10000000 ) if get_terminal ( i ) == 89 ) NEW_LINE return str ( ans ) NEW_LINE DEDENT TERMINALS = ( 1 , 89 ) NEW_LINE def get_terminal ( n ) : NEW_LINE INDENT while n not in TERMINALS : NEW_LINE INDENT n = square_digit_sum ( n ) NEW_LINE DEDENT return n NEW_LINE DEDENT def square_digit_sum ( n ) : NEW_LINE INDENT result = 0 NEW_LINE while n > 0 : NEW_LINE INDENT result += SQUARE_DIGITS_SUM [ n % 1000 ] NEW_LINE n //= 1000 NEW_LINE DEDENT return result NEW_LINE DEDENT SQUARE_DIGITS_SUM = [ sum ( int ( c ) ** 2 for c in str ( i ) ) for i in range ( 1000 ) ] NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . Arrays ; public final class p111 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p111 ( ) . run ( ) ) ; } private static final int DIGITS = 10 ; private int [ ] primes ; public String run ( ) { primes = Library . listPrimes ( ( int ) Library . sqrt ( pow ( 10 , DIGITS ) ) ) ; long total = 0 ; for ( int digit = 0 ; digit < 10 ; digit ++ ) { for ( int rep = DIGITS ; rep >= 0 ; rep -- ) { long sum = 0 ; int [ ] digits = new int [ DIGITS ] ; long count = pow ( 9 , DIGITS - rep ) ; level2 : for ( long i = 0 ; i < count ; i ++ ) { Arrays . fill ( digits , 0 , rep , digit ) ; long temp = i ; for ( int j = 0 ; j < DIGITS - rep ; j ++ ) { int d = ( int ) ( temp % 9 ) ; if ( d >= digit ) d ++ ; if ( j > 0 && d > digits [ DIGITS - j ] ) continue level2 ; digits [ DIGITS - 1 - j ] = d ; temp /= 9 ; } Arrays . sort ( digits ) ; do { if ( digits [ 0 ] > 0 ) { long num = toInteger ( digits ) ; if ( isPrime ( num ) ) sum += num ; } } while ( Library . nextPermutation ( digits ) ) ; } if ( sum > 0 ) { total += sum ; break ; } } } return Long . toString ( total ) ; } private boolean isPrime ( long n ) { for ( int p : primes ) { if ( n % p == 0 ) return false ; } return true ; } private static long toInteger ( int [ ] digits ) { long result = 0 ; for ( int x : digits ) result = result * 10 + x ; return result ; } private static long pow ( int x , int y ) { long z = 1 ; for ( int i = 0 ; i < y ; i ++ ) z *= x ; return z ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT DIGITS = 10 NEW_LINE primes = eulerlib . list_primes ( eulerlib . sqrt ( 10 ** DIGITS ) ) NEW_LINE def is_prime ( n ) : NEW_LINE INDENT end = eulerlib . sqrt ( n ) NEW_LINE for p in primes : NEW_LINE INDENT if p > end : NEW_LINE INDENT break NEW_LINE DEDENT if n % p == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT ans = 0 NEW_LINE for digit in range ( 10 ) : NEW_LINE INDENT for rep in range ( DIGITS , - 1 , - 1 ) : NEW_LINE INDENT sum = 0 NEW_LINE digits = [ 0 ] * DIGITS NEW_LINE for i in range ( 9 ** ( DIGITS - rep ) ) : NEW_LINE INDENT for j in range ( rep ) : NEW_LINE INDENT digits [ j ] = digit NEW_LINE DEDENT temp = i NEW_LINE for j in range ( DIGITS - rep ) : NEW_LINE INDENT d = temp % 9 NEW_LINE if d >= digit : NEW_LINE INDENT d += 1 NEW_LINE DEDENT if j > 0 and d > digits [ DIGITS - j ] : NEW_LINE INDENT break NEW_LINE DEDENT digits [ - 1 - j ] = d NEW_LINE temp //= 9 NEW_LINE DEDENT else : NEW_LINE INDENT digits . sort ( ) NEW_LINE while True : NEW_LINE INDENT if digits [ 0 ] > 0 : NEW_LINE INDENT num = int ( " " . join ( map ( str , digits ) ) ) NEW_LINE if is_prime ( num ) : NEW_LINE INDENT sum += num NEW_LINE DEDENT DEDENT if not eulerlib . next_permutation ( digits ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT DEDENT if sum > 0 : NEW_LINE INDENT ans += sum NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p001 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p001 ( ) . run ( ) ) ; } public String run ( ) { int sum = 0 ; for ( int i = 0 ; i < 1000 ; i ++ ) { if ( i % 3 == 0 || i % 5 == 0 ) sum += i ; } return Integer . toString ( sum ) ; } }

def compute ( ) : NEW_LINE INDENT ans = sum ( x for x in range ( 1000 ) if ( x % 3 == 0 or x % 5 == 0 ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p128 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p128 ( ) . run ( ) ) ; } private static final int TARGET = 2000 ; public String run ( ) { int count = 2 ; for ( int ring = 2 ; ; ring ++ ) { if ( ( long ) ring * 12 + 5 > Integer . MAX_VALUE ) throw new ArithmeticException ( ) ; if ( Library . isPrime ( ring * 6 - 1 ) && Library . isPrime ( ring * 6 + 1 ) && Library . isPrime ( ring * 12 + 5 ) ) { count ++ ; if ( count == TARGET ) return Long . toString ( ( long ) ring * ( ring - 1 ) * 3 + 2 ) ; } if ( Library . isPrime ( ring * 6 - 1 ) && Library . isPrime ( ring * 6 + 5 ) && Library . isPrime ( ring * 12 - 7 ) ) { count ++ ; if ( count == TARGET ) return Long . toString ( ( long ) ring * ( ring + 1 ) * 3 + 1 ) ; } } } }

import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT TARGET = 2000 NEW_LINE count = 2 NEW_LINE for ring in itertools . count ( 2 ) : NEW_LINE INDENT if all ( map ( eulerlib . is_prime , ( ring * 6 - 1 , ring * 6 + 1 , ring * 12 + 5 ) ) ) : NEW_LINE INDENT count += 1 NEW_LINE if count == TARGET : NEW_LINE INDENT return str ( ring * ( ring - 1 ) * 3 + 2 ) NEW_LINE DEDENT DEDENT if all ( map ( eulerlib . is_prime , ( ring * 6 - 1 , ring * 6 + 5 , ring * 12 - 7 ) ) ) : NEW_LINE INDENT count += 1 NEW_LINE if count == TARGET : NEW_LINE INDENT return str ( ring * ( ring + 1 ) * 3 + 1 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; public final class p063 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p063 ( ) . run ( ) ) ; } public String run ( ) { int count = 0 ; for ( int n = 1 ; n <= 9 ; n ++ ) { for ( int k = 1 ; k <= 21 ; k ++ ) { if ( BigInteger . valueOf ( n ) . pow ( k ) . toString ( ) . length ( ) == k ) count ++ ; } } return Integer . toString ( count ) ; } }

def compute ( ) : NEW_LINE INDENT ans = sum ( 1 for i in range ( 1 , 10 ) for j in range ( 1 , 22 ) if len ( str ( i ** j ) ) == j ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . ArrayList ; import java . util . List ; public final class p115 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p115 ( ) . run ( ) ) ; } private static final int M = 50 ; public String run ( ) { List < Long > ways = new ArrayList < > ( ) ; ways . add ( 1L ) ; for ( int n = 1 ; ; n ++ ) { long sum = ways . get ( n - 1 ) ; for ( int k = M ; k < n ; k ++ ) sum += ways . get ( n - k - 1 ) ; if ( n >= M ) sum ++ ; ways . add ( sum ) ; if ( sum > 1000000 ) return Long . toString ( n ) ; } } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT M = 50 NEW_LINE ways = [ 1 ] NEW_LINE for n in itertools . count ( 1 ) : NEW_LINE INDENT s = ways [ n - 1 ] + sum ( ways [ : max ( n - M , 0 ) ] ) NEW_LINE if n >= M : NEW_LINE INDENT s += 1 NEW_LINE DEDENT ways . append ( s ) NEW_LINE if s > 1000000 : NEW_LINE INDENT return str ( n ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p034 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p034 ( ) . run ( ) ) ; } public String run ( ) { int sum = 0 ; for ( int i = 3 ; i < 10000000 ; i ++ ) { if ( i == factorialDigitSum ( i ) ) sum += i ; } return Integer . toString ( sum ) ; } private static int [ ] FACTORIAL = { 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 } ; private static int factorialDigitSum ( int x ) { int sum = 0 ; while ( x != 0 ) { sum += FACTORIAL [ x % 10 ] ; x /= 10 ; } return sum ; } }

import math NEW_LINE def compute ( ) : NEW_LINE INDENT ans = sum ( i for i in range ( 3 , 10000000 ) if i == factorial_digit_sum ( i ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def factorial_digit_sum ( n ) : NEW_LINE INDENT result = 0 NEW_LINE while n >= 10000 : NEW_LINE INDENT result += FACTORIAL_DIGITS_SUM_WITH_LEADING_ZEROS [ n % 10000 ] NEW_LINE n //= 10000 NEW_LINE DEDENT return result + FACTORIAL_DIGITS_SUM_WITHOUT_LEADING_ZEROS [ n ] NEW_LINE DEDENT FACTORIAL_DIGITS_SUM_WITHOUT_LEADING_ZEROS = [ sum ( math . factorial ( int ( c ) ) for c in str ( i ) ) for i in range ( 10000 ) ] NEW_LINE FACTORIAL_DIGITS_SUM_WITH_LEADING_ZEROS = [ sum ( math . factorial ( int ( c ) ) for c in str ( i ) . zfill ( 4 ) ) for i in range ( 10000 ) ] NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; public final class p304 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p304 ( ) . run ( ) ) ; } private static final long BASE = 100000000000000L ; private static final int SEARCH_RANGE = 10000000 ; private static final long MODULUS = 1234567891011L ; private static final BigInteger MODULUS_BI = BigInteger . valueOf ( MODULUS ) ; private boolean [ ] isComposite ; public String run ( ) { int [ ] primes = Library . listPrimes ( ( int ) Library . sqrt ( BASE + SEARCH_RANGE ) ) ; isComposite = new boolean [ SEARCH_RANGE ] ; for ( int p : primes ) { for ( int i = ( int ) ( ( BASE + p - 1 ) / p * p - BASE ) ; i < isComposite . length ; i += p ) isComposite [ i ] = true ; } long sum = 0 ; int p = 0 ; for ( int i = 0 ; i < 100000 ; i ++ ) { p = nextPrime ( p ) ; sum = ( sum + fibonacciMod ( BASE + p ) ) % MODULUS ; } return Long . toString ( sum ) ; } private int nextPrime ( int n ) { do { n ++ ; if ( n >= isComposite . length ) throw new AssertionError ( " Search ▁ range ▁ exhausted " ) ; } while ( isComposite [ n ] ) ; return n ; } private static long fibonacciMod ( long n ) { BigInteger a = BigInteger . ZERO ; BigInteger b = BigInteger . ONE ; for ( int i = 63 ; i >= 0 ; i -- ) { BigInteger d = a . multiply ( b . shiftLeft ( 1 ) . subtract ( a ) ) ; BigInteger e = a . pow ( 2 ) . add ( b . pow ( 2 ) ) ; a = d ; b = e ; if ( ( ( n >>> i ) & 1 ) != 0 ) { BigInteger c = a . add ( b ) ; a = b ; b = c ; } a = a . mod ( MODULUS_BI ) ; b = b . mod ( MODULUS_BI ) ; } return a . longValue ( ) ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT BASE = 10 ** 14 NEW_LINE SEARCH_RANGE = 10000000 NEW_LINE MODULUS = 1234567891011 NEW_LINE iscomposite = [ False ] * SEARCH_RANGE NEW_LINE primes = eulerlib . list_primes ( eulerlib . sqrt ( BASE + SEARCH_RANGE ) ) NEW_LINE for p in primes : NEW_LINE INDENT for i in range ( ( BASE + p - 1 ) // p * p - BASE , len ( iscomposite ) , p ) : NEW_LINE INDENT iscomposite [ i ] = True NEW_LINE DEDENT DEDENT def next_prime ( n ) : NEW_LINE INDENT while True : NEW_LINE INDENT n += 1 NEW_LINE if n >= len ( iscomposite ) : NEW_LINE INDENT raise AssertionError ( " Search ▁ range ▁ exhausted " ) NEW_LINE DEDENT if not iscomposite [ n ] : NEW_LINE INDENT return n NEW_LINE DEDENT DEDENT DEDENT ans = 0 NEW_LINE p = 0 NEW_LINE for i in range ( 100000 ) : NEW_LINE INDENT p = next_prime ( p ) NEW_LINE ans = ( ans + fibonacci_mod ( BASE + p , MODULUS ) ) % MODULUS NEW_LINE DEDENT return str ( ans ) NEW_LINE DEDENT def fibonacci_mod ( n , mod ) : NEW_LINE INDENT a , b = 0 , 1 NEW_LINE binary = bin ( n ) [ 2 : ] NEW_LINE for bit in binary : NEW_LINE INDENT a , b = a * ( b * 2 - a ) , a * a + b * b NEW_LINE if bit == "1" : NEW_LINE INDENT a , b = b , a + b NEW_LINE DEDENT a %= mod NEW_LINE b %= mod NEW_LINE DEDENT return a NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . Arrays ; public final class p049 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p049 ( ) . run ( ) ) ; } private static final int LIMIT = 10000 ; public String run ( ) { boolean [ ] isPrime = Library . listPrimality ( LIMIT - 1 ) ; for ( int base = 1000 ; base < LIMIT ; base ++ ) { if ( isPrime [ base ] ) { for ( int step = 1 ; step < LIMIT ; step ++ ) { int a = base + step ; int b = a + step ; if ( a < LIMIT && isPrime [ a ] && hasSameDigits ( a , base ) && b < LIMIT && isPrime [ b ] && hasSameDigits ( b , base ) && ( base != 1487 || a != 4817 ) ) return " " + base + a + b ; } } } throw new RuntimeException ( " Not ▁ found " ) ; } private static boolean hasSameDigits ( int x , int y ) { char [ ] xdigits = Integer . toString ( x ) . toCharArray ( ) ; char [ ] ydigits = Integer . toString ( y ) . toCharArray ( ) ; Arrays . sort ( xdigits ) ; Arrays . sort ( ydigits ) ; return Arrays . equals ( xdigits , ydigits ) ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10000 NEW_LINE isprime = eulerlib . list_primality ( LIMIT - 1 ) NEW_LINE for base in range ( 1000 , LIMIT ) : NEW_LINE INDENT if isprime [ base ] : NEW_LINE INDENT for step in range ( 1 , LIMIT ) : NEW_LINE INDENT a = base + step NEW_LINE b = a + step NEW_LINE if a < LIMIT and isprime [ a ] and has_same_digits ( a , base ) and b < LIMIT and isprime [ b ] and has_same_digits ( b , base ) and ( base != 1487 or a != 4817 ) : NEW_LINE INDENT return str ( base ) + str ( a ) + str ( b ) NEW_LINE DEDENT DEDENT DEDENT DEDENT raise RuntimeError ( " Not ▁ found " ) NEW_LINE DEDENT def has_same_digits ( x , y ) : NEW_LINE INDENT return sorted ( str ( x ) ) == sorted ( str ( y ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p090 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p090 ( ) . run ( ) ) ; } public String run ( ) { int count = 0 ; for ( int i = 0 ; i < ( 1 << 10 ) ; i ++ ) { for ( int j = i ; j < ( 1 << 10 ) ; j ++ ) { if ( Integer . bitCount ( i ) == 6 && Integer . bitCount ( j ) == 6 && isArrangementValid ( i , j ) ) count ++ ; } } return Integer . toString ( count ) ; } private static int [ ] [ ] SQUARES = { { 0 , 1 } , { 0 , 4 } , { 0 , 9 } , { 1 , 6 } , { 2 , 5 } , { 3 , 6 } , { 4 , 9 } , { 6 , 4 } , { 8 , 1 } } ; private static boolean isArrangementValid ( int a , int b ) { if ( testBit ( a , 6 ) || testBit ( a , 9 ) ) a |= ( 1 << 6 ) | ( 1 << 9 ) ; if ( testBit ( b , 6 ) || testBit ( b , 9 ) ) b |= ( 1 << 6 ) | ( 1 << 9 ) ; for ( int [ ] sqr : SQUARES ) { if ( ! ( testBit ( a , sqr [ 0 ] ) && testBit ( b , sqr [ 1 ] ) || testBit ( a , sqr [ 1 ] ) && testBit ( b , sqr [ 0 ] ) ) ) return false ; } return true ; } private static boolean testBit ( int x , int i ) { return ( ( x >>> i ) & 1 ) != 0 ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT ans = sum ( 1 for i in range ( 1 << 10 ) for j in range ( i , 1 << 10 ) if eulerlib . popcount ( i ) == eulerlib . popcount ( j ) == 6 and is_arrangement_valid ( i , j ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def is_arrangement_valid ( a , b ) : NEW_LINE INDENT if test_bit ( a , 6 ) or test_bit ( a , 9 ) : NEW_LINE INDENT a |= ( 1 << 6 ) | ( 1 << 9 ) NEW_LINE DEDENT if test_bit ( b , 6 ) or test_bit ( b , 9 ) : NEW_LINE INDENT b |= ( 1 << 6 ) | ( 1 << 9 ) NEW_LINE DEDENT return all ( ( ( test_bit ( a , c ) and test_bit ( b , d ) ) or ( test_bit ( a , d ) and test_bit ( b , c ) ) ) for ( c , d ) in SQUARES ) NEW_LINE DEDENT def test_bit ( x , i ) : NEW_LINE INDENT return ( ( x >> i ) & 1 ) != 0 NEW_LINE DEDENT SQUARES = [ ( i ** 2 // 10 , i ** 2 % 10 ) for i in range ( 1 , 10 ) ] NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p118 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p118 ( ) . run ( ) ) ; } public String run ( ) { isPrime = Library . listPrimality ( 10000 ) ; count = 0 ; int [ ] digits = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; do countPrimeSets ( digits , 0 , 0 ) ; while ( Library . nextPermutation ( digits ) ) ; return Integer . toString ( count ) ; } private boolean [ ] isPrime ; private int count ; private void countPrimeSets ( int [ ] digits , int startIndex , int prevNum ) { if ( startIndex == digits . length ) count ++ ; else { for ( int split = startIndex + 1 ; split <= digits . length ; split ++ ) { int num = toInteger ( digits , startIndex , split ) ; if ( num > prevNum && isPrime ( num ) ) countPrimeSets ( digits , split , num ) ; } } } private boolean isPrime ( int n ) { if ( n < isPrime . length ) return isPrime [ n ] ; else return Library . isPrime ( n ) ; } private static int toInteger ( int [ ] digits , int start , int end ) { int result = 0 ; for ( int i = start ; i < end ; i ++ ) result = result * 10 + digits [ i ] ; return result ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT isprime = eulerlib . list_primality ( 10000 ) NEW_LINE digits = list ( range ( 1 , 10 ) ) NEW_LINE def count_prime_sets ( startindex , prevnum ) : NEW_LINE INDENT if startindex == len ( digits ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT result = 0 NEW_LINE for split in range ( startindex + 1 , len ( digits ) + 1 ) : NEW_LINE INDENT num = int ( " " . join ( map ( str , digits [ startindex : split ] ) ) ) NEW_LINE if num > prevnum and is_prime ( num ) : NEW_LINE INDENT result += count_prime_sets ( split , num ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT DEDENT def is_prime ( n ) : NEW_LINE INDENT if n < len ( isprime ) : NEW_LINE INDENT return isprime [ n ] NEW_LINE DEDENT else : NEW_LINE INDENT return eulerlib . is_prime ( n ) NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE while True : NEW_LINE INDENT ans += count_prime_sets ( 0 , 0 ) NEW_LINE if not eulerlib . next_permutation ( digits ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p174 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p174 ( ) . run ( ) ) ; } private static final int SIZE_LIMIT = 1000000 ; private static final int TYPE_LIMIT = 10 ; public String run ( ) { int [ ] type = new int [ SIZE_LIMIT + 1 ] ; for ( int n = 3 ; ( n - 1 ) * 4 <= SIZE_LIMIT ; n ++ ) { for ( int m = n - 2 ; m >= 1 ; m -= 2 ) { int tiles = n * n - m * m ; if ( tiles > SIZE_LIMIT ) break ; type [ tiles ] ++ ; } } int count = 0 ; for ( int t : type ) { if ( 1 <= t && t <= TYPE_LIMIT ) count ++ ; } return Integer . toString ( count ) ; } }

def compute ( ) : NEW_LINE INDENT SIZE_LIMIT = 1000000 NEW_LINE TYPE_LIMIT = 10 NEW_LINE type = [ 0 ] * ( SIZE_LIMIT + 1 ) NEW_LINE for n in range ( 3 , SIZE_LIMIT // 4 + 2 ) : NEW_LINE INDENT for m in range ( n - 2 , 0 , - 2 ) : NEW_LINE INDENT tiles = n * n - m * m NEW_LINE if tiles > SIZE_LIMIT : NEW_LINE INDENT break NEW_LINE DEDENT type [ tiles ] += 1 NEW_LINE DEDENT DEDENT ans = sum ( 1 for t in type if 1 <= t <= TYPE_LIMIT ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . ArrayList ; import java . util . Arrays ; import java . util . List ; public final class p109 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p109 ( ) . run ( ) ) ; } public String run ( ) { points = new ArrayList < > ( ) ; for ( int i = 1 ; i <= 20 ; i ++ ) { for ( int j = 1 ; j <= 3 ; j ++ ) points . add ( i * j ) ; } points . add ( 25 ) ; points . add ( 50 ) ; List < Integer > doublePoints = new ArrayList < > ( ) ; for ( int i = 1 ; i <= 20 ; i ++ ) doublePoints . add ( i * 2 ) ; doublePoints . add ( 25 * 2 ) ; ways = new int [ 3 ] [ 101 ] [ points . size ( ) ] ; for ( int [ ] [ ] x : ways ) { for ( int [ ] y : x ) Arrays . fill ( y , - 1 ) ; } int checkouts = 0 ; for ( int remainingPoints = 1 ; remainingPoints < 100 ; remainingPoints ++ ) { for ( int throwz = 0 ; throwz <= 2 ; throwz ++ ) { for ( int p : doublePoints ) { if ( p <= remainingPoints ) checkouts += ways ( throwz , remainingPoints - p , points . size ( ) - 1 ) ; } } } return Integer . toString ( checkouts ) ; } private List < Integer > points ; private int [ ] [ ] [ ] ways ; private int ways ( int throwz , int total , int maxIndex ) { if ( ways [ throwz ] [ total ] [ maxIndex ] == - 1 ) { int result ; if ( throwz == 0 ) result = total == 0 ? 1 : 0 ; else { result = 0 ; if ( maxIndex > 0 ) result += ways ( throwz , total , maxIndex - 1 ) ; if ( points . get ( maxIndex ) <= total ) result += ways ( throwz - 1 , total - points . get ( maxIndex ) , maxIndex ) ; } ways [ throwz ] [ total ] [ maxIndex ] = result ; } return ways [ throwz ] [ total ] [ maxIndex ] ; } }

def compute ( ) : NEW_LINE INDENT points = [ i * j for i in range ( 1 , 21 ) for j in range ( 1 , 4 ) ] + [ 25 , 50 ] NEW_LINE doublepoints = [ i * 2 for i in range ( 1 , 21 ) ] + [ 25 * 2 ] NEW_LINE ways = [ [ [ None ] * len ( points ) for j in range ( 101 ) ] for i in range ( 3 ) ] NEW_LINE def calc_ways ( throws , total , maxindex ) : NEW_LINE INDENT if ways [ throws ] [ total ] [ maxindex ] is None : NEW_LINE INDENT if throws == 0 : NEW_LINE INDENT result = 1 if total == 0 else 0 NEW_LINE DEDENT else : NEW_LINE INDENT result = 0 NEW_LINE if maxindex > 0 : NEW_LINE INDENT result += calc_ways ( throws , total , maxindex - 1 ) NEW_LINE DEDENT if points [ maxindex ] <= total : NEW_LINE INDENT result += calc_ways ( throws - 1 , total - points [ maxindex ] , maxindex ) NEW_LINE DEDENT DEDENT ways [ throws ] [ total ] [ maxindex ] = result NEW_LINE DEDENT return ways [ throws ] [ total ] [ maxindex ] NEW_LINE DEDENT checkouts = 0 NEW_LINE for remainingpoints in range ( 1 , 100 ) : NEW_LINE INDENT for throws in range ( 3 ) : NEW_LINE INDENT for p in doublepoints : NEW_LINE INDENT if p <= remainingpoints : NEW_LINE INDENT checkouts += calc_ways ( throws , remainingpoints - p , len ( points ) - 1 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return str ( checkouts ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p023 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p023 ( ) . run ( ) ) ; } private static final int LIMIT = 28123 ; private boolean [ ] isAbundant = new boolean [ LIMIT + 1 ] ; public String run ( ) { for ( int i = 1 ; i < isAbundant . length ; i ++ ) isAbundant [ i ] = isAbundant ( i ) ; int sum = 0 ; for ( int i = 1 ; i <= LIMIT ; i ++ ) { if ( ! isSumOf2Abundants ( i ) ) sum += i ; } return Integer . toString ( sum ) ; } private boolean isSumOf2Abundants ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) { if ( isAbundant [ i ] && isAbundant [ n - i ] ) return true ; } return false ; } private static boolean isAbundant ( int n ) { if ( n < 1 ) throw new IllegalArgumentException ( ) ; int sum = 1 ; int end = Library . sqrt ( n ) ; for ( int i = 2 ; i <= end ; i ++ ) { if ( n % i == 0 ) sum += i + n / i ; } if ( end * end == n ) sum -= end ; return sum > n ; } }

def compute ( ) : NEW_LINE INDENT LIMIT = 28124 NEW_LINE divisorsum = [ 0 ] * LIMIT NEW_LINE for i in range ( 1 , LIMIT ) : NEW_LINE INDENT for j in range ( i * 2 , LIMIT , i ) : NEW_LINE INDENT divisorsum [ j ] += i NEW_LINE DEDENT DEDENT abundantnums = [ i for ( i , x ) in enumerate ( divisorsum ) if x > i ] NEW_LINE expressible = [ False ] * LIMIT NEW_LINE for i in abundantnums : NEW_LINE INDENT for j in abundantnums : NEW_LINE INDENT if i + j < LIMIT : NEW_LINE INDENT expressible [ i + j ] = True NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT ans = sum ( i for ( i , x ) in enumerate ( expressible ) if not x ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p045 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p045 ( ) . run ( ) ) ; } public String run ( ) { int i = 286 ; int j = 166 ; int k = 144 ; while ( true ) { long triangle = ( long ) i * ( i + 1 ) / 2 ; long pentagon = ( long ) j * ( j * 3 - 1 ) / 2 ; long hexagon = ( long ) k * ( k * 2 - 1 ) ; long min = Math . min ( Math . min ( triangle , pentagon ) , hexagon ) ; if ( min == triangle && min == pentagon && min == hexagon ) return Long . toString ( min ) ; if ( min == triangle ) i ++ ; if ( min == pentagon ) j ++ ; if ( min == hexagon ) k ++ ; } } }

def compute ( ) : NEW_LINE INDENT i = 286 NEW_LINE j = 166 NEW_LINE k = 144 NEW_LINE while True : NEW_LINE INDENT triangle = i * ( i + 1 ) // 2 NEW_LINE pentagon = j * ( j * 3 - 1 ) // 2 NEW_LINE hexagon = k * ( k * 2 - 1 ) NEW_LINE minimum = min ( triangle , pentagon , hexagon ) NEW_LINE if minimum == max ( triangle , pentagon , hexagon ) : NEW_LINE INDENT return str ( triangle ) NEW_LINE DEDENT if minimum == triangle : i += 1 NEW_LINE if minimum == pentagon : j += 1 NEW_LINE if minimum == hexagon : k += 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; public final class p016 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p016 ( ) . run ( ) ) ; } public String run ( ) { String temp = BigInteger . ONE . shiftLeft ( 1000 ) . toString ( ) ; int sum = 0 ; for ( int i = 0 ; i < temp . length ( ) ; i ++ ) sum += temp . charAt ( i ) - '0' ; return Integer . toString ( sum ) ; } }

def compute ( ) : NEW_LINE INDENT n = 2 ** 1000 NEW_LINE ans = sum ( int ( c ) for c in str ( n ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p050 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p050 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 6 ) ; public String run ( ) { boolean [ ] isPrime = Library . listPrimality ( LIMIT ) ; int [ ] primes = Library . listPrimes ( LIMIT ) ; long maxSum = 0 ; int maxRun = - 1 ; for ( int i = 0 ; i < primes . length ; i ++ ) { int sum = 0 ; for ( int j = i ; j < primes . length ; j ++ ) { sum += primes [ j ] ; if ( sum > LIMIT ) break ; else if ( j - i > maxRun && sum > maxSum && isPrime [ sum ] ) { maxSum = sum ; maxRun = j - i ; } } } return Long . toString ( maxSum ) ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT ans = 0 NEW_LINE isprime = eulerlib . list_primality ( 999999 ) NEW_LINE primes = eulerlib . list_primes ( 999999 ) NEW_LINE consecutive = 0 NEW_LINE for i in range ( len ( primes ) ) : NEW_LINE INDENT sum = primes [ i ] NEW_LINE consec = 1 NEW_LINE for j in range ( i + 1 , len ( primes ) ) : NEW_LINE INDENT sum += primes [ j ] NEW_LINE consec += 1 NEW_LINE if sum >= len ( isprime ) : NEW_LINE INDENT break NEW_LINE DEDENT if isprime [ sum ] and consec > consecutive : NEW_LINE INDENT ans = sum NEW_LINE consecutive = consec NEW_LINE DEDENT DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p123 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p123 ( ) . run ( ) ) ; } private static final int PRIME_LIMIT = 1000000 ; private static final long THRESHOLD = 10000000000L ; public String run ( ) { int [ ] primes = Library . listPrimes ( PRIME_LIMIT ) ; for ( int n = 5 ; n <= primes . length ; n += 2 ) { long rem = ( long ) n * primes [ n - 1 ] * 2 ; if ( rem > THRESHOLD ) return Integer . toString ( n ) ; } throw new AssertionError ( " Not ▁ found " ) ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT primes = eulerlib . list_primes ( 1000000 ) NEW_LINE for n in range ( 5 , len ( primes ) , 2 ) : NEW_LINE INDENT rem = n * primes [ n - 1 ] * 2 NEW_LINE if rem > 10000000000 : NEW_LINE INDENT return str ( n ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p108 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p108 ( ) . run ( ) ) ; } public String run ( ) { for ( int n = 1 ; ; n ++ ) { if ( ( countDivisorsSquared ( n ) + 1 ) / 2 > 1000 ) return Integer . toString ( n ) ; } } private static int countDivisorsSquared ( int n ) { int count = 1 ; for ( int i = 2 , end = Library . sqrt ( n ) ; i <= end ; i ++ ) { if ( n % i == 0 ) { int j = 0 ; do { n /= i ; j ++ ; } while ( n % i == 0 ) ; count *= j * 2 + 1 ; end = Library . sqrt ( n ) ; } } if ( n != 1 ) count *= 3 ; return count ; } }

import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT for n in itertools . count ( 1 ) : NEW_LINE INDENT if ( count_divisors_squared ( n ) + 1 ) // 2 > 1000 : NEW_LINE INDENT return str ( n ) NEW_LINE DEDENT DEDENT DEDENT def count_divisors_squared ( n ) : NEW_LINE INDENT count = 1 NEW_LINE end = eulerlib . sqrt ( n ) NEW_LINE for i in itertools . count ( 2 ) : NEW_LINE INDENT if i > end : NEW_LINE INDENT break NEW_LINE DEDENT if n % i == 0 : NEW_LINE INDENT j = 0 NEW_LINE while True : NEW_LINE INDENT n //= i NEW_LINE j += 1 NEW_LINE if n % i != 0 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT count *= j * 2 + 1 NEW_LINE end = eulerlib . sqrt ( n ) NEW_LINE DEDENT DEDENT if n != 1 : NEW_LINE INDENT count *= 3 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p114 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p114 ( ) . run ( ) ) ; } private static final int LENGTH = 50 ; public String run ( ) { long [ ] ways = new long [ LENGTH + 1 ] ; ways [ 0 ] = 1 ; ways [ 1 ] = 1 ; ways [ 2 ] = 1 ; for ( int n = 3 ; n <= LENGTH ; n ++ ) { long sum = ways [ n - 1 ] + 1 ; for ( int k = 3 ; k < n ; k ++ ) sum += ways [ n - k - 1 ] ; ways [ n ] = sum ; } return Long . toString ( ways [ LENGTH ] ) ; } }

def compute ( ) : NEW_LINE INDENT LENGTH = 50 NEW_LINE ways = [ 0 ] * ( LENGTH + 1 ) NEW_LINE for n in range ( len ( ways ) ) : NEW_LINE INDENT if n < 3 : NEW_LINE INDENT ways [ n ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT ways [ n ] = ways [ n - 1 ] + sum ( ways [ : n - 3 ] ) + 1 NEW_LINE DEDENT DEDENT return str ( ways [ - 1 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p030 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p030 ( ) . run ( ) ) ; } public String run ( ) { int sum = 0 ; for ( int i = 2 ; i < 1000000 ; i ++ ) { if ( i == fifthPowerDigitSum ( i ) ) sum += i ; } return Integer . toString ( sum ) ; } private static int fifthPowerDigitSum ( int x ) { int sum = 0 ; while ( x != 0 ) { int y = x % 10 ; sum += y * y * y * y * y ; x /= 10 ; } return sum ; } }

def compute ( ) : NEW_LINE INDENT ans = sum ( i for i in range ( 2 , 1000000 ) if i == fifth_power_digit_sum ( i ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def fifth_power_digit_sum ( n ) : NEW_LINE INDENT return sum ( int ( c ) ** 5 for c in str ( n ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p010 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p010 ( ) . run ( ) ) ; } private static final int LIMIT = 2000000 ; public String run ( ) { long sum = 0 ; for ( int p : Library . listPrimes ( LIMIT - 1 ) ) sum += p ; return Long . toString ( sum ) ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT ans = sum ( eulerlib . list_primes ( 1999999 ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p145 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p145 ( ) . run ( ) ) ; } public String run ( ) { int sum = 0 ; for ( int digits = 1 ; digits <= 9 ; digits ++ ) { if ( digits % 2 == 0 ) sum += 20 * Library . pow ( 30 , digits / 2 - 1 ) ; else if ( digits % 4 == 3 ) sum += 100 * Library . pow ( 500 , ( digits - 3 ) / 4 ) ; else if ( digits % 4 == 1 ) sum += 0 ; else throw new AssertionError ( ) ; } return Integer . toString ( sum ) ; } }

def compute ( ) : NEW_LINE INDENT def count_reversibles ( numdigits ) : NEW_LINE INDENT if numdigits % 2 == 0 : NEW_LINE INDENT return 20 * 30 ** ( numdigits // 2 - 1 ) NEW_LINE DEDENT elif numdigits % 4 == 3 : NEW_LINE INDENT return 100 * 500 ** ( ( numdigits - 3 ) // 4 ) NEW_LINE DEDENT elif numdigits % 4 == 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT raise AssertionError ( ) NEW_LINE DEDENT DEDENT ans = sum ( count_reversibles ( d ) for d in range ( 2 , 10 ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p142 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p142 ( ) . run ( ) ) ; } private boolean [ ] isSquare ; public String run ( ) { int sumLimit = 10 ; while ( true ) { isSquare = new boolean [ sumLimit ] ; for ( int i = 0 ; i * i < sumLimit ; i ++ ) isSquare [ i * i ] = true ; int sum = findSum ( sumLimit ) ; if ( sum != - 1 ) { sum = sumLimit ; break ; } sumLimit *= 10 ; } while ( true ) { int sum = findSum ( sumLimit ) ; if ( sum == - 1 ) return Integer . toString ( sumLimit ) ; sumLimit = sum ; } } private int findSum ( int limit ) { for ( int a = 1 ; a * a < limit ; a ++ ) { for ( int b = a - 1 ; b > 0 ; b -- ) { if ( ( a + b ) % 2 != 0 ) continue ; int x = ( a * a + b * b ) / 2 ; int y = ( a * a - b * b ) / 2 ; if ( x + y + 1 >= limit ) continue ; int zlimit = Math . min ( y , limit - x - y ) ; for ( int c = Library . sqrt ( y ) + 1 ; c * c - y < zlimit ; c ++ ) { int z = c * c - y ; if ( isSquare [ x + z ] && isSquare [ x - z ] && isSquare [ y - z ] ) return x + y + z ; } } } return - 1 ; } }

import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT def find_sum ( limit ) : NEW_LINE INDENT for a in itertools . count ( 1 ) : NEW_LINE INDENT if a * a >= limit : NEW_LINE INDENT break NEW_LINE DEDENT for b in reversed ( range ( 1 , a ) ) : NEW_LINE INDENT if ( a + b ) % 2 != 0 : NEW_LINE INDENT continue NEW_LINE DEDENT x = ( a * a + b * b ) // 2 NEW_LINE y = ( a * a - b * b ) // 2 NEW_LINE if x + y + 1 >= limit : NEW_LINE INDENT continue NEW_LINE DEDENT zlimit = min ( y , limit - x - y ) NEW_LINE for c in itertools . count ( eulerlib . sqrt ( y ) + 1 ) : NEW_LINE INDENT z = c * c - y NEW_LINE if z >= zlimit : NEW_LINE INDENT break NEW_LINE DEDENT if issquare [ x + z ] and issquare [ x - z ] and issquare [ y - z ] : NEW_LINE INDENT return x + y + z NEW_LINE DEDENT DEDENT DEDENT DEDENT return None NEW_LINE DEDENT sumlimit = 10 NEW_LINE while True : NEW_LINE INDENT issquare = [ False ] * sumlimit NEW_LINE for i in range ( eulerlib . sqrt ( len ( issquare ) - 1 ) + 1 ) : NEW_LINE INDENT issquare [ i * i ] = True NEW_LINE DEDENT sum = find_sum ( sumlimit ) NEW_LINE if sum is not None : NEW_LINE INDENT sum = sumlimit NEW_LINE break NEW_LINE DEDENT sumlimit *= 10 NEW_LINE DEDENT while True : NEW_LINE INDENT sum = find_sum ( sumlimit ) NEW_LINE if sum is None : NEW_LINE INDENT return str ( sumlimit ) NEW_LINE DEDENT sumlimit = sum NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p020 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p020 ( ) . run ( ) ) ; } public String run ( ) { String temp = Library . factorial ( 100 ) . toString ( ) ; int sum = 0 ; for ( int i = 0 ; i < temp . length ( ) ; i ++ ) sum += temp . charAt ( i ) - '0' ; return Integer . toString ( sum ) ; } }

import math NEW_LINE def compute ( ) : NEW_LINE INDENT n = math . factorial ( 100 ) NEW_LINE ans = sum ( int ( c ) for c in str ( n ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; public final class p065 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p065 ( ) . run ( ) ) ; } public String run ( ) { BigInteger n = BigInteger . ONE ; BigInteger d = BigInteger . ZERO ; for ( int i = 99 ; i >= 0 ; i -- ) { BigInteger temp = BigInteger . valueOf ( continuedFractionTerm ( i ) ) . multiply ( n ) . add ( d ) ; d = n ; n = temp ; } int sum = 0 ; while ( ! n . equals ( BigInteger . ZERO ) ) { BigInteger [ ] divrem = n . divideAndRemainder ( BigInteger . TEN ) ; sum += divrem [ 1 ] . intValue ( ) ; n = divrem [ 0 ] ; } return Integer . toString ( sum ) ; } private static int continuedFractionTerm ( int i ) { if ( i == 0 ) return 2 ; else if ( i % 3 == 2 ) return i / 3 * 2 + 2 ; else return 1 ; } }

def compute ( ) : NEW_LINE INDENT numer = 1 NEW_LINE denom = 0 NEW_LINE for i in reversed ( range ( 100 ) ) : NEW_LINE INDENT numer , denom = e_contfrac_term ( i ) * numer + denom , numer NEW_LINE DEDENT ans = sum ( int ( c ) for c in str ( numer ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def e_contfrac_term ( i ) : NEW_LINE INDENT if i == 0 : NEW_LINE INDENT return 2 NEW_LINE DEDENT elif i % 3 == 2 : NEW_LINE INDENT return i // 3 * 2 + 2 NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . Arrays ; public final class p146 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p146 ( ) . run ( ) ) ; } private static final int LIMIT = 150000000 ; private static long [ ] INCREMENTS = { 1 , 3 , 7 , 9 , 13 , 27 } ; public String run ( ) { long sum = 0 ; for ( int n = 0 ; n < LIMIT ; n += 10 ) { if ( hasConsecutivePrimes ( n ) ) sum += n ; } return Long . toString ( sum ) ; } private static long maxNumber = ( long ) LIMIT * LIMIT + INCREMENTS [ INCREMENTS . length - 1 ] ; private static int [ ] primes = Library . listPrimes ( ( int ) Library . sqrt ( maxNumber ) ) ; private static boolean hasConsecutivePrimes ( int n ) { long n2 = ( long ) n * n ; long [ ] temp = new long [ INCREMENTS . length ] ; for ( int i = 0 ; i < INCREMENTS . length ; i ++ ) temp [ i ] = n2 + INCREMENTS [ i ] ; for ( int p : primes ) { for ( long x : temp ) { if ( x != p && x % p == 0 ) return false ; } } for ( int i = 1 ; i < INCREMENTS [ INCREMENTS . length - 1 ] ; i ++ ) { if ( Arrays . binarySearch ( INCREMENTS , i ) < 0 && isPrime ( n2 + i ) ) return false ; } return true ; } private static boolean isPrime ( long n ) { int end = ( int ) Library . sqrt ( n ) ; for ( int p : primes ) { if ( p > end ) break ; if ( n != p && n % p == 0 ) return false ; } return true ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 150000000 NEW_LINE INCREMENTS = [ 1 , 3 , 7 , 9 , 13 , 27 ] NEW_LINE NON_INCREMENTS = set ( range ( INCREMENTS [ - 1 ] ) ) - set ( INCREMENTS ) NEW_LINE maxnumber = LIMIT ** 2 + INCREMENTS [ - 1 ] NEW_LINE primes = eulerlib . list_primes ( eulerlib . sqrt ( maxnumber ) ) NEW_LINE def has_consecutive_primes ( n ) : NEW_LINE INDENT n2 = n ** 2 NEW_LINE temp = [ ( n2 + k ) for k in INCREMENTS ] NEW_LINE if any ( ( x != p and x % p == 0 ) for p in primes for x in temp ) : NEW_LINE INDENT return False NEW_LINE DEDENT return all ( ( not is_prime ( n2 + k ) ) for k in NON_INCREMENTS ) NEW_LINE DEDENT def is_prime ( n ) : NEW_LINE INDENT end = eulerlib . sqrt ( n ) NEW_LINE for p in primes : NEW_LINE INDENT if p > end : NEW_LINE INDENT break NEW_LINE DEDENT if n % p == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT ans = sum ( n for n in range ( 0 , LIMIT , 10 ) if has_consecutive_primes ( n ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; import java . util . Arrays ; import java . util . HashMap ; import java . util . List ; import java . util . Map ; public final class p169 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p169 ( ) . run ( ) ) ; } private static final BigInteger NUMBER = BigInteger . TEN . pow ( 25 ) ; public String run ( ) { return countWays ( NUMBER , NUMBER . bitLength ( ) - 1 , 2 ) . toString ( ) ; } private Map < List < BigInteger > , BigInteger > ways = new HashMap < > ( ) ; private BigInteger countWays ( BigInteger number , int exponent , int repetitions ) { List < BigInteger > key = Arrays . asList ( number , BigInteger . valueOf ( exponent ) , BigInteger . valueOf ( repetitions ) ) ; if ( ways . containsKey ( key ) ) return ways . get ( key ) ; BigInteger result ; if ( exponent < 0 ) result = number . equals ( BigInteger . ZERO ) ? BigInteger . ONE : BigInteger . ZERO ; else { result = countWays ( number , exponent - 1 , 2 ) ; BigInteger pow = BigInteger . ONE . shiftLeft ( exponent ) ; BigInteger upper = pow . multiply ( BigInteger . valueOf ( repetitions + 2 ) ) ; if ( repetitions > 0 && pow . compareTo ( number ) <= 0 && number . compareTo ( upper ) < 0 ) result = result . add ( countWays ( number . subtract ( pow ) , exponent , repetitions - 1 ) ) ; } ways . put ( key , result ) ; return result ; } }

import eulerlib , sys NEW_LINE def compute ( ) : NEW_LINE INDENT sys . setrecursionlimit ( 3000 ) NEW_LINE NUMBER = 10 ** 25 NEW_LINE ans = count_ways ( NUMBER , NUMBER . bit_length ( ) - 1 , 2 ) NEW_LINE return str ( ans ) NEW_LINE DEDENT @ eulerlib . memoize NEW_LINE def count_ways ( number , exponent , repetitions ) : NEW_LINE INDENT if exponent < 0 : NEW_LINE INDENT return 1 if number == 0 else 0 NEW_LINE DEDENT else : NEW_LINE INDENT result = count_ways ( number , exponent - 1 , 2 ) NEW_LINE power = 1 << exponent NEW_LINE upper = power * ( repetitions + 2 ) NEW_LINE if repetitions > 0 and power <= number < upper : NEW_LINE INDENT result += count_ways ( number - power , exponent , repetitions - 1 ) NEW_LINE DEDENT return result NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p204 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p204 ( ) . run ( ) ) ; } public String run ( ) { return Integer . toString ( count ( 0 , 1 ) ) ; } private static long LIMIT = Library . pow ( 10 , 9 ) ; private int [ ] primes = Library . listPrimes ( 100 ) ; private int count ( int primeIndex , long product ) { if ( primeIndex == primes . length ) return product <= LIMIT ? 1 : 0 ; else { int count = 0 ; while ( product <= LIMIT ) { count += count ( primeIndex + 1 , product ) ; product *= primes [ primeIndex ] ; } return count ; } } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 9 NEW_LINE primes = eulerlib . list_primes ( 100 ) NEW_LINE def count ( primeindex , product ) : NEW_LINE INDENT if primeindex == len ( primes ) : NEW_LINE INDENT return 1 if product <= LIMIT else 0 NEW_LINE DEDENT else : NEW_LINE INDENT result = 0 NEW_LINE while product <= LIMIT : NEW_LINE INDENT result += count ( primeindex + 1 , product ) NEW_LINE product *= primes [ primeindex ] NEW_LINE DEDENT return result NEW_LINE DEDENT DEDENT return str ( count ( 0 , 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; public final class p057 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p057 ( ) . run ( ) ) ; } private static final int LIMIT = 1000 ; public String run ( ) { BigInteger n = BigInteger . ZERO ; BigInteger d = BigInteger . ONE ; int count = 0 ; for ( int i = 0 ; i < LIMIT ; i ++ ) { BigInteger temp = d . multiply ( BigInteger . valueOf ( 2 ) ) . add ( n ) ; n = d ; d = temp ; if ( n . add ( d ) . toString ( ) . length ( ) > d . toString ( ) . length ( ) ) count ++ ; } return Integer . toString ( count ) ; } }

def compute ( ) : NEW_LINE INDENT LIMIT = 1000 NEW_LINE ans = 0 NEW_LINE numer = 0 NEW_LINE denom = 1 NEW_LINE for _ in range ( LIMIT ) : NEW_LINE INDENT numer , denom = denom , denom * 2 + numer NEW_LINE if len ( str ( numer + denom ) ) > len ( str ( denom ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; import java . util . ArrayList ; import java . util . SortedSet ; import java . util . TreeSet ; public final class p119 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p119 ( ) . run ( ) ) ; } private static final int INDEX = 30 ; public String run ( ) { for ( BigInteger limit = BigInteger . ONE ; ; limit = limit . shiftLeft ( 8 ) ) { SortedSet < BigInteger > candidates = new TreeSet < > ( ) ; for ( int k = 2 ; BigInteger . valueOf ( 1 ) . shiftLeft ( k ) . compareTo ( limit ) < 0 ; k ++ ) { for ( int n = 2 ; ; n ++ ) { BigInteger pow = BigInteger . valueOf ( n ) . pow ( k ) ; if ( pow . compareTo ( limit ) >= 0 && pow . toString ( ) . length ( ) * 9 < n ) break ; if ( pow . compareTo ( BigInteger . TEN ) >= 0 && isDigitSumPower ( pow ) ) candidates . add ( pow ) ; } } if ( candidates . size ( ) >= INDEX ) return new ArrayList < > ( candidates ) . get ( INDEX - 1 ) . toString ( ) ; } } private static boolean isDigitSumPower ( BigInteger x ) { int digitSum = digitSum ( x ) ; if ( digitSum == 1 ) return false ; BigInteger base = BigInteger . valueOf ( digitSum ) ; BigInteger pow = base ; while ( pow . compareTo ( x ) < 0 ) pow = pow . multiply ( base ) ; return pow . equals ( x ) ; } private static int digitSum ( BigInteger x ) { if ( x . signum ( ) < 1 ) throw new IllegalArgumentException ( " Only ▁ for ▁ positive ▁ integers " ) ; int sum = 0 ; for ( char c : x . toString ( ) . toCharArray ( ) ) sum += c - '0' ; return sum ; } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT INDEX = 30 NEW_LINE limit = 1 NEW_LINE while True : NEW_LINE INDENT candidates = set ( ) NEW_LINE k = 2 NEW_LINE while ( 1 << k ) < limit : NEW_LINE INDENT for n in itertools . count ( 2 ) : NEW_LINE INDENT pow = n ** k NEW_LINE if pow >= limit and len ( str ( pow ) ) * 9 < n : NEW_LINE INDENT break NEW_LINE DEDENT if pow >= 10 and is_digit_sum_power ( pow ) : NEW_LINE INDENT candidates . add ( pow ) NEW_LINE DEDENT DEDENT k += 1 NEW_LINE DEDENT if len ( candidates ) >= INDEX : NEW_LINE INDENT return str ( sorted ( candidates ) [ INDEX - 1 ] ) NEW_LINE DEDENT limit <<= 8 NEW_LINE DEDENT DEDENT def is_digit_sum_power ( x ) : NEW_LINE INDENT digitsum = sum ( int ( c ) for c in str ( x ) ) NEW_LINE if digitsum == 1 : NEW_LINE INDENT return False NEW_LINE DEDENT pow = digitsum NEW_LINE while pow < x : NEW_LINE INDENT pow *= digitsum NEW_LINE DEDENT return pow == x NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p047 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p047 ( ) . run ( ) ) ; } public String run ( ) { for ( int i = 2 ; ; i ++ ) { if ( has4PrimeFactors ( i + 0 ) && has4PrimeFactors ( i + 1 ) && has4PrimeFactors ( i + 2 ) && has4PrimeFactors ( i + 3 ) ) return Integer . toString ( i ) ; } } private static boolean has4PrimeFactors ( int n ) { return countDistinctPrimeFactors ( n ) == 4 ; } private static int countDistinctPrimeFactors ( int n ) { int count = 0 ; for ( int i = 2 , end = Library . sqrt ( n ) ; i <= end ; i ++ ) { if ( n % i == 0 ) { do n /= i ; while ( n % i == 0 ) ; count ++ ; end = Library . sqrt ( n ) ; } } if ( n > 1 ) count ++ ; return count ; } }

import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT cond = lambda i : all ( ( count_distinct_prime_factors ( i + j ) == 4 ) for j in range ( 4 ) ) NEW_LINE ans = next ( filter ( cond , itertools . count ( ) ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT @ eulerlib . memoize NEW_LINE def count_distinct_prime_factors ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while n > 1 : NEW_LINE INDENT count += 1 NEW_LINE for i in range ( 2 , eulerlib . sqrt ( n ) + 1 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT while True : NEW_LINE INDENT n //= i NEW_LINE if n % i != 0 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . Arrays ; public final class p070 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p070 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 7 ) ; public String run ( ) { int minNumer = 1 ; int minDenom = 0 ; int [ ] totients = Library . listTotients ( LIMIT - 1 ) ; for ( int n = 2 ; n < totients . length ; n ++ ) { int tot = totients [ n ] ; if ( ( long ) n * minDenom < ( long ) minNumer * tot && hasSameDigits ( n , tot ) ) { minNumer = n ; minDenom = tot ; } } if ( minDenom == 0 ) throw new RuntimeException ( " Not ▁ found " ) ; return Integer . toString ( minNumer ) ; } private static boolean hasSameDigits ( int x , int y ) { char [ ] xdigits = Integer . toString ( x ) . toCharArray ( ) ; char [ ] ydigits = Integer . toString ( y ) . toCharArray ( ) ; Arrays . sort ( xdigits ) ; Arrays . sort ( ydigits ) ; return Arrays . equals ( xdigits , ydigits ) ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT totients = eulerlib . list_totients ( 10 ** 7 - 1 ) NEW_LINE minnumer = 1 NEW_LINE mindenom = 0 NEW_LINE for ( i , tot ) in enumerate ( totients [ 2 : ] , 2 ) : NEW_LINE INDENT if i * mindenom < minnumer * tot and sorted ( str ( i ) ) == sorted ( str ( tot ) ) : NEW_LINE INDENT minnumer = i NEW_LINE mindenom = totients [ i ] NEW_LINE DEDENT DEDENT return str ( minnumer ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p301 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p301 ( ) . run ( ) ) ; } public String run ( ) { int a = 0 ; int b = 1 ; for ( int i = 0 ; i < 32 ; i ++ ) { int c = a + b ; a = b ; b = c ; } return Integer . toString ( a ) ; } }

def compute ( ) : NEW_LINE INDENT a = 0 NEW_LINE b = 1 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT a , b = b , a + b NEW_LINE DEDENT return str ( a ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; public final class p162 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p162 ( ) . run ( ) ) ; } public String run ( ) { BigInteger sum = BigInteger . ZERO ; for ( int n = 1 ; n <= 16 ; n ++ ) { sum = sum . add ( bi ( 15 ) . multiply ( bi ( 16 ) . pow ( n - 1 ) ) ) . subtract ( bi ( 43 ) . multiply ( bi ( 15 ) . pow ( n - 1 ) ) ) . add ( bi ( 41 ) . multiply ( bi ( 14 ) . pow ( n - 1 ) ) ) . subtract ( bi ( 13 ) . pow ( n ) ) ; } return sum . toString ( 16 ) . toUpperCase ( ) ; } private static BigInteger bi ( int n ) { return BigInteger . valueOf ( n ) ; } }

def compute ( ) : NEW_LINE INDENT ans = sum ( ( 15 * 16 ** ( n - 1 ) - 43 * 15 ** ( n - 1 ) + 41 * 14 ** ( n - 1 ) - 13 ** n ) for n in range ( 1 , 17 ) ) NEW_LINE return f " { ans : X } " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p171 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p171 ( ) . run ( ) ) ; } private static final int LENGTH = 20 ; private static final int BASE = 10 ; private static final int MODULUS = Library . pow ( 10 , 9 ) ; public String run ( ) { int MAX_SQR_DIGIT_SUM = ( BASE - 1 ) * ( BASE - 1 ) * LENGTH ; long [ ] [ ] sum = new long [ LENGTH + 1 ] [ MAX_SQR_DIGIT_SUM + 1 ] ; long [ ] [ ] count = new long [ LENGTH + 1 ] [ MAX_SQR_DIGIT_SUM + 1 ] ; count [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= LENGTH ; i ++ ) { for ( int j = 0 ; j < BASE ; j ++ ) { for ( int k = 0 ; k + j * j <= MAX_SQR_DIGIT_SUM ; k ++ ) { sum [ i ] [ k + j * j ] = ( sum [ i ] [ k + j * j ] + sum [ i - 1 ] [ k ] + Library . powMod ( BASE , i - 1 , MODULUS ) * j % MODULUS * count [ i - 1 ] [ k ] ) % MODULUS ; count [ i ] [ k + j * j ] = ( count [ i ] [ k + j * j ] + count [ i - 1 ] [ k ] ) % MODULUS ; } } } long s = 0 ; for ( int i = 1 ; i * i <= MAX_SQR_DIGIT_SUM ; i ++ ) s = ( s + sum [ LENGTH ] [ i * i ] ) % MODULUS ; return String . format ( " % 09d " , s ) ; } }

import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT LENGTH = 20 NEW_LINE BASE = 10 NEW_LINE MODULUS = 10 ** 9 NEW_LINE MAX_SQR_DIGIT_SUM = ( BASE - 1 ) ** 2 * LENGTH NEW_LINE sqsum = [ ] NEW_LINE count = [ ] NEW_LINE for i in range ( LENGTH + 1 ) : NEW_LINE INDENT sqsum . append ( [ 0 ] * ( MAX_SQR_DIGIT_SUM + 1 ) ) NEW_LINE count . append ( [ 0 ] * ( MAX_SQR_DIGIT_SUM + 1 ) ) NEW_LINE if i == 0 : NEW_LINE INDENT count [ 0 ] [ 0 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT for j in range ( BASE ) : NEW_LINE INDENT for k in itertools . count ( ) : NEW_LINE INDENT index = k + j ** 2 NEW_LINE if index > MAX_SQR_DIGIT_SUM : NEW_LINE INDENT break NEW_LINE DEDENT sqsum [ i ] [ index ] = ( sqsum [ i ] [ index ] + sqsum [ i - 1 ] [ k ] + pow ( BASE , i - 1 , MODULUS ) * j * count [ i - 1 ] [ k ] ) % MODULUS NEW_LINE count [ i ] [ index ] = ( count [ i ] [ index ] + count [ i - 1 ] [ k ] ) % MODULUS NEW_LINE DEDENT DEDENT DEDENT DEDENT ans = sum ( sqsum [ LENGTH ] [ i ** 2 ] for i in range ( 1 , eulerlib . sqrt ( MAX_SQR_DIGIT_SUM ) ) ) NEW_LINE return f " { ans % MODULUS : 09 } " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . Arrays ; import java . util . HashSet ; import java . util . Set ; public final class p088 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p088 ( ) . run ( ) ) ; } private static final int LIMIT = 12000 ; private int [ ] minSumProduct ; public String run ( ) { minSumProduct = new int [ LIMIT + 1 ] ; Arrays . fill ( minSumProduct , Integer . MAX_VALUE ) ; for ( int i = 2 ; i <= LIMIT * 2 ; i ++ ) factorize ( i , i , i , 0 , 0 ) ; Set < Integer > items = new HashSet < > ( ) ; for ( int i = 2 ; i < minSumProduct . length ; i ++ ) items . add ( minSumProduct [ i ] ) ; int sum = 0 ; for ( int n : items ) sum += n ; return Integer . toString ( sum ) ; } private void factorize ( int n , int remain , int maxFactor , int sum , int terms ) { if ( remain == 1 ) { if ( sum > n ) throw new AssertionError ( ) ; terms += n - sum ; if ( terms <= LIMIT && n < minSumProduct [ terms ] ) minSumProduct [ terms ] = n ; } else { for ( int i = 2 ; i <= maxFactor ; i ++ ) { if ( remain % i == 0 ) { int factor = i ; factorize ( n , remain / factor , Math . min ( factor , maxFactor ) , sum + factor , terms + 1 ) ; } } } } }

def compute ( ) : NEW_LINE INDENT LIMIT = 12000 NEW_LINE minsumproduct = [ None ] * ( LIMIT + 1 ) NEW_LINE def factorize ( n , remain , maxfactor , sum , terms ) : NEW_LINE INDENT if remain == 1 : NEW_LINE INDENT if sum > n : NEW_LINE INDENT raise AssertionError ( ) NEW_LINE DEDENT terms += n - sum NEW_LINE if terms <= LIMIT and ( minsumproduct [ terms ] is None or n < minsumproduct [ terms ] ) : NEW_LINE INDENT minsumproduct [ terms ] = n NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 2 , maxfactor + 1 ) : NEW_LINE INDENT if remain % i == 0 : NEW_LINE INDENT factor = i NEW_LINE factorize ( n , remain // factor , min ( factor , maxfactor ) , sum + factor , terms + 1 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( 2 , LIMIT * 2 + 1 ) : NEW_LINE INDENT factorize ( i , i , i , 0 , 0 ) NEW_LINE DEDENT ans = sum ( set ( minsumproduct [ 2 : ] ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; public final class p055 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p055 ( ) . run ( ) ) ; } public String run ( ) { int count = 0 ; for ( int i = 0 ; i < 10000 ; i ++ ) { if ( isLychrel ( i ) ) count ++ ; } return Integer . toString ( count ) ; } private static boolean isLychrel ( int n ) { BigInteger temp = BigInteger . valueOf ( n ) ; for ( int i = 0 ; i < 49 ; i ++ ) { temp = temp . add ( new BigInteger ( Library . reverse ( temp . toString ( ) ) ) ) ; if ( Library . isPalindrome ( temp . toString ( ) ) ) return false ; } return true ; } }

def compute ( ) : NEW_LINE INDENT ans = sum ( 1 for i in range ( 10000 ) if is_lychrel ( i ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def is_lychrel ( n ) : NEW_LINE INDENT for i in range ( 50 ) : NEW_LINE INDENT n += int ( str ( n ) [ : : - 1 ] ) NEW_LINE if str ( n ) == str ( n ) [ : : - 1 ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p024 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p024 ( ) . run ( ) ) ; } public String run ( ) { int [ ] array = new int [ 10 ] ; for ( int i = 0 ; i < array . length ; i ++ ) array [ i ] = i ; for ( int i = 0 ; i < 999999 ; i ++ ) { if ( ! Library . nextPermutation ( array ) ) throw new AssertionError ( ) ; } String ans = " " ; for ( int i = 0 ; i < array . length ; i ++ ) ans += array [ i ] ; return ans ; } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT arr = list ( range ( 10 ) ) NEW_LINE temp = itertools . islice ( itertools . permutations ( arr ) , 999999 , None ) NEW_LINE return " " . join ( str ( x ) for x in next ( temp ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p036 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p036 ( ) . run ( ) ) ; } public String run ( ) { long sum = 0 ; for ( int i = 1 ; i < 1000000 ; i ++ ) { if ( Library . isPalindrome ( Integer . toString ( i , 10 ) ) && Library . isPalindrome ( Integer . toString ( i , 2 ) ) ) sum += i ; } return Long . toString ( sum ) ; } }

def compute ( ) : NEW_LINE INDENT ans = sum ( i for i in range ( 1000000 ) if is_decimal_binary_palindrome ( i ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def is_decimal_binary_palindrome ( n ) : NEW_LINE INDENT s = str ( n ) NEW_LINE if s != s [ : : - 1 ] : NEW_LINE INDENT return False NEW_LINE DEDENT t = bin ( n ) [ 2 : ] NEW_LINE return t == t [ : : - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p085 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p085 ( ) . run ( ) ) ; } private static final int TARGET = 2000000 ; public String run ( ) { int bestDiff = Integer . MAX_VALUE ; int bestArea = - 1 ; int sqrt = Library . sqrt ( TARGET ) ; for ( int w = 1 ; w <= sqrt ; w ++ ) { for ( int h = 1 ; h <= sqrt ; h ++ ) { int diff = Math . abs ( numberOfRectangles ( w , h ) - TARGET ) ; if ( diff < bestDiff ) { bestDiff = diff ; bestArea = w * h ; } } } return Integer . toString ( bestArea ) ; } private static int numberOfRectangles ( int m , int n ) { return ( m + 1 ) * m * ( n + 1 ) * n / 4 ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT TARGET = 2000000 NEW_LINE end = eulerlib . sqrt ( TARGET ) + 1 NEW_LINE gen = ( ( w , h ) for w in range ( 1 , end ) for h in range ( 1 , end ) ) NEW_LINE func = lambda wh : abs ( num_rectangles ( * wh ) - TARGET ) NEW_LINE ans = min ( gen , key = func ) NEW_LINE return str ( ans [ 0 ] * ans [ 1 ] ) NEW_LINE DEDENT def num_rectangles ( m , n ) : NEW_LINE INDENT return ( m + 1 ) * m * ( n + 1 ) * n // 4 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; import java . util . HashSet ; import java . util . Set ; public final class p346 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p346 ( ) . run ( ) ) ; } private static final long LIMIT = 1_000_000_000_000L ; public String run ( ) { Set < Long > strongRepunits = new HashSet < > ( ) ; strongRepunits . add ( 1L ) ; for ( int length = 3 ; length <= BigInteger . valueOf ( LIMIT ) . bitLength ( ) ; length ++ ) { middle : for ( int base = 2 ; ; base ++ ) { long value = 0 ; for ( int i = 0 ; i < length ; i ++ ) { if ( Long . MAX_VALUE / base < value ) break middle ; value *= base ; if ( value + 1 < value ) break middle ; value ++ ; } if ( value >= LIMIT ) break ; strongRepunits . add ( value ) ; } } long sum = 0 ; for ( long x : strongRepunits ) { if ( sum + x < sum ) throw new ArithmeticException ( " Overflow " ) ; sum += x ; } return Long . toString ( sum ) ; } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 12 NEW_LINE strongrepunits = { 1 } NEW_LINE for length in range ( 3 , LIMIT . bit_length ( ) + 1 ) : NEW_LINE INDENT for base in itertools . count ( 2 ) : NEW_LINE INDENT value = ( base ** length - 1 ) // ( base - 1 ) NEW_LINE if value >= LIMIT : NEW_LINE INDENT break NEW_LINE DEDENT strongrepunits . add ( value ) NEW_LINE DEDENT DEDENT ans = sum ( strongrepunits ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p035 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p035 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 6 ) ; private boolean [ ] isPrime = Library . listPrimality ( LIMIT - 1 ) ; public String run ( ) { int count = 0 ; for ( int i = 0 ; i < isPrime . length ; i ++ ) { if ( isCircularPrime ( i ) ) count ++ ; } return Integer . toString ( count ) ; } private boolean isCircularPrime ( int n ) { String s = Integer . toString ( n ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! isPrime [ Integer . parseInt ( s . substring ( i ) + s . substring ( 0 , i ) ) ] ) return false ; } return true ; } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT isprime = eulerlib . list_primality ( 999999 ) NEW_LINE def is_circular_prime ( n ) : NEW_LINE INDENT s = str ( n ) NEW_LINE return all ( isprime [ int ( s [ i : ] + s [ : i ] ) ] for i in range ( len ( s ) ) ) NEW_LINE DEDENT ans = sum ( 1 for i in range ( len ( isprime ) ) if is_circular_prime ( i ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT