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


import java . math . BigInteger ; import java . util . Arrays ; public final class p104 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p104 ( ) . run ( ) ) ; } public String run ( ) { int i = 0 ; int a = 0 ; int b = 1 ; while ( ! isFound ( i , a ) ) { int c = ( a + b ) % 1000000000 ; a = b ; b = c ; i ++ ; } return Integer . toString ( i ) ; } private static boolean isFound ( int n , int fibMod ) { if ( ! isPandigital ( Integer . toString ( fibMod ) ) ) return false ; BigInteger fib = fibonacci ( n ) [ 0 ] ; if ( fib . mod ( BigInteger . valueOf ( 1000000000 ) ) . intValue ( ) != fibMod ) throw new AssertionError ( ) ; return isPandigital ( leading9Digits ( fib ) ) ; } private static String leading9Digits ( BigInteger x ) { int log10 = ( x . bitLength ( ) - 1 ) * 3 / 10 ; x = x . divide ( BigInteger . TEN . pow ( Math . max ( log10 + 1 - 9 , 0 ) ) ) ; return x . toString ( ) . substring ( 0 , 9 ) ; } private static boolean isPandigital ( String s ) { if ( s . length ( ) != 9 ) return false ; char [ ] temp = s . toCharArray ( ) ; Arrays . sort ( temp ) ; return new String ( temp ) . equals ( "123456789" ) ; } private static BigInteger [ ] fibonacci ( int n ) { if ( n < 0 ) throw new IllegalArgumentException ( ) ; else if ( n == 0 ) return new BigInteger [ ] { BigInteger . ZERO , BigInteger . ONE } ; else { BigInteger [ ] ab = fibonacci ( n / 2 ) ; BigInteger a = ab [ 0 ] ; BigInteger b = ab [ 1 ] ; BigInteger c = a . multiply ( b . shiftLeft ( 1 ) . subtract ( a ) ) ; BigInteger d = a . multiply ( a ) . add ( b . multiply ( b ) ) ; if ( n % 2 == 0 ) return new BigInteger [ ] { c , d } ; else return new BigInteger [ ] { d , c . add ( d ) } ; } } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT MOD = 10 ** 9 NEW_LINE a = 0 NEW_LINE b = 1 NEW_LINE for i in itertools . count ( ) : NEW_LINE INDENT if " " . join ( sorted ( str ( a ) ) ) == "123456789" : NEW_LINE INDENT f = fibonacci ( i ) [ 0 ] NEW_LINE if " " . join ( sorted ( str ( f ) [ : 9 ] ) ) == "123456789" : NEW_LINE INDENT return str ( i ) NEW_LINE DEDENT DEDENT a , b = b , ( a + b ) % MOD NEW_LINE DEDENT return str ( ans ) NEW_LINE DEDENT def fibonacci ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return ( 0 , 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT a , b = fibonacci ( n // 2 ) NEW_LINE c = a * ( b * 2 - a ) NEW_LINE d = a * a + b * b NEW_LINE if n % 2 == 0 : NEW_LINE INDENT return ( c , d ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( d , c + d ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p206 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p206 ( ) . run ( ) ) ; } public String run ( ) { long n = 1000000000 ; int [ ] ndigits = new int [ 10 ] ; int [ ] n2digits = new int [ 19 ] ; long temp = n ; for ( int i = 0 ; i < ndigits . length ; i ++ , temp /= 10 ) ndigits [ i ] = ( int ) ( temp % 10 ) ; temp = n * n ; for ( int i = 0 ; i < n2digits . length ; i ++ , temp /= 10 ) n2digits [ i ] = ( int ) ( temp % 10 ) ; while ( ! isConcealedSquare ( n2digits ) ) { add20n ( ndigits , n2digits ) ; add10Pow ( n2digits , 2 ) ; n += 10 ; add10Pow ( ndigits , 1 ) ; } return Long . toString ( n ) ; } private static boolean isConcealedSquare ( int [ ] n ) { for ( int i = 1 ; i <= 9 ; i ++ ) { if ( n [ 20 - i * 2 ] != i ) return false ; } return n [ 0 ] == 0 ; } private static void add10Pow ( int [ ] n , int i ) { while ( n [ i ] == 9 ) { n [ i ] = 0 ; i ++ ; } n [ i ] ++ ; } private static void add20n ( int [ ] n , int [ ] n2 ) { int carry = 0 ; int i ; for ( i = 0 ; i < n . length ; i ++ ) { int sum = n [ i ] * 2 + n2 [ i + 1 ] + carry ; n2 [ i + 1 ] = sum % 10 ; carry = sum / 10 ; } for ( i ++ ; carry > 0 ; i ++ ) { int sum = n2 [ i ] + carry ; n2 [ i ] = sum % 10 ; carry = sum / 10 ; } } }

def compute ( ) : NEW_LINE INDENT n = 1000000000 NEW_LINE ndigits = [ 0 ] * 10 NEW_LINE temp = n NEW_LINE for i in range ( len ( ndigits ) ) : NEW_LINE INDENT ndigits [ i ] = temp % 10 NEW_LINE temp //= 10 NEW_LINE DEDENT n2digits = [ 0 ] * 19 NEW_LINE temp = n * n NEW_LINE for i in range ( len ( n2digits ) ) : NEW_LINE INDENT n2digits [ i ] = temp % 10 NEW_LINE temp //= 10 NEW_LINE DEDENT while not is_concealed_square ( n2digits ) : NEW_LINE INDENT add_20n ( ndigits , n2digits ) NEW_LINE add_10pow ( n2digits , 2 ) NEW_LINE n += 10 NEW_LINE add_10pow ( ndigits , 1 ) NEW_LINE DEDENT return str ( n ) NEW_LINE DEDENT def is_concealed_square ( n ) : NEW_LINE INDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT if n [ 20 - i * 2 ] != i : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return n [ 0 ] == 0 NEW_LINE DEDENT def add_10pow ( n , i ) : NEW_LINE INDENT while n [ i ] == 9 : NEW_LINE INDENT n [ i ] = 0 NEW_LINE i += 1 NEW_LINE DEDENT n [ i ] += 1 NEW_LINE DEDENT def add_20n ( n , n2 ) : NEW_LINE INDENT carry = 0 NEW_LINE i = 0 NEW_LINE while i < len ( n ) : NEW_LINE INDENT sum = n [ i ] * 2 + n2 [ i + 1 ] + carry NEW_LINE n2 [ i + 1 ] = sum % 10 NEW_LINE carry = sum // 10 NEW_LINE i += 1 NEW_LINE DEDENT i += 1 NEW_LINE while carry > 0 : NEW_LINE INDENT sum = n2 [ i ] + carry NEW_LINE n2 [ i ] = sum % 10 NEW_LINE carry = sum // 10 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p077 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p077 ( ) . run ( ) ) ; } private static final int TARGET = 5000 ; public String run ( ) { for ( int limit = 1 ; ; limit *= 2 ) { int result = search ( limit , TARGET ) ; if ( result != - 1 ) return Integer . toString ( result ) ; } } private static int search ( int limit , int target ) { int [ ] partitions = new int [ limit ] ; partitions [ 0 ] = 1 ; for ( int i = 0 ; i < partitions . length ; i ++ ) { if ( ! Library . isPrime ( i ) ) continue ; for ( int j = i ; j < partitions . length ; j ++ ) partitions [ j ] += partitions [ j - i ] ; } for ( int i = 0 ; i < limit ; i ++ ) { if ( partitions [ i ] > target ) return i ; } return - 1 ; } }

import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT cond = lambda n : num_prime_sum_ways ( n ) > 5000 NEW_LINE ans = next ( filter ( cond , itertools . count ( 2 ) ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT primes = [ 2 ] NEW_LINE def num_prime_sum_ways ( n ) : NEW_LINE INDENT for i in range ( primes [ - 1 ] + 1 , n + 1 ) : NEW_LINE INDENT if eulerlib . is_prime ( i ) : NEW_LINE INDENT primes . append ( i ) NEW_LINE DEDENT DEDENT ways = [ 1 ] + [ 0 ] * n NEW_LINE for p in primes : NEW_LINE INDENT for i in range ( n + 1 - p ) : NEW_LINE INDENT ways [ i + p ] += ways [ i ] NEW_LINE DEDENT DEDENT return ways [ n ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p116 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p116 ( ) . run ( ) ) ; } private static final int LENGTH = 50 ; public String run ( ) { return Long . toString ( countWays ( LENGTH , 2 ) + countWays ( LENGTH , 3 ) + countWays ( LENGTH , 4 ) ) ; } private static long countWays ( int length , int m ) { long [ ] ways = new long [ length + 1 ] ; ways [ 0 ] = 1 ; for ( int n = 1 ; n <= length ; n ++ ) { ways [ n ] += ways [ n - 1 ] ; if ( n >= m ) ways [ n ] += ways [ n - m ] ; } return ways [ length ] - 1 ; } }

def compute ( ) : NEW_LINE INDENT LENGTH = 50 NEW_LINE return str ( sum ( count_ways ( LENGTH , i ) for i in range ( 2 , 5 ) ) ) NEW_LINE DEDENT def count_ways ( length , m ) : NEW_LINE INDENT ways = [ 1 ] + [ 0 ] * length NEW_LINE for n in range ( 1 , len ( ways ) ) : NEW_LINE INDENT ways [ n ] += ways [ n - 1 ] NEW_LINE if n >= m : NEW_LINE INDENT ways [ n ] += ways [ n - m ] NEW_LINE DEDENT DEDENT return ways [ - 1 ] - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; public final class p100 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p100 ( ) . run ( ) ) ; } public String run ( ) { BigInteger x0 = BigInteger . valueOf ( 3 ) ; BigInteger y0 = BigInteger . valueOf ( 1 ) ; BigInteger x = x0 ; BigInteger y = y0 ; while ( true ) { BigInteger sqrt = Library . sqrt ( y . multiply ( y ) . multiply ( BigInteger . valueOf ( 8 ) ) . add ( BigInteger . ONE ) ) ; if ( sqrt . testBit ( 0 ) ) { BigInteger blue = sqrt . add ( BigInteger . ONE ) . divide ( BigInteger . valueOf ( 2 ) ) . add ( y ) ; if ( blue . add ( y ) . compareTo ( BigInteger . TEN . pow ( 12 ) ) > 0 ) return blue . toString ( ) ; } BigInteger nextx = x . multiply ( x0 ) . add ( y . multiply ( y0 ) . multiply ( BigInteger . valueOf ( 8 ) ) ) ; BigInteger nexty = x . multiply ( y0 ) . add ( y . multiply ( x0 ) ) ; x = nextx ; y = nexty ; } } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT x0 = 3 NEW_LINE y0 = 1 NEW_LINE x = x0 NEW_LINE y = y0 NEW_LINE while True : NEW_LINE INDENT sqrt = eulerlib . sqrt ( y ** 2 * 8 + 1 ) NEW_LINE if sqrt % 2 == 1 : NEW_LINE INDENT blue = ( sqrt + 1 ) // 2 + y NEW_LINE if blue + y > 10 ** 12 : NEW_LINE INDENT return str ( blue ) NEW_LINE DEDENT DEDENT nextx = x * x0 + y * y0 * 8 NEW_LINE nexty = x * y0 + y * x0 NEW_LINE x = nextx NEW_LINE y = nexty NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p222 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p222 ( ) . run ( ) ) ; } public String run ( ) { sphereRadii = new double [ 21 ] ; for ( int i = 0 ; i < sphereRadii . length ; i ++ ) sphereRadii [ i ] = ( i + 30 ) * 1000 ; minLength = new double [ sphereRadii . length ] [ 1 << sphereRadii . length ] ; double min = Double . POSITIVE_INFINITY ; for ( int i = 0 ; i < sphereRadii . length ; i ++ ) min = Math . min ( findMinimumLength ( i , ( 1 << sphereRadii . length ) - 1 ) + sphereRadii [ i ] , min ) ; return Long . toString ( Math . round ( min ) ) ; } private double [ ] sphereRadii ; private double [ ] [ ] minLength ; private double findMinimumLength ( int currentSphereIndex , int setOfSpheres ) { if ( ( setOfSpheres & ( 1 << currentSphereIndex ) ) == 0 ) throw new IllegalArgumentException ( ) ; if ( minLength [ currentSphereIndex ] [ setOfSpheres ] == 0 ) { double result ; if ( Integer . bitCount ( setOfSpheres ) == 1 ) result = sphereRadii [ currentSphereIndex ] ; else { result = Double . POSITIVE_INFINITY ; int newSetOfSpheres = setOfSpheres ^ ( 1 << currentSphereIndex ) ; for ( int i = 0 ; i < sphereRadii . length ; i ++ ) { if ( ( newSetOfSpheres & ( 1 << i ) ) == 0 ) continue ; double temp = Math . sqrt ( ( sphereRadii [ i ] + sphereRadii [ currentSphereIndex ] - 50000 ) * 200000 ) ; temp += findMinimumLength ( i , newSetOfSpheres ) ; result = Math . min ( temp , result ) ; } } minLength [ currentSphereIndex ] [ setOfSpheres ] = result ; } return minLength [ currentSphereIndex ] [ setOfSpheres ] ; } }

import eulerlib , math NEW_LINE def compute ( ) : NEW_LINE INDENT NUM_SPHERES = 21 NEW_LINE sphereradii = [ ( i + 30 ) * 1000 for i in range ( NUM_SPHERES ) ] NEW_LINE minlength = [ [ None ] * ( 2 ** NUM_SPHERES ) for _ in range ( NUM_SPHERES ) ] NEW_LINE def find_minimum_length ( currentsphereindex , setofspheres ) : NEW_LINE INDENT if setofspheres & ( 1 << currentsphereindex ) == 0 : NEW_LINE INDENT raise ValueError ( ) NEW_LINE DEDENT if minlength [ currentsphereindex ] [ setofspheres ] is None : NEW_LINE INDENT if eulerlib . popcount ( setofspheres ) == 1 : NEW_LINE INDENT result = sphereradii [ currentsphereindex ] NEW_LINE DEDENT else : NEW_LINE INDENT result = float ( " inf " ) NEW_LINE newsetofspheres = setofspheres ^ ( 1 << currentsphereindex ) NEW_LINE for i in range ( NUM_SPHERES ) : NEW_LINE INDENT if newsetofspheres & ( 1 << i ) == 0 : NEW_LINE INDENT continue NEW_LINE DEDENT temp = math . sqrt ( ( sphereradii [ i ] + sphereradii [ currentsphereindex ] - 50000 ) * 200000 ) NEW_LINE temp += find_minimum_length ( i , newsetofspheres ) NEW_LINE result = min ( temp , result ) NEW_LINE DEDENT DEDENT minlength [ currentsphereindex ] [ setofspheres ] = result NEW_LINE DEDENT return minlength [ currentsphereindex ] [ setofspheres ] NEW_LINE DEDENT ans = min ( ( find_minimum_length ( i , ( 1 << NUM_SPHERES ) - 1 ) + sphereradii [ i ] ) for i in range ( NUM_SPHERES ) ) NEW_LINE return str ( int ( round ( 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 p074 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p074 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 6 ) ; public String run ( ) { int count = 0 ; for ( int i = 0 ; i < LIMIT ; i ++ ) { if ( getChainLength ( i ) == 60 ) count ++ ; } return Integer . toString ( count ) ; } private static int getChainLength ( int n ) { Set < Integer > seen = new HashSet < > ( ) ; while ( true ) { if ( ! seen . add ( n ) ) return seen . size ( ) ; n = factorialize ( n ) ; } } private static int [ ] FACTORIAL = { 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 } ; private static int factorialize ( int n ) { int sum = 0 ; for ( ; n != 0 ; n /= 10 ) sum += FACTORIAL [ n % 10 ] ; return sum ; } }

import math NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 6 NEW_LINE ans = sum ( 1 for i in range ( LIMIT ) if get_chain_length ( i ) == 60 ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def get_chain_length ( n ) : NEW_LINE INDENT seen = set ( ) NEW_LINE while True : NEW_LINE INDENT seen . add ( n ) NEW_LINE n = factorialize ( n ) NEW_LINE if n in seen : NEW_LINE INDENT return len ( seen ) NEW_LINE DEDENT DEDENT DEDENT def factorialize ( n ) : NEW_LINE INDENT result = 0 NEW_LINE while n != 0 : NEW_LINE INDENT result += FACTORIAL [ n % 10 ] NEW_LINE n //= 10 NEW_LINE DEDENT return result NEW_LINE DEDENT FACTORIAL = [ math . factorial ( i ) for i in range ( 10 ) ] NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


class Solution { private TreeNode ans ; public Solution ( ) { this . ans = null ; } private boolean recurseTree ( TreeNode currentNode , TreeNode p , TreeNode q ) { if ( currentNode == null ) { return false ; } int left = this . recurseTree ( currentNode . left , p , q ) ? 1 : 0 ; int right = this . recurseTree ( currentNode . right , p , q ) ? 1 : 0 ; int mid = ( currentNode == p || currentNode == q ) ? 1 : 0 ; if ( mid + left + right >= 2 ) { this . ans = currentNode ; } return ( mid + left + right > 0 ) ; } public TreeNode lowestCommonAncestor ( TreeNode root , TreeNode p , TreeNode q ) { this . recurseTree ( root , p , q ) ; return this . ans ; } }

class Solution ( object ) : NEW_LINE INDENT def lowestCommonAncestor ( self , root , p , q ) : NEW_LINE INDENT stack = [ root ] NEW_LINE parent = { root : None } NEW_LINE while p not in parent or q not in parent : NEW_LINE INDENT node = stack . pop ( ) NEW_LINE if node . left : NEW_LINE INDENT parent [ node . left ] = node NEW_LINE stack . append ( node . left ) NEW_LINE DEDENT if node . right : NEW_LINE INDENT parent [ node . right ] = node NEW_LINE stack . append ( node . right ) NEW_LINE DEDENT DEDENT ancestors = set ( ) NEW_LINE while p : NEW_LINE INDENT ancestors . add ( p ) NEW_LINE p = parent [ p ] NEW_LINE DEDENT while q not in ancestors : NEW_LINE INDENT q = parent [ q ] NEW_LINE DEDENT return q NEW_LINE DEDENT DEDENT


public class Solution { public int findRadius ( int [ ] houses , int [ ] heaters ) { Arrays . sort ( heaters ) ; int result = Integer . MIN_VALUE ; for ( int house : houses ) { int index = Arrays . binarySearch ( heaters , house ) ; if ( index < 0 ) index = - ( index + 1 ) ; int dist1 = index - 1 >= 0 ? house - heaters [ index - 1 ] : Integer . MAX_VALUE ; int dist2 = index < heaters . length ? heaters [ index ] - house : Integer . MAX_VALUE ; result = Math . max ( result , Math . min ( dist1 , dist2 ) ) ; } return result ; } }

class Solution ( object ) : NEW_LINE INDENT def findRadius ( self , houses , heaters ) : NEW_LINE INDENT heaters = sorted ( heaters ) + [ float ( ' inf ' ) ] NEW_LINE i = r = 0 NEW_LINE for x in sorted ( houses ) : NEW_LINE INDENT while x >= sum ( heaters [ i : i + 2 ] ) / 2. : NEW_LINE INDENT i += 1 NEW_LINE DEDENT r = max ( r , abs ( heaters [ i ] - x ) ) NEW_LINE DEDENT return r NEW_LINE DEDENT DEDENT


class Solution { private class LargerNumberComparator implements Comparator < String > { @ Override public int compare ( String a , String b ) { String order1 = a + b ; String order2 = b + a ; return order2 . compareTo ( order1 ) ; } } public String largestNumber ( int [ ] nums ) { String [ ] asStrs = new String [ nums . length ] ; for ( int i = 0 ; i < nums . length ; i ++ ) { asStrs [ i ] = String . valueOf ( nums [ i ] ) ; } Arrays . sort ( asStrs , new LargerNumberComparator ( ) ) ; if ( asStrs [ 0 ] . equals ( "0" ) ) { return "0" ; } String largestNumberStr = new String ( ) ; for ( String numAsStr : asStrs ) { largestNumberStr += numAsStr ; } return largestNumberStr ; } }

class LargerNumKey ( str ) : NEW_LINE INDENT def __lt__ ( x , y ) : NEW_LINE INDENT return x + y > y + x NEW_LINE DEDENT DEDENT class Solution : NEW_LINE INDENT def largestNumber ( self , nums ) : NEW_LINE INDENT largest_num = ' ' . join ( sorted ( map ( str , nums ) , key = LargerNumKey ) ) NEW_LINE return '0' if largest_num [ 0 ] == '0' else largest_num NEW_LINE DEDENT DEDENT


class Solution { public int largestPalindrome ( int n ) { if ( n == 1 ) { return 9 ; } int upperBound = ( int ) Math . pow ( 10 , n ) - 1 , lowerBound = upperBound / 10 ; long maxNumber = ( long ) upperBound * ( long ) upperBound ; int firstHalf = ( int ) ( maxNumber / ( long ) Math . pow ( 10 , n ) ) ; boolean palindromFound = false ; long palindrom = 0 ; while ( ! palindromFound ) { palindrom = createPalindrom ( firstHalf ) ; for ( long i = upperBound ; upperBound > lowerBound ; i -- ) { if ( palindrom / i > maxNumber || i * i < palindrom ) { break ; } if ( palindrom % i == 0 ) { palindromFound = true ; break ; } } firstHalf -- ; } return ( int ) ( palindrom % 1337 ) ; } private long createPalindrom ( long num ) { String str = num + new StringBuilder ( ) . append ( num ) . reverse ( ) . toString ( ) ; return Long . parseLong ( str ) ; } }

class Solution ( object ) : NEW_LINE INDENT def largestPalindrome ( self , n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return 9 NEW_LINE DEDENT for a in xrange ( 2 , 9 * 10 ** ( n - 1 ) ) : NEW_LINE INDENT hi = ( 10 ** n ) - a NEW_LINE lo = int ( str ( hi ) [ : : - 1 ] ) NEW_LINE if a ** 2 - 4 * lo < 0 : NEW_LINE INDENT continue NEW_LINE DEDENT if ( a ** 2 - 4 * lo ) ** .5 == int ( ( a ** 2 - 4 * lo ) ** .5 ) : NEW_LINE INDENT return ( lo + 10 ** n * ( 10 ** n - a ) ) % 1337 NEW_LINE DEDENT DEDENT DEDENT DEDENT


class Solution { public int binaryGap ( int N ) { int last = - 1 , ans = 0 ; for ( int i = 0 ; i < 32 ; ++ i ) if ( ( ( N >> i ) & 1 ) > 0 ) { if ( last >= 0 ) ans = Math . max ( ans , i - last ) ; last = i ; } return ans ; } }

class Solution : NEW_LINE INDENT def binaryGap ( self , n : int ) -> int : NEW_LINE INDENT current = 1 NEW_LINE last1 = - 1 NEW_LINE out = 0 NEW_LINE while n > 0 : NEW_LINE INDENT if n % 2 == 1 : NEW_LINE INDENT if last1 >= 1 : NEW_LINE INDENT out = max ( out , current - last1 ) NEW_LINE DEDENT last1 = current NEW_LINE DEDENT current += 1 NEW_LINE n = n // 2 NEW_LINE DEDENT return out NEW_LINE DEDENT DEDENT


class Solution { public int [ ] anagramMappings ( int [ ] A , int [ ] B ) { int [ ] ans = new int [ A . length ] ; HashMap < Integer , Integer > valIndex = new HashMap < > ( ) ; for ( int i = 0 ; i < B . length ; i ++ ) valIndex . put ( B [ i ] , i ) ; for ( int i = 0 ; i < A . length ; i ++ ) ans [ i ] = valIndex . get ( A [ i ] ) ; return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def anagramMappings ( self , A , B ) : NEW_LINE INDENT val_index = { } NEW_LINE ans = [ ] NEW_LINE for i , n in enumerate ( B ) : NEW_LINE INDENT val_index [ n ] = i NEW_LINE DEDENT for n in A : NEW_LINE INDENT ans . append ( val_index [ n ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT


public class Solution { public String addStrings ( String num1 , String num2 ) { StringBuilder sb = new StringBuilder ( ) ; int carry = 0 ; for ( int i = num1 . length ( ) - 1 , j = num2 . length ( ) - 1 ; i >= 0 || j >= 0 || carry == 1 ; i -- , j -- ) { int x = i < 0 ? 0 : num1 . charAt ( i ) - '0' ; int y = j < 0 ? 0 : num2 . charAt ( j ) - '0' ; sb . append ( ( x + y + carry ) % 10 ) ; carry = ( x + y + carry ) / 10 ; } return sb . reverse ( ) . toString ( ) ; } }

class Solution ( object ) : NEW_LINE INDENT def addStrings ( self , num1 , num2 ) : NEW_LINE INDENT res = [ ] NEW_LINE pos1 = len ( num1 ) - 1 NEW_LINE pos2 = len ( num2 ) - 1 NEW_LINE carry = 0 NEW_LINE while pos1 >= 0 or pos2 >= 0 or carry == 1 : NEW_LINE INDENT digit1 = digit2 = 0 NEW_LINE if pos1 >= 0 : NEW_LINE INDENT digit1 = ord ( num1 [ pos1 ] ) - ord ( '0' ) NEW_LINE DEDENT if pos2 >= 0 : NEW_LINE INDENT digit2 = ord ( num2 [ pos2 ] ) - ord ( '0' ) NEW_LINE DEDENT res . append ( str ( ( digit1 + digit2 + carry ) % 10 ) ) NEW_LINE carry = ( digit1 + digit2 + carry ) / 10 NEW_LINE pos1 -= 1 NEW_LINE pos2 -= 1 NEW_LINE DEDENT return ' ' . join ( res [ : : - 1 ] ) NEW_LINE DEDENT DEDENT


public class Solution { public int subarraySum ( int [ ] nums , int k ) { int count = 0 , sum = 0 ; HashMap < Integer , Integer > map = new HashMap < > ( ) ; map . put ( 0 , 1 ) ; for ( int i = 0 ; i < nums . length ; i ++ ) { sum += nums [ i ] ; if ( map . containsKey ( sum - k ) ) count += map . get ( sum - k ) ; map . put ( sum , map . getOrDefault ( sum , 0 ) + 1 ) ; } return count ; } }

class Solution ( object ) : NEW_LINE INDENT def subarraySum ( self , nums , k ) : NEW_LINE INDENT sum_map = { } NEW_LINE sum_map [ 0 ] = 1 NEW_LINE count = curr_sum = 0 NEW_LINE for num in nums : NEW_LINE INDENT curr_sum += num NEW_LINE count += sum_map . get ( curr_sum - k , 0 ) NEW_LINE sum_map [ curr_sum ] = sum_map . get ( curr_sum , 0 ) + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT DEDENT


class Solution { public int findShortestSubArray ( int [ ] nums ) { Map < Integer , Integer > left = new HashMap ( ) , right = new HashMap ( ) , count = new HashMap ( ) ; for ( int i = 0 ; i < nums . length ; i ++ ) { int x = nums [ i ] ; if ( left . get ( x ) == null ) left . put ( x , i ) ; right . put ( x , i ) ; count . put ( x , count . getOrDefault ( x , 0 ) + 1 ) ; } int ans = nums . length ; int degree = Collections . max ( count . values ( ) ) ; for ( int x : count . keySet ( ) ) { if ( count . get ( x ) == degree ) { ans = Math . min ( ans , right . get ( x ) - left . get ( x ) + 1 ) ; } } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def findShortestSubArray ( self , nums ) : NEW_LINE INDENT left , right , count = { } , { } , { } NEW_LINE for i , x in enumerate ( nums ) : NEW_LINE INDENT if x not in left : left [ x ] = i NEW_LINE right [ x ] = i NEW_LINE count [ x ] = count . get ( x , 0 ) + 1 NEW_LINE DEDENT ans = len ( nums ) NEW_LINE degree = max ( count . values ( ) ) NEW_LINE for x in count : NEW_LINE INDENT if count [ x ] == degree : NEW_LINE INDENT ans = min ( ans , right [ x ] - left [ x ] + 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT


public class Solution { public int maximumProduct ( int [ ] nums ) { int min1 = Integer . MAX_VALUE , min2 = Integer . MAX_VALUE ; int max1 = Integer . MIN_VALUE , max2 = Integer . MIN_VALUE , max3 = Integer . MIN_VALUE ; for ( int n : nums ) { if ( n <= min1 ) { min2 = min1 ; min1 = n ; } else if ( n <= min2 ) { min2 = n ; } if ( n >= max1 ) { max3 = max2 ; max2 = max1 ; max1 = n ; } else if ( n >= max2 ) { max3 = max2 ; max2 = n ; } else if ( n >= max3 ) { max3 = n ; } } return Math . max ( min1 * min2 * max1 , max1 * max2 * max3 ) ; } }

class Solution ( object ) : NEW_LINE INDENT def maximumProduct ( self , nums ) : NEW_LINE INDENT min1 = min2 = float ( ' inf ' ) NEW_LINE max1 = max2 = max3 = float ( ' - inf ' ) NEW_LINE for num in nums : NEW_LINE INDENT if num <= min1 : NEW_LINE INDENT min2 = min1 NEW_LINE min1 = num NEW_LINE DEDENT elif num <= min2 : NEW_LINE INDENT min2 = num NEW_LINE DEDENT if num >= max1 : NEW_LINE INDENT max3 = max2 NEW_LINE max2 = max1 NEW_LINE max1 = num NEW_LINE DEDENT elif num >= max2 : NEW_LINE INDENT max3 = max2 NEW_LINE max2 = num NEW_LINE DEDENT elif num >= max3 : NEW_LINE INDENT max3 = num NEW_LINE DEDENT DEDENT return max ( min1 * min2 * max1 , max1 * max2 * max3 ) NEW_LINE DEDENT DEDENT


public class Solution { public TreeNode constructMaximumBinaryTree ( int [ ] nums ) { return construct ( nums , 0 , nums . length ) ; } public TreeNode construct ( int [ ] nums , int l , int r ) { if ( l == r ) return null ; int max_i = max ( nums , l , r ) ; TreeNode root = new TreeNode ( nums [ max_i ] ) ; root . left = construct ( nums , l , max_i ) ; root . right = construct ( nums , max_i + 1 , r ) ; return root ; } public int max ( int [ ] nums , int l , int r ) { int max_i = l ; for ( int i = l ; i < r ; i ++ ) { if ( nums [ max_i ] < nums [ i ] ) max_i = i ; } return max_i ; } }

class Solution ( object ) : NEW_LINE INDENT def constructMaximumBinaryTree ( self , nums ) : NEW_LINE INDENT if nums is None or len ( nums ) == 0 : NEW_LINE INDENT return None NEW_LINE DEDENT max_index , max_value = 0 , 0 NEW_LINE for i , value in enumerate ( nums ) : NEW_LINE INDENT if value >= max_value : NEW_LINE INDENT max_value = value NEW_LINE max_index = i NEW_LINE DEDENT DEDENT root = TreeNode ( max_value ) NEW_LINE root . left = self . constructMaximumBinaryTree ( nums [ : max_index ] ) NEW_LINE root . right = self . constructMaximumBinaryTree ( nums [ max_index + 1 : ] ) NEW_LINE return root NEW_LINE DEDENT DEDENT


class Solution { public int firstUniqChar ( String s ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) freq [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( freq [ s . charAt ( i ) - ' a ' ] == 1 ) return i ; return - 1 ; } }

class Solution ( object ) : NEW_LINE INDENT def firstUniqChar ( self , s ) : NEW_LINE INDENT count_map = { } NEW_LINE for c in s : NEW_LINE INDENT count_map [ c ] = count_map . get ( c , 0 ) + 1 NEW_LINE DEDENT for i , c in enumerate ( s ) : NEW_LINE INDENT if count_map [ c ] == 1 : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT DEDENT


class Solution { public boolean isPalindromeRange ( String s , int i , int j ) { for ( int k = i ; k <= i + ( j - i ) / 2 ; k ++ ) { if ( s . charAt ( k ) != s . charAt ( j - k + i ) ) return false ; } return true ; } public boolean validPalindrome ( String s ) { for ( int i = 0 ; i < s . length ( ) / 2 ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( s . length ( ) - 1 - i ) ) { int j = s . length ( ) - 1 - i ; return ( isPalindromeRange ( s , i + 1 , j ) || isPalindromeRange ( s , i , j - 1 ) ) ; } } return true ; } }

class Solution ( object ) : NEW_LINE INDENT def validPalindrome ( self , s ) : NEW_LINE INDENT return self . validPalindromeHelper ( s , 0 , len ( s ) - 1 , 1 ) NEW_LINE DEDENT def validPalindromeHelper ( self , s , left , right , budget ) : NEW_LINE INDENT while left < len ( s ) and right >= 0 and left <= right and s [ left ] == s [ right ] : NEW_LINE INDENT left += 1 NEW_LINE right -= 1 NEW_LINE DEDENT if left >= len ( s ) or right < 0 or left >= right : NEW_LINE INDENT return True NEW_LINE DEDENT if budget == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT budget -= 1 NEW_LINE return self . validPalindromeHelper ( s , left + 1 , right , budget ) or self . validPalindromeHelper ( s , left , right - 1 , budget ) NEW_LINE DEDENT DEDENT


class Solution { private List < Integer > memo ; public Solution ( ) { memo = new ArrayList ( ) ; memo . add ( 0 ) ; memo . add ( 1 ) ; } public int fib ( int N ) { if ( N < memo . size ( ) ) return memo . get ( N ) ; for ( int i = memo . size ( ) ; i <= N ; i ++ ) { memo . add ( memo . get ( i - 1 ) + memo . get ( i - 2 ) ) ; } return memo . get ( N ) ; } }

class Solution ( object ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . memo = [ ] NEW_LINE self . memo . append ( 0 ) NEW_LINE self . memo . append ( 1 ) NEW_LINE DEDENT def fib ( self , N ) : NEW_LINE INDENT if N < len ( self . memo ) : NEW_LINE INDENT return self . memo [ N ] NEW_LINE DEDENT for i in range ( len ( self . memo ) , N + 1 ) : NEW_LINE INDENT self . memo . append ( self . memo [ i - 1 ] + self . memo [ i - 2 ] ) NEW_LINE DEDENT return self . memo [ N ] NEW_LINE DEDENT DEDENT


class Solution { public int [ ] [ ] transpose ( int [ ] [ ] A ) { int R = A . length , C = A [ 0 ] . length ; int [ ] [ ] ans = new int [ C ] [ R ] ; for ( int r = 0 ; r < R ; ++ r ) for ( int c = 0 ; c < C ; ++ c ) { ans [ c ] [ r ] = A [ r ] [ c ] ; } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def transpose ( self , A ) : NEW_LINE INDENT R , C = len ( A ) , len ( A [ 0 ] ) NEW_LINE ans = [ [ None ] * R for _ in xrange ( C ) ] NEW_LINE for r , row in enumerate ( A ) : NEW_LINE INDENT for c , val in enumerate ( row ) : NEW_LINE INDENT ans [ c ] [ r ] = val NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT


class Solution { public int totalFruit ( int [ ] tree ) { int ans = 0 , i = 0 ; Counter count = new Counter ( ) ; for ( int j = 0 ; j < tree . length ; ++ j ) { count . add ( tree [ j ] , 1 ) ; while ( count . size ( ) >= 3 ) { count . add ( tree [ i ] , - 1 ) ; if ( count . get ( tree [ i ] ) == 0 ) count . remove ( tree [ i ] ) ; i ++ ; } ans = Math . max ( ans , j - i + 1 ) ; } return ans ; } } class Counter extends HashMap < Integer , Integer > { public int get ( int k ) { return containsKey ( k ) ? super . get ( k ) : 0 ; } public void add ( int k , int v ) { put ( k , get ( k ) + v ) ; } }

class Solution ( object ) : NEW_LINE INDENT def totalFruit ( self , tree ) : NEW_LINE INDENT ans = i = 0 NEW_LINE count = collections . Counter ( ) NEW_LINE for j , x in enumerate ( tree ) : NEW_LINE INDENT count [ x ] += 1 NEW_LINE while len ( count ) >= 3 : NEW_LINE INDENT count [ tree [ i ] ] -= 1 NEW_LINE if count [ tree [ i ] ] == 0 : NEW_LINE INDENT del count [ tree [ i ] ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT ans = max ( ans , j - i + 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT


class Solution { public boolean canConstruct ( String ransomNote , String magazine ) { int [ ] table = new int [ 128 ] ; for ( char c : magazine . toCharArray ( ) ) table [ c ] ++ ; for ( char c : ransomNote . toCharArray ( ) ) if ( -- table [ c ] < 0 ) return false ; return true ; } }

class Solution ( object ) : NEW_LINE INDENT def canConstruct ( self , ransomNote , magazine ) : NEW_LINE INDENT letter_map = { } NEW_LINE for letter in magazine : NEW_LINE INDENT letter_map [ letter ] = letter_map . get ( letter , 0 ) + 1 NEW_LINE DEDENT for letter in ransomNote : NEW_LINE INDENT letter_map [ letter ] = letter_map . get ( letter , 0 ) - 1 NEW_LINE if letter_map [ letter ] < 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT DEDENT


class Solution { public ListNode reverseList ( ListNode head ) { ListNode newHead = null ; while ( head != null ) { ListNode next = head . next ; head . next = newHead ; newHead = head ; head = next ; } return newHead ; } }

class Solution ( object ) : NEW_LINE INDENT def reverseList ( self , head ) : NEW_LINE INDENT if head is None or head . next is None : NEW_LINE INDENT return head NEW_LINE DEDENT p = self . reverseList ( head . next ) NEW_LINE head . next . next = head NEW_LINE head . next = None NEW_LINE return p NEW_LINE DEDENT DEDENT


class Solution { public boolean enough ( int x , int m , int n , int k ) { int count = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { count += Math . min ( x / i , n ) ; } return count >= k ; } public int findKthNumber ( int m , int n , int k ) { int lo = 1 , hi = m * n ; while ( lo < hi ) { int mi = lo + ( hi - lo ) / 2 ; if ( ! enough ( mi , m , n , k ) ) lo = mi + 1 ; else hi = mi ; } return lo ; } }

class Solution : NEW_LINE INDENT def findKthNumber ( self , m : int , n : int , k : int ) -> int : NEW_LINE INDENT def enough ( x ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT count += min ( x // i , n ) NEW_LINE DEDENT return count >= k NEW_LINE DEDENT lo , hi = 1 , m * n NEW_LINE while lo < hi : NEW_LINE INDENT mi = ( lo + hi ) // 2 NEW_LINE if not enough ( mi ) : NEW_LINE INDENT lo = mi + 1 NEW_LINE DEDENT else : NEW_LINE INDENT hi = mi NEW_LINE DEDENT DEDENT return lo NEW_LINE DEDENT DEDENT


class Solution { public List < Integer > findAnagrams ( String s , String p ) { List < Integer > list = new ArrayList < > ( ) ; if ( s == null || s . length ( ) == 0 || p == null || p . length ( ) == 0 ) return list ; int [ ] hash = new int [ 256 ] ; for ( char c : p . toCharArray ( ) ) { hash [ c ] ++ ; } int left = 0 , right = 0 , count = p . length ( ) ; while ( right < s . length ( ) ) { if ( hash [ s . charAt ( right ++ ) ] -- >= 1 ) count -- ; if ( count == 0 ) list . add ( left ) ; if ( right - left == p . length ( ) && hash [ s . charAt ( left ++ ) ] ++ >= 0 ) count ++ ; } return list ; } }

class Solution ( object ) : NEW_LINE INDENT def findAnagrams ( self , s , p ) : NEW_LINE INDENT res = [ ] NEW_LINE if s is None or p is None or len ( s ) == 0 or len ( p ) == 0 : NEW_LINE INDENT return res NEW_LINE DEDENT char_map = [ 0 ] * 256 NEW_LINE for c in p : NEW_LINE INDENT char_map [ ord ( c ) ] += 1 NEW_LINE DEDENT left , right , count = 0 , 0 , len ( p ) NEW_LINE while right < len ( s ) : NEW_LINE INDENT if char_map [ ord ( s [ right ] ) ] >= 1 : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT char_map [ ord ( s [ right ] ) ] -= 1 NEW_LINE right += 1 NEW_LINE if count == 0 : NEW_LINE INDENT res . append ( left ) NEW_LINE DEDENT if right - left == len ( p ) : NEW_LINE INDENT if char_map [ ord ( s [ left ] ) ] >= 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT char_map [ ord ( s [ left ] ) ] += 1 NEW_LINE left += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT DEDENT


public class Solution { public TreeNode mergeTrees ( TreeNode t1 , TreeNode t2 ) { if ( t1 == null ) return t2 ; if ( t2 == null ) return t1 ; t1 . val += t2 . val ; t1 . left = mergeTrees ( t1 . left , t2 . left ) ; t1 . right = mergeTrees ( t1 . right , t2 . right ) ; return t1 ; } }

class Solution ( object ) : NEW_LINE INDENT def mergeTrees ( self , t1 , t2 ) : NEW_LINE INDENT if t1 is None : NEW_LINE INDENT return t2 NEW_LINE DEDENT if t2 is None : NEW_LINE INDENT return t1 NEW_LINE DEDENT t1 . val += t2 . val NEW_LINE t1 . left = self . mergeTrees ( t1 . left , t2 . left ) NEW_LINE t1 . right = self . mergeTrees ( t1 . right , t2 . right ) NEW_LINE return t1 NEW_LINE DEDENT DEDENT