id
stringlengths 2
6
| java
stringlengths 48
5.92k
| python
stringlengths 33
11.1k
|
---|---|---|
T40200 | import java . util . * ; class GFG { public static boolean isDivisible ( String S ) { int n = S . length ( ) ; if ( S . charAt ( n - 1 ) != '5' && S . charAt ( n - 1 ) != '0' ) return false ; int sum = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) sum += ( int ) S . charAt ( i ) ; if ( sum % 3 == 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { String S = "15645746327462384723984023940239" ; if ( isDivisible ( S ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; String S1 = "15645746327462384723984023940235" ; if ( isDivisible ( S1 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| def accumulate ( s ) : NEW_LINE INDENT acc = 0 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT acc += ord ( s [ i ] ) - 48 ; NEW_LINE DEDENT return acc ; NEW_LINE DEDENT def isDivisible ( s ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE if ( s [ n - 1 ] != '5' and s [ n - 1 ] != '0' ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT sum = accumulate ( s ) ; NEW_LINE return ( sum % 3 == 0 ) ; NEW_LINE DEDENT s = "15645746327462384723984023940239" ; NEW_LINE if isDivisible ( s ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT s = "15645746327462384723984023940235" ; NEW_LINE if isDivisible ( s ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
|
T40201 | import java . util . Arrays ; class GFG { static boolean modularSum ( int arr [ ] , int n , int m ) { if ( n > m ) return true ; boolean DP [ ] = new boolean [ m ] ; Arrays . fill ( DP , false ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( DP [ 0 ] ) return true ; boolean temp [ ] = new boolean [ m ] ; Arrays . fill ( temp , false ) ; for ( int j = 0 ; j < m ; j ++ ) { if ( DP [ j ] == true ) { if ( DP [ ( j + arr [ i ] ) % m ] == false ) temp [ ( j + arr [ i ] ) % m ] = true ; } } for ( int j = 0 ; j < m ; j ++ ) if ( temp [ j ] ) DP [ j ] = true ; DP [ arr [ i ] % m ] = true ; } return DP [ 0 ] ; } public static void main ( String arg [ ] ) { int arr [ ] = { 1 , 7 } ; int n = arr . length ; int m = 5 ; if ( modularSum ( arr , n , m ) ) System . out . print ( " YES \n " ) ; else System . out . print ( " NO \n " ) ; } }
| def modularSum ( arr , n , m ) : NEW_LINE INDENT if ( n > m ) : NEW_LINE INDENT return True NEW_LINE DEDENT DP = [ False for i in range ( m ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( DP [ 0 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT temp = [ False for i in range ( m ) ] NEW_LINE for j in range ( m ) : NEW_LINE INDENT if ( DP [ j ] == True ) : NEW_LINE INDENT if ( DP [ ( j + arr [ i ] ) % m ] == False ) : NEW_LINE INDENT temp [ ( j + arr [ i ] ) % m ] = True NEW_LINE DEDENT DEDENT DEDENT for j in range ( m ) : NEW_LINE INDENT if ( temp [ j ] ) : NEW_LINE INDENT DP [ j ] = True NEW_LINE DEDENT DEDENT DP [ arr [ i ] % m ] = True NEW_LINE DEDENT return DP [ 0 ] NEW_LINE DEDENT arr = [ 1 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE m = 5 NEW_LINE print ( " YES " ) if ( modularSum ( arr , n , m ) ) else print ( " NO " ) NEW_LINE
|
T40202 | class GFG { static boolean isWordPresent ( String sentence , String word ) { String [ ] s = sentence . split ( " β " ) ; for ( String temp : s ) { if ( temp . compareTo ( word ) == 0 ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String s = " Geeks β for β Geeks " ; String word = " Geeks " ; if ( isWordPresent ( s , word ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
| def isWordPresent ( sentence , word ) : NEW_LINE INDENT s = sentence . split ( " β " ) NEW_LINE for i in s : NEW_LINE INDENT if ( i == word ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT s = " Geeks β for β Geeks " NEW_LINE word = " Geeks " NEW_LINE if ( isWordPresent ( s , word ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
|
T40203 | import java . io . * ; class GFG { static void pattern ( int n ) { int i , j ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= ( 2 * n ) ; j ++ ) { if ( i < j ) System . out . print ( " β " ) ; else System . out . print ( " * " ) ; if ( i <= ( ( 2 * n ) - j ) ) System . out . print ( " β " ) ; else System . out . print ( " * " ) ; } System . out . println ( " " ) ; } for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= ( 2 * n ) ; j ++ ) { if ( i > ( n - j + 1 ) ) System . out . print ( " β " ) ; else System . out . print ( " * " ) ; if ( ( i + n ) > j ) System . out . print ( " β " ) ; else System . out . print ( " * " ) ; } System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { pattern ( 7 ) ; } }
| def pattern ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , 2 * n + 1 ) : NEW_LINE INDENT if ( i < j ) : NEW_LINE INDENT print ( " " , end = " β " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = " " ) ; NEW_LINE DEDENT if ( i <= ( ( 2 * n ) - j ) ) : NEW_LINE INDENT print ( " " , end = " β " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = " " ) ; NEW_LINE DEDENT DEDENT print ( " " ) ; NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , 2 * n + 1 ) : NEW_LINE INDENT if ( i > ( n - j + 1 ) ) : NEW_LINE INDENT print ( " " , end = " β " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = " " ) ; NEW_LINE DEDENT if ( ( i + n ) > j ) : NEW_LINE INDENT print ( " " , end = " β " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = " " ) ; NEW_LINE DEDENT DEDENT print ( " " ) ; NEW_LINE DEDENT DEDENT pattern ( 7 ) ; NEW_LINE
|
T40204 | import java . util . Arrays ; class CoinChange { static long countWays ( int S [ ] , int m , int n ) { long [ ] table = new long [ n + 1 ] ; Arrays . fill ( table , 0 ) ; table [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = S [ i ] ; j <= n ; j ++ ) table [ j ] += table [ j - S [ i ] ] ; return table [ n ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 } ; int m = arr . length ; int n = 4 ; System . out . println ( countWays ( arr , m , n ) ) ; } }
| def count ( S , m , n ) : NEW_LINE INDENT table = [ [ 0 for x in range ( m ) ] for x in range ( n + 1 ) ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT table [ 0 ] [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT x = table [ i - S [ j ] ] [ j ] if i - S [ j ] >= 0 else 0 NEW_LINE y = table [ i ] [ j - 1 ] if j >= 1 else 0 NEW_LINE table [ i ] [ j ] = x + y NEW_LINE DEDENT DEDENT return table [ n ] [ m - 1 ] NEW_LINE DEDENT arr = [ 1 , 2 , 3 ] NEW_LINE m = len ( arr ) NEW_LINE n = 4 NEW_LINE print ( count ( arr , m , n ) ) NEW_LINE
|
T40205 | import java . io . * ; class GFG { static int has0 ( int x ) { while ( x != 0 ) { if ( x % 10 == 0 ) return 1 ; x /= 10 ; } return 0 ; } static int getCount ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) count += has0 ( i ) ; return count ; } public static void main ( String args [ ] ) { int n = 107 ; System . out . println ( " Count β of β numbers β from β 1" + " β to β " + n + " β is β " + getCount ( n ) ) ; } }
| def has0 ( x ) : NEW_LINE INDENT while ( x != 0 ) : NEW_LINE INDENT if ( x % 10 == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT x = x // 10 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def getCount ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT count = count + has0 ( i ) NEW_LINE DEDENT return count NEW_LINE DEDENT n = 107 NEW_LINE print ( " Count β of β numbers β from β 1" , " β to β " , n , " β is β " , getCount ( n ) ) NEW_LINE
|
T40206 | public class GFG { public static double floatError ( double no ) { double sum = 0.0 ; for ( int i = 0 ; i < 10 ; i ++ ) { sum = sum + no ; } return sum ; } public static void main ( String [ ] args ) { System . out . println ( floatError ( 0.1 ) ) ; } }
| def floatError ( no ) : NEW_LINE INDENT sum = 0.0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT sum = sum + no NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( floatError ( 0.1 ) ) NEW_LINE DEDENT
|
T40207 | import java . util . * ; class GFG { static int count_numbers ( int k , int n ) { int [ ] [ ] dp = new int [ n + 1 ] [ 2 ] ; dp [ 1 ] [ 0 ] = 0 ; dp [ 1 ] [ 1 ] = k - 1 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] ) * ( k - 1 ) ; } return dp [ n ] [ 0 ] + dp [ n ] [ 1 ] ; } public static void main ( String [ ] args ) { int k = 10 ; int n = 3 ; System . out . println ( count_numbers ( k , n ) ) ; } }
| def count_numbers ( k , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 2 ) ] for i in range ( n + 1 ) ] NEW_LINE dp [ 1 ] [ 0 ] = 0 NEW_LINE dp [ 1 ] [ 1 ] = k - 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = dp [ i - 1 ] [ 1 ] NEW_LINE dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] ) * ( k - 1 ) NEW_LINE DEDENT return dp [ n ] [ 0 ] + dp [ n ] [ 1 ] NEW_LINE DEDENT k = 10 NEW_LINE n = 3 NEW_LINE print ( count_numbers ( k , n ) ) NEW_LINE
|
T40208 | class Generate { static void printWellOrdered ( int number , int x , int k ) { if ( k == 0 ) { System . out . print ( number + " β " ) ; return ; } for ( int i = ( x + 1 ) ; i < 10 ; i ++ ) printWellOrdered ( number * 10 + i , i , k - 1 ) ; } static void generateWellOrdered ( int k ) { printWellOrdered ( 0 , 0 , k ) ; } public static void main ( String [ ] args ) { int k = 3 ; generateWellOrdered ( k ) ; } }
| def printWellOrdered ( number , x , k ) : NEW_LINE INDENT if ( k == 0 ) : NEW_LINE INDENT print ( number , end = " β " ) NEW_LINE return NEW_LINE DEDENT for i in range ( ( x + 1 ) , 10 ) : NEW_LINE INDENT printWellOrdered ( number * 10 + i , i , k - 1 ) NEW_LINE DEDENT DEDENT def generateWellOrdered ( k ) : NEW_LINE INDENT printWellOrdered ( 0 , 0 , k ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k = 3 NEW_LINE generateWellOrdered ( k ) NEW_LINE DEDENT
|
T40209 | import java . util . * ; class GFG { static int ans = 0 ; static Vector < Vector < Integer > > graph = new Vector < Vector < Integer > > ( ) ; static Vector < Integer > weight = new Vector < Integer > ( ) ; static boolean isEvenParity ( int x ) { int parity = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; parity ++ ; } if ( parity % 2 == 0 ) return true ; else return false ; } static void dfs ( int node , int parent ) { if ( isEvenParity ( weight . get ( node ) ) ) ans += 1 ; for ( int i = 0 ; i < graph . get ( node ) . size ( ) ; i ++ ) { if ( graph . get ( node ) . get ( i ) == parent ) continue ; dfs ( graph . get ( node ) . get ( i ) , node ) ; } } public static void main ( String args [ ] ) { weight . add ( 0 ) ; weight . add ( 5 ) ; weight . add ( 10 ) ; ; weight . add ( 11 ) ; ; weight . add ( 8 ) ; weight . add ( 6 ) ; for ( int i = 0 ; i < 100 ; i ++ ) graph . add ( new Vector < Integer > ( ) ) ; graph . get ( 1 ) . add ( 2 ) ; graph . get ( 2 ) . add ( 3 ) ; graph . get ( 2 ) . add ( 4 ) ; graph . get ( 1 ) . add ( 5 ) ; dfs ( 1 , 1 ) ; System . out . println ( ans ) ; } }
| ans = 0 NEW_LINE graph = [ [ ] for i in range ( 100 ) ] NEW_LINE weight = [ 0 ] * 100 NEW_LINE def isEvenParity ( x ) : NEW_LINE INDENT parity = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT x = x & ( x - 1 ) NEW_LINE parity += 1 NEW_LINE DEDENT if ( parity % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def dfs ( node , parent ) : NEW_LINE INDENT global ans NEW_LINE if ( isEvenParity ( weight [ node ] ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT for to in graph [ node ] : NEW_LINE INDENT if ( to == parent ) : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( to , node ) NEW_LINE DEDENT DEDENT weight [ 1 ] = 5 NEW_LINE weight [ 2 ] = 10 NEW_LINE weight [ 3 ] = 11 NEW_LINE weight [ 4 ] = 8 NEW_LINE weight [ 5 ] = 6 NEW_LINE graph [ 1 ] . append ( 2 ) NEW_LINE graph [ 2 ] . append ( 3 ) NEW_LINE graph [ 2 ] . append ( 4 ) NEW_LINE graph [ 1 ] . append ( 5 ) NEW_LINE dfs ( 1 , 1 ) NEW_LINE print ( ans ) NEW_LINE
|
T40210 | class GFG { static int MAX = 1000 ; static boolean lineExists ( int x [ ] , int y [ ] , int v [ ] , int n ) { int size = ( 2 * MAX ) + 1 ; long [ ] arr = new long [ size ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ x [ i ] + MAX ] += v [ i ] ; } for ( int i = 1 ; i < size ; i ++ ) arr [ i ] += arr [ i - 1 ] ; if ( arr [ size - 1 ] == 0 ) return true ; if ( arr [ size - 1 ] - arr [ 0 ] == 0 ) return true ; for ( int i = 1 ; i < size - 1 ; i ++ ) { if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i - 1 ] ) return true ; if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i ] ) return true ; if ( arr [ i ] == arr [ size - 1 ] - arr [ i ] ) return true ; } if ( arr [ size - 2 ] == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int x [ ] = { - 3 , 5 , 8 } ; int y [ ] = { 8 , 7 , 9 } ; int v [ ] = { 8 , 2 , 10 } ; int n = x . length ; if ( lineExists ( x , y , v , n ) ) System . out . printf ( " Yes " ) ; else System . out . printf ( " No " ) ; } }
| MAX = 1000 ; NEW_LINE def lineExists ( x , y , v , n ) : NEW_LINE INDENT size = ( 2 * MAX ) + 1 ; NEW_LINE arr = [ 0 ] * size ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ x [ i ] + MAX ] += v [ i ] ; NEW_LINE DEDENT for i in range ( 1 , size ) : NEW_LINE INDENT arr [ i ] += arr [ i - 1 ] ; NEW_LINE DEDENT if ( arr [ size - 1 ] == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT if ( arr [ size - 1 ] - arr [ 0 ] == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT for i in range ( 1 , size - 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i - 1 ] ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i ] ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT if ( arr [ i ] == arr [ size - 1 ] - arr [ i ] ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT if ( arr [ size - 2 ] == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = [ - 3 , 5 , 8 ] ; NEW_LINE y = [ 8 , 7 , 9 ] ; NEW_LINE v = [ 8 , 2 , 10 ] ; NEW_LINE n = len ( x ) ; NEW_LINE if ( lineExists ( x , y , v , n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
|
T40211 | class GFG { static int maxDivide ( int a , int b ) { while ( a % b == 0 ) a = a / b ; return a ; } static int isUgly ( int no ) { no = maxDivide ( no , 2 ) ; no = maxDivide ( no , 3 ) ; no = maxDivide ( no , 5 ) ; return ( no == 1 ) ? 1 : 0 ; } static int getNthUglyNo ( int n ) { int i = 1 ; int count = 1 ; while ( n > count ) { i ++ ; if ( isUgly ( i ) == 1 ) count ++ ; } return i ; } public static void main ( String args [ ] ) { int no = getNthUglyNo ( 150 ) ; System . out . println ( "150th β ugly β " + " no . β is β " + no ) ; } }
| def maxDivide ( a , b ) : NEW_LINE INDENT while a % b == 0 : NEW_LINE INDENT a = a / b NEW_LINE DEDENT return a NEW_LINE DEDENT def isUgly ( no ) : NEW_LINE INDENT no = maxDivide ( no , 2 ) NEW_LINE no = maxDivide ( no , 3 ) NEW_LINE no = maxDivide ( no , 5 ) NEW_LINE return 1 if no == 1 else 0 NEW_LINE DEDENT def getNthUglyNo ( n ) : NEW_LINE INDENT i = 1 NEW_LINE count = 1 NEW_LINE while n > count : NEW_LINE INDENT i += 1 NEW_LINE if isUgly ( i ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return i NEW_LINE DEDENT no = getNthUglyNo ( 150 ) NEW_LINE print ( "150th β ugly β no . β is β " , no ) NEW_LINE
|
T40212 | import java . util . Arrays ; import java . io . * ; class GFG { static void pendulumArrangement ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int odd , temp , in , pos ; pos = n - 1 ; if ( n % 2 == 0 ) odd = n - 1 ; else odd = n - 2 ; while ( odd > 0 ) { temp = arr [ odd ] ; in = odd ; while ( in != pos ) { arr [ in ] = arr [ in + 1 ] ; in ++ ; } arr [ in ] = temp ; odd = odd - 2 ; pos = pos - 1 ; } int start = 0 , end = ( n - 1 ) / 2 ; for ( ; start < end ; start ++ , end -- ) { temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " β " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 2 , 4 , 55 , 6 , 8 } ; int n = arr . length ; pendulumArrangement ( arr , n ) ; } }
| def pendulumArrangement ( arr , n ) : NEW_LINE INDENT arr . sort ( reverse = False ) NEW_LINE pos = n - 1 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT odd = n - 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd = n - 2 NEW_LINE DEDENT while ( odd > 0 ) : NEW_LINE INDENT temp = arr [ odd ] NEW_LINE in1 = odd NEW_LINE while ( in1 != pos ) : NEW_LINE INDENT arr [ in1 ] = arr [ in1 + 1 ] NEW_LINE in1 += 1 NEW_LINE DEDENT arr [ in1 ] = temp NEW_LINE odd = odd - 2 NEW_LINE pos = pos - 1 NEW_LINE DEDENT start = 0 NEW_LINE end = int ( ( n - 1 ) / 2 ) NEW_LINE while ( start < end ) : NEW_LINE INDENT temp = arr [ start ] NEW_LINE arr [ start ] = arr [ end ] NEW_LINE arr [ end ] = temp NEW_LINE start += 1 NEW_LINE end -= 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 11 , 2 , 4 , 55 , 6 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE pendulumArrangement ( arr , n ) NEW_LINE DEDENT
|
T40213 | class GFG { static int product ( int ar [ ] , int n ) { int result = 1 ; for ( int i = 0 ; i < n ; i ++ ) result = result * ar [ i ] ; return result ; } public static void main ( String [ ] args ) { int ar [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = ar . length ; System . out . printf ( " % d " , product ( ar , n ) ) ; } }
| def product ( ar , n ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT result = result * ar [ i ] NEW_LINE DEDENT return result NEW_LINE DEDENT ar = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( ar ) NEW_LINE print ( product ( ar , n ) ) NEW_LINE
|
T40214 | public class GFG { static float sumOfSeries ( int x , int k ) { float y = ( float ) ( ( ( float ) ( x ) / 81 ) * ( 9 * k - 1 + Math . pow ( 10 , ( - 1 ) * k ) ) ) ; return y ; } public static void main ( String args [ ] ) { int x = 9 ; int k = 20 ; System . out . println ( sumOfSeries ( x , k ) ) ; } }
| def sumOfSeries ( x , k ) : NEW_LINE INDENT return ( float ( x ) / 81 ) * ( 9 * k - 1 + 10 ** ( ( - 1 ) * k ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 9 NEW_LINE k = 20 NEW_LINE print ( sumOfSeries ( x , k ) ) NEW_LINE DEDENT
|
T40215 | class GFG { static String newString ( String s , int k ) { String X = " " ; while ( s . length ( ) > 0 ) { char temp = s . charAt ( 0 ) ; for ( int i = 1 ; i < k && i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) < temp ) { temp = s . charAt ( i ) ; } } X = X + temp ; for ( int i = 0 ; i < k ; i ++ ) { if ( s . charAt ( i ) == temp ) { s = s . substring ( 0 , i ) + s . substring ( i + 1 ) ; break ; } } } return X ; } public static void main ( String [ ] args ) { String s = " gaurang " ; int k = 3 ; System . out . println ( newString ( s , k ) ) ; } }
| def newString ( s , k ) : NEW_LINE INDENT X = " " NEW_LINE while ( len ( s ) > 0 ) : NEW_LINE INDENT temp = s [ 0 ] NEW_LINE i = 1 NEW_LINE while ( i < k and i < len ( s ) ) : NEW_LINE INDENT if ( s [ i ] < temp ) : NEW_LINE INDENT temp = s [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT X = X + temp NEW_LINE for i in range ( k ) : NEW_LINE INDENT if ( s [ i ] == temp ) : NEW_LINE INDENT s = s [ 0 : i ] + s [ i + 1 : ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT return X NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " gaurang " NEW_LINE k = 3 NEW_LINE print ( newString ( s , k ) ) NEW_LINE DEDENT
|
T40216 | class GFG { static int possibleStrings ( int n , int r , int b , int g ) { int fact [ ] = new int [ n + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; int left = n - ( r + g + b ) ; int sum = 0 ; for ( int i = 0 ; i <= left ; i ++ ) { for ( int j = 0 ; j <= left - i ; j ++ ) { int k = left - ( i + j ) ; sum = sum + fact [ n ] / ( fact [ i + r ] * fact [ j + b ] * fact [ k + g ] ) ; } } return sum ; } public static void main ( String [ ] args ) { int n = 4 , r = 2 ; int b = 0 , g = 1 ; System . out . println ( possibleStrings ( n , r , b , g ) ) ; } }
| def possibleStrings ( n , r , b , g ) : NEW_LINE INDENT fact = [ 0 for i in range ( n + 1 ) ] NEW_LINE fact [ 0 ] = 1 NEW_LINE for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT fact [ i ] = fact [ i - 1 ] * i NEW_LINE DEDENT left = n - ( r + g + b ) NEW_LINE sum = 0 NEW_LINE for i in range ( 0 , left + 1 , 1 ) : NEW_LINE INDENT for j in range ( 0 , left - i + 1 , 1 ) : NEW_LINE INDENT k = left - ( i + j ) NEW_LINE sum = ( sum + fact [ n ] / ( fact [ i + r ] * fact [ j + b ] * fact [ k + g ] ) ) NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE r = 2 NEW_LINE b = 0 NEW_LINE g = 1 NEW_LINE print ( int ( possibleStrings ( n , r , b , g ) ) ) NEW_LINE DEDENT
|
T40217 | class GFG { static int countKdivPairs ( int A [ ] , int n , int K ) { int [ ] freq = new int [ K ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int rem = A [ i ] % K ; if ( rem != 0 ) ans += freq [ K - rem ] ; else ans += freq [ 0 ] ; freq [ rem ] ++ ; } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 2 , 1 , 7 , 5 , 3 } ; int n = A . length ; int K = 4 ; System . out . println ( countKdivPairs ( A , n , K ) ) ; } }
| def countKdivPairs ( A , n , K ) : NEW_LINE INDENT freq = [ 0 for i in range ( K ) ] NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT rem = A [ i ] % K NEW_LINE if ( rem != 0 ) : NEW_LINE INDENT ans += freq [ K - rem ] NEW_LINE DEDENT else : NEW_LINE INDENT ans += freq [ 0 ] NEW_LINE DEDENT freq [ rem ] += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 2 , 1 , 7 , 5 , 3 ] NEW_LINE n = len ( A ) NEW_LINE K = 4 NEW_LINE print ( countKdivPairs ( A , n , K ) ) NEW_LINE DEDENT
|
T40218 | import java . io . * ; import java . util . * ; class GFG { static void sieve ( int n , boolean prime [ ] ) { for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } } static void printPrimeTriplets ( int n ) { boolean prime [ ] = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; sieve ( n , prime ) ; System . out . println ( " The β prime β triplets " + " β from β 1 β to β " + n + " are β : " ) ; for ( int i = 2 ; i <= n - 6 ; ++ i ) { if ( prime [ i ] && prime [ i + 2 ] && prime [ i + 6 ] ) System . out . println ( i + " β " + ( i + 2 ) + " β " + ( i + 6 ) ) ; else if ( prime [ i ] && prime [ i + 4 ] && prime [ i + 6 ] ) System . out . println ( i + " β " + ( i + 4 ) + " β " + ( i + 6 ) ) ; } } public static void main ( String args [ ] ) { int n = 25 ; printPrimeTriplets ( n ) ; } }
| def sieve ( n , prime ) : NEW_LINE INDENT p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT i = p * 2 NEW_LINE while ( i <= n ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE i = i + p NEW_LINE DEDENT DEDENT p = p + 1 NEW_LINE DEDENT DEDENT def printPrimeTriplets ( n ) : NEW_LINE INDENT prime = [ True ] * ( n + 1 ) NEW_LINE sieve ( n , prime ) NEW_LINE print ( " The β prime β triplets β from β 1 β to β " , n , " are β : " ) NEW_LINE for i in range ( 2 , n - 6 + 1 ) : NEW_LINE INDENT if ( prime [ i ] and prime [ i + 2 ] and prime [ i + 6 ] ) : NEW_LINE INDENT print ( i , ( i + 2 ) , ( i + 6 ) ) NEW_LINE DEDENT elif ( prime [ i ] and prime [ i + 4 ] and prime [ i + 6 ] ) : NEW_LINE INDENT print ( i , ( i + 4 ) , ( i + 6 ) ) NEW_LINE DEDENT DEDENT DEDENT n = 25 NEW_LINE printPrimeTriplets ( n ) NEW_LINE
|
T40219 | import java . util . * ; import java . lang . * ; class GFG { public static int sumOfSeries ( int n ) { int sum = 0 ; for ( int x = 1 ; x <= n ; x ++ ) sum += x * x * x ; return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( sumOfSeries ( n ) ) ; } }
| def sumOfSeries ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum += i * i * i NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 5 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE
|
T40220 | import java . io . * ; class Series { static int term ( int n ) { int x = ( ( ( 1 ) + ( int ) Math . sqrt ( 1 + ( 8 * n ) ) ) / 2 ) ; return x ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( term ( n ) ) ; } }
| import math NEW_LINE def term ( n ) : NEW_LINE INDENT x = ( ( ( 1 ) + math . sqrt ( 1 + ( 8 * n ) ) ) / 2 ) NEW_LINE return x NEW_LINE DEDENT n = 5 NEW_LINE print ( int ( term ( n ) ) ) NEW_LINE
|
T40221 | import java . util . * ; class Odd { public static int oddSum ( int n ) { int sum = 0 , curr = 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += curr ; curr += 2 ; } return sum ; } public static void main ( String [ ] args ) { int n = 20 ; System . out . println ( " β Sum β of β first β " + n + " β Odd β Numbers β is : β " + oddSum ( n ) ) ; } }
| def oddSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE curr = 1 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT sum = sum + curr NEW_LINE curr = curr + 2 NEW_LINE i = i + 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 20 NEW_LINE print ( " β Sum β of β first " , n , " Odd β Numbers β is : β " , oddSum ( n ) ) NEW_LINE
|
T40222 | class GFG { static long c [ ] = new long [ 100 ] ; static void coef ( int n ) { c [ 0 ] = 1 ; for ( int i = 0 ; i < n ; c [ 0 ] = - c [ 0 ] , i ++ ) { c [ 1 + i ] = 1 ; for ( int j = i ; j > 0 ; j -- ) c [ j ] = c [ j - 1 ] - c [ j ] ; } } static boolean isPrime ( int n ) { coef ( n ) ; c [ 0 ] ++ ; c [ n ] -- ; int i = n ; while ( ( i -- ) > 0 && c [ i ] % n == 0 ) ; return i < 0 ; } public static void main ( String [ ] args ) { int n = 37 ; if ( isPrime ( n ) ) System . out . println ( " Prime " ) ; else System . out . println ( " Not β Prime " ) ; } }
| c = [ 0 ] * 100 ; NEW_LINE def coef ( n ) : NEW_LINE INDENT c [ 0 ] = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ 1 + i ] = 1 ; NEW_LINE for j in range ( i , 0 , - 1 ) : NEW_LINE INDENT c [ j ] = c [ j - 1 ] - c [ j ] ; NEW_LINE DEDENT c [ 0 ] = - c [ 0 ] ; NEW_LINE DEDENT DEDENT def isPrime ( n ) : NEW_LINE INDENT coef ( n ) ; NEW_LINE c [ 0 ] = c [ 0 ] + 1 ; NEW_LINE c [ n ] = c [ n ] - 1 ; NEW_LINE i = n ; NEW_LINE while ( i > - 1 and c [ i ] % n == 0 ) : NEW_LINE INDENT i = i - 1 ; NEW_LINE DEDENT return True if i < 0 else False ; NEW_LINE DEDENT n = 37 ; NEW_LINE if ( isPrime ( n ) ) : NEW_LINE INDENT print ( " Prime " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not β Prime " ) ; NEW_LINE DEDENT
|
T40223 | import java . util . * ; class GFG { static int sumOfTheSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int k = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " Sum β = β " + sumOfTheSeries ( n ) ) ; } }
| def sumOfTheSeries ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT k = 1 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT sum += k NEW_LINE k += 2 NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 5 NEW_LINE print ( " Sum β = " , sumOfTheSeries ( n ) ) NEW_LINE
|
T40224 | import java . util . * ; class GFG { static int R = 4 ; static int C = 3 ; static boolean isSafe ( int x , int y ) { return ( x < R && y < C ) ; } static int [ ] [ ] dp = new int [ R ] [ C ] ; static int minJump ( int height [ ] [ ] , int x , int y ) { if ( dp [ x ] [ y ] != - 1 ) { return dp [ x ] [ y ] ; } if ( x == R - 1 && y == C - 1 ) { return ( dp [ x ] [ y ] = 0 ) ; } int diag = Integer . MAX_VALUE ; if ( isSafe ( x + 1 , y + 1 ) ) { diag = minJump ( height , x + 1 , y + 1 ) + Math . abs ( height [ x ] [ y ] - height [ x + 1 ] [ y + 1 ] ) ; } int down = Integer . MAX_VALUE ; if ( isSafe ( x + 1 , y ) ) { down = minJump ( height , x + 1 , y ) + Math . abs ( height [ x ] [ y ] - height [ x + 1 ] [ y ] ) ; } int right = Integer . MAX_VALUE ; if ( isSafe ( x , y + 1 ) ) { right = minJump ( height , x , y + 1 ) + Math . abs ( height [ x ] [ y ] - height [ x ] [ y + 1 ] ) ; } dp [ x ] [ y ] = Math . min ( Math . min ( down , right ) , diag ) ; return dp [ x ] [ y ] ; } public static void main ( String [ ] args ) { int height [ ] [ ] = { { 5 , 4 , 2 } , { 9 , 2 , 1 } , { 2 , 5 , 9 } , { 1 , 3 , 11 } } ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } System . out . println ( minJump ( height , 0 , 0 ) ) ; } }
| R = 4 NEW_LINE C = 3 NEW_LINE def isSafe ( x , y ) : NEW_LINE INDENT return ( x < R and y < C ) NEW_LINE DEDENT dp = [ [ - 1 for i in range ( C ) ] for i in range ( R ) ] NEW_LINE def minJump ( height , x , y ) : NEW_LINE INDENT if ( dp [ x ] [ y ] != - 1 ) : NEW_LINE INDENT return dp [ x ] [ y ] NEW_LINE DEDENT if ( x == R - 1 and y == C - 1 ) : NEW_LINE INDENT return ( dp [ x ] [ y ] == 0 ) NEW_LINE DEDENT diag = 10 ** 9 NEW_LINE if ( isSafe ( x + 1 , y + 1 ) ) : NEW_LINE INDENT diag = minJump ( height , x + 1 , y + 1 ) + abs ( height [ x ] [ y ] - height [ x + 1 ] [ y + 1 ] ) NEW_LINE DEDENT down = 10 ** 9 NEW_LINE if ( isSafe ( x + 1 , y ) ) : NEW_LINE INDENT down = minJump ( height , x + 1 , y ) + abs ( height [ x ] [ y ] - height [ x + 1 ] [ y ] ) NEW_LINE DEDENT right = 10 ** 9 NEW_LINE if ( isSafe ( x , y + 1 ) ) : NEW_LINE INDENT right = minJump ( height , x , y + 1 ) + abs ( height [ x ] [ y ] - height [ x ] [ y + 1 ] ) NEW_LINE DEDENT dp [ x ] [ y ] = min ( down , right , diag ) NEW_LINE return dp [ x ] [ y ] NEW_LINE DEDENT height = [ [ 5 , 4 , 2 ] , [ 9 , 2 , 1 ] , [ 2 , 5 , 9 ] , [ 1 , 3 , 11 ] ] NEW_LINE print ( minJump ( height , 0 , 0 ) ) NEW_LINE
|
T40225 | import java . io . * ; class GFG { static void findsolution ( long n , long x , long y ) { if ( ( y - n + 1 ) * ( y - n + 1 ) + n - 1 < x || y < n ) { System . out . println ( " No β solution " ) ; return ; } System . out . println ( y - n + 1 ) ; while ( n -- > 1 ) System . out . println ( "1" ) ; } public static void main ( String [ ] args ) { long n , x , y ; n = 5 ; x = 15 ; y = 15 ; findsolution ( n , x , y ) ; } }
| def findsolution ( n , x , y ) : NEW_LINE INDENT if ( ( y - n + 1 ) * ( y - n + 1 ) + n - 1 < x or y < n ) : NEW_LINE INDENT print ( " No β solution " ) ; NEW_LINE return ; NEW_LINE DEDENT print ( y - n + 1 ) ; NEW_LINE while ( n > 1 ) : NEW_LINE INDENT print ( 1 ) ; NEW_LINE n -= 1 ; NEW_LINE DEDENT DEDENT n = 5 ; NEW_LINE x = 15 ; NEW_LINE y = 15 ; NEW_LINE findsolution ( n , x , y ) ; NEW_LINE
|
T40226 | public final class p068 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p068 ( ) . run ( ) ) ; } public String run ( ) { int [ ] state = new int [ 10 ] ; for ( int i = 0 ; i < state . length ; i ++ ) state [ i ] = i + 1 ; String max = null ; do { int sum = state [ 0 ] + state [ 5 ] + state [ 6 ] ; if ( state [ 1 ] + state [ 6 ] + state [ 7 ] != sum || state [ 2 ] + state [ 7 ] + state [ 8 ] != sum || state [ 3 ] + state [ 8 ] + state [ 9 ] != sum || state [ 4 ] + state [ 9 ] + state [ 5 ] != sum ) continue ; int minOuterIndex = - 1 ; int minOuter = Integer . MAX_VALUE ; for ( int i = 0 ; i < 5 ; i ++ ) { if ( state [ i ] < minOuter ) { minOuterIndex = i ; minOuter = state [ i ] ; } } String s = " " ; for ( int i = 0 ; i < 5 ; i ++ ) s += " " + state [ ( minOuterIndex + i ) % 5 ] + state [ ( minOuterIndex + i ) % 5 + 5 ] + state [ ( minOuterIndex + i + 1 ) % 5 + 5 ] ; if ( s . length ( ) == 16 && ( max == null || s . compareTo ( max ) > 0 ) ) max = s ; } while ( Library . nextPermutation ( state ) ) ; if ( max == null ) throw new AssertionError ( ) ; return max ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT state = list ( range ( 1 , 11 ) ) NEW_LINE max = None NEW_LINE while True : NEW_LINE INDENT sum = state [ 0 ] + state [ 5 ] + state [ 6 ] NEW_LINE if state [ 1 ] + state [ 6 ] + state [ 7 ] == sum and state [ 2 ] + state [ 7 ] + state [ 8 ] == sum and state [ 3 ] + state [ 8 ] + state [ 9 ] == sum and state [ 4 ] + state [ 9 ] + state [ 5 ] == sum : NEW_LINE INDENT minouterindex = 0 NEW_LINE minouter = state [ 0 ] NEW_LINE for i in range ( 1 , 5 ) : NEW_LINE INDENT if state [ i ] < minouter : NEW_LINE INDENT minouterindex = i NEW_LINE minouter = state [ i ] NEW_LINE DEDENT DEDENT s = " " NEW_LINE for i in range ( 5 ) : NEW_LINE INDENT s += str ( state [ ( minouterindex + i ) % 5 ] ) NEW_LINE s += str ( state [ ( minouterindex + i ) % 5 + 5 ] ) NEW_LINE s += str ( state [ ( minouterindex + i + 1 ) % 5 + 5 ] ) NEW_LINE DEDENT if len ( s ) == 16 and ( max is None or s > max ) : NEW_LINE INDENT max = s NEW_LINE DEDENT DEDENT if not eulerlib . next_permutation ( state ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT assert max is not None NEW_LINE return max NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40227 | import java . math . BigInteger ; public final class p243 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p243 ( ) . run ( ) ) ; } private static final Fraction TARGET = new Fraction ( BigInteger . valueOf ( 15499 ) , BigInteger . valueOf ( 94744 ) ) ; public String run ( ) { BigInteger totient = BigInteger . ONE ; BigInteger denominator = BigInteger . ONE ; for ( int p = 2 ; ; ) { totient = totient . multiply ( BigInteger . valueOf ( p - 1 ) ) ; denominator = denominator . multiply ( BigInteger . valueOf ( p ) ) ; do p ++ ; while ( ! Library . isPrime ( p ) ) ; if ( new Fraction ( totient , denominator ) . compareTo ( TARGET ) < 0 ) { for ( int i = 1 ; i < p ; i ++ ) { BigInteger numer = BigInteger . valueOf ( i ) . multiply ( totient ) ; BigInteger denom = BigInteger . valueOf ( i ) . multiply ( denominator ) ; if ( new Fraction ( numer , denom . subtract ( BigInteger . ONE ) ) . compareTo ( TARGET ) < 0 ) return denom . toString ( ) ; } } } } }
| import eulerlib , fractions NEW_LINE def compute ( ) : NEW_LINE INDENT TARGET = fractions . Fraction ( 15499 , 94744 ) NEW_LINE totient = 1 NEW_LINE denominator = 1 NEW_LINE p = 2 NEW_LINE while True : NEW_LINE INDENT totient *= p - 1 NEW_LINE denominator *= p NEW_LINE while True : NEW_LINE INDENT p += 1 NEW_LINE if eulerlib . is_prime ( p ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if fractions . Fraction ( totient , denominator ) < TARGET : NEW_LINE INDENT for i in range ( 1 , p ) : NEW_LINE INDENT numer = i * totient NEW_LINE denom = i * denominator NEW_LINE if fractions . Fraction ( numer , denom - 1 ) < TARGET : NEW_LINE INDENT return str ( denom ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40228 | public final class p037 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p037 ( ) . run ( ) ) ; } public String run ( ) { long sum = 0 ; for ( int count = 0 , n = 10 ; count < 11 ; n ++ ) { if ( isTruncatablePrime ( n ) ) { sum += n ; count ++ ; } } return Long . toString ( sum ) ; } private static boolean isTruncatablePrime ( int n ) { for ( long i = 10 ; i <= n ; i *= 10 ) { if ( ! Library . isPrime ( n % ( int ) i ) ) return false ; } for ( ; n != 0 ; n /= 10 ) { if ( ! Library . isPrime ( n ) ) return false ; } return true ; } }
| import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT ans = sum ( itertools . islice ( filter ( is_truncatable_prime , itertools . count ( 10 ) ) , 11 ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def is_truncatable_prime ( n ) : NEW_LINE INDENT i = 10 NEW_LINE while i <= n : NEW_LINE INDENT if not eulerlib . is_prime ( n % i ) : NEW_LINE INDENT return False NEW_LINE DEDENT i *= 10 NEW_LINE DEDENT while n > 0 : NEW_LINE INDENT if not eulerlib . is_prime ( n ) : NEW_LINE INDENT return False NEW_LINE DEDENT n //= 10 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40229 | public final class p182 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p182 ( ) . run ( ) ) ; } private static final int P = 1009 ; private static final int Q = 3643 ; private static final int TOTIENT = ( P - 1 ) * ( Q - 1 ) ; public String run ( ) { int [ ] numUnconcealedP = countAllUnconcealed ( P ) ; int [ ] numUnconcealedQ = countAllUnconcealed ( Q ) ; int minUnconcealedP = Integer . MAX_VALUE ; for ( int x : numUnconcealedP ) minUnconcealedP = Math . min ( x , minUnconcealedP ) ; int minUnconcealedQ = Integer . MAX_VALUE ; for ( int x : numUnconcealedQ ) minUnconcealedQ = Math . min ( x , minUnconcealedQ ) ; long sum = 0 ; for ( int e = 0 ; e < TOTIENT ; e ++ ) { if ( numUnconcealedP [ e % ( P - 1 ) ] == minUnconcealedP && numUnconcealedQ [ e % ( Q - 1 ) ] == minUnconcealedQ ) sum += e ; } return Long . toString ( sum ) ; } private static int [ ] countAllUnconcealed ( int prime ) { int [ ] numUnconcealed = new int [ prime - 1 ] ; for ( int e = 0 ; e < numUnconcealed . length ; e ++ ) { if ( Library . gcd ( e , prime - 1 ) == 1 ) numUnconcealed [ e ] = countUnconcealed ( prime , e ) ; else numUnconcealed [ e ] = Integer . MAX_VALUE ; } return numUnconcealed ; } private static int countUnconcealed ( int modulus , int e ) { int count = 0 ; for ( int m = 0 ; m < modulus ; m ++ ) { if ( Library . powMod ( m , e , modulus ) == m ) count ++ ; } return count ; } }
| import fractions NEW_LINE def compute ( ) : NEW_LINE INDENT P = 1009 NEW_LINE Q = 3643 NEW_LINE TOTIENT = ( P - 1 ) * ( Q - 1 ) NEW_LINE numunconcealedp = count_all_unconcealed ( P ) NEW_LINE numunconcealedq = count_all_unconcealed ( Q ) NEW_LINE minunconcealedp = min ( numunconcealedp ) NEW_LINE minunconcealedq = min ( numunconcealedq ) NEW_LINE ans = sum ( e for e in range ( TOTIENT ) if numunconcealedp [ e % ( P - 1 ) ] == minunconcealedp and numunconcealedq [ e % ( Q - 1 ) ] == minunconcealedq ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def count_all_unconcealed ( prime ) : NEW_LINE INDENT result = [ ] NEW_LINE for e in range ( prime - 1 ) : NEW_LINE INDENT if fractions . gcd ( e , prime - 1 ) == 1 : NEW_LINE INDENT result . append ( count_unconcealed ( prime , e ) ) NEW_LINE DEDENT else : NEW_LINE INDENT result . append ( 10 ** 20 ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT def count_unconcealed ( modulus , e ) : NEW_LINE INDENT result = 0 NEW_LINE for m in range ( modulus ) : NEW_LINE INDENT if pow ( m , e , modulus ) == m : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40230 | public final class p006 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p006 ( ) . run ( ) ) ; } private static final int N = 100 ; public String run ( ) { int sum = 0 ; int sum2 = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += i ; sum2 += i * i ; } return Integer . toString ( sum * sum - sum2 ) ; } }
| def compute ( ) : NEW_LINE INDENT N = 100 NEW_LINE s = sum ( i for i in range ( 1 , N + 1 ) ) NEW_LINE s2 = sum ( i ** 2 for i in range ( 1 , N + 1 ) ) NEW_LINE return str ( s ** 2 - s2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40231 | import java . math . BigInteger ; public final class p053 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p053 ( ) . run ( ) ) ; } public String run ( ) { BigInteger MILLION = BigInteger . TEN . pow ( 6 ) ; int count = 0 ; for ( int n = 1 ; n <= 100 ; n ++ ) { for ( int r = 0 ; r <= n ; r ++ ) { if ( Library . binomial ( n , r ) . compareTo ( MILLION ) > 0 ) count ++ ; } } return Integer . toString ( count ) ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT ans = sum ( 1 for n in range ( 1 , 101 ) for k in range ( 0 , n + 1 ) if eulerlib . binomial ( n , k ) > 1000000 ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40232 | import java . math . BigInteger ; import java . util . HashSet ; import java . util . Set ; public final class p029 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p029 ( ) . run ( ) ) ; } public String run ( ) { Set < BigInteger > generated = new HashSet < > ( ) ; for ( int a = 2 ; a <= 100 ; a ++ ) { for ( int b = 2 ; b <= 100 ; b ++ ) generated . add ( BigInteger . valueOf ( a ) . pow ( b ) ) ; } return Integer . toString ( generated . size ( ) ) ; } }
| def compute ( ) : NEW_LINE INDENT seen = set ( a ** b for a in range ( 2 , 101 ) for b in range ( 2 , 101 ) ) NEW_LINE return str ( len ( seen ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40233 | public final class p058 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p058 ( ) . run ( ) ) ; } public String run ( ) { int numPrimes = 0 ; for ( int n = 1 ; ; n += 2 ) { for ( int i = 0 ; i < 4 ; i ++ ) { if ( Library . isPrime ( n * n - i * ( n - 1 ) ) ) numPrimes ++ ; } if ( n > 1 && numPrimes * 10 < n * 2 - 1 ) return Integer . toString ( n ) ; } } }
| import eulerlib , fractions , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT TARGET = fractions . Fraction ( 1 , 10 ) NEW_LINE numprimes = 0 NEW_LINE for n in itertools . count ( 1 , 2 ) : NEW_LINE INDENT for i in range ( 4 ) : NEW_LINE INDENT if eulerlib . is_prime ( n * n - i * ( n - 1 ) ) : NEW_LINE INDENT numprimes += 1 NEW_LINE DEDENT DEDENT if n > 1 and fractions . Fraction ( numprimes , n * 2 - 1 ) < TARGET : NEW_LINE INDENT return str ( n ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40234 | import java . math . BigDecimal ; import java . math . BigInteger ; import java . math . RoundingMode ; import java . util . Stack ; public final class p493 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p493 ( ) . run ( ) ) ; } private static final int NUM_COLORS = 7 ; private static final int BALLS_PER_COLOR = 10 ; private static final int NUM_PICKED = 20 ; private BigInteger numerator = BigInteger . ZERO ; public String run ( ) { explore ( NUM_PICKED , BALLS_PER_COLOR , new Stack < Integer > ( ) ) ; BigInteger denominator = Library . binomial ( NUM_COLORS * BALLS_PER_COLOR , NUM_PICKED ) ; BigDecimal num = new BigDecimal ( numerator ) ; BigDecimal den = new BigDecimal ( denominator ) ; return num . divide ( den , 9 , RoundingMode . HALF_EVEN ) . toString ( ) ; } private void explore ( int remain , int limit , Stack < Integer > history ) { if ( remain == 0 ) { int [ ] hist = new int [ NUM_COLORS ] ; for ( int i = 0 ; i < history . size ( ) ; i ++ ) hist [ i ] = history . get ( i ) ; int [ ] histogram = new int [ BALLS_PER_COLOR + 1 ] ; for ( int x : hist ) histogram [ x ] ++ ; BigInteger count = Library . factorial ( NUM_COLORS ) ; for ( int x : histogram ) count = divideExactly ( count , Library . factorial ( x ) ) ; for ( int x : hist ) count = count . multiply ( Library . binomial ( BALLS_PER_COLOR , x ) ) ; int distinctColors = history . size ( ) ; numerator = numerator . add ( count . multiply ( BigInteger . valueOf ( distinctColors ) ) ) ; } else if ( history . size ( ) < NUM_COLORS ) { for ( int i = Math . min ( limit , remain ) ; i > 0 ; i -- ) { history . push ( i ) ; explore ( remain - i , i , history ) ; history . pop ( ) ; } } } private static BigInteger divideExactly ( BigInteger x , BigInteger y ) { BigInteger [ ] temp = x . divideAndRemainder ( y ) ; if ( temp [ 1 ] . signum ( ) != 0 ) throw new IllegalArgumentException ( " Not β divisible " ) ; return temp [ 0 ] ; } }
| import eulerlib , fractions , math NEW_LINE def compute ( ) : NEW_LINE INDENT NUM_COLORS = 7 NEW_LINE BALLS_PER_COLOR = 10 NEW_LINE NUM_PICKED = 20 NEW_LINE DECIMALS = 9 NEW_LINE numerator = [ 0 ] NEW_LINE def explore ( remain , limit , history ) : NEW_LINE INDENT if remain == 0 : NEW_LINE INDENT hist = list ( history ) NEW_LINE while len ( hist ) < NUM_COLORS : NEW_LINE INDENT hist . append ( 0 ) NEW_LINE DEDENT histogram = [ 0 ] * ( BALLS_PER_COLOR + 1 ) NEW_LINE for x in hist : NEW_LINE INDENT histogram [ x ] += 1 NEW_LINE DEDENT count = math . factorial ( NUM_COLORS ) NEW_LINE for x in histogram : NEW_LINE INDENT count = divide_exactly ( count , math . factorial ( x ) ) NEW_LINE DEDENT for x in hist : NEW_LINE INDENT count *= eulerlib . binomial ( BALLS_PER_COLOR , x ) NEW_LINE DEDENT distinctcolors = len ( history ) NEW_LINE numerator [ 0 ] += count * distinctcolors NEW_LINE DEDENT elif len ( history ) < NUM_COLORS : NEW_LINE INDENT for i in range ( min ( limit , remain ) , 0 , - 1 ) : NEW_LINE INDENT history . append ( i ) NEW_LINE explore ( remain - i , i , history ) NEW_LINE history . pop ( ) NEW_LINE DEDENT DEDENT DEDENT explore ( NUM_PICKED , BALLS_PER_COLOR , [ ] ) NEW_LINE denominator = eulerlib . binomial ( NUM_COLORS * BALLS_PER_COLOR , NUM_PICKED ) NEW_LINE ans = fractions . Fraction ( numerator [ 0 ] , denominator ) NEW_LINE return format_fraction ( ans , DECIMALS ) NEW_LINE DEDENT def format_fraction ( val , digits ) : NEW_LINE INDENT if digits <= 0 : NEW_LINE INDENT raise ValueError ( ) NEW_LINE DEDENT if val < 0 : NEW_LINE INDENT return " - " + format_fraction ( - val , digits ) NEW_LINE DEDENT s = str ( round ( val * 10 ** digits ) ) . zfill ( digits + 1 ) NEW_LINE return f " { s [ : - digits ] } . { s [ - digits : ] } " NEW_LINE DEDENT def divide_exactly ( x , y ) : NEW_LINE INDENT if x % y != 0 : NEW_LINE INDENT raise ValueError ( " Not β divisible " ) NEW_LINE DEDENT return x // y NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40235 | public final class p218 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p218 ( ) . run ( ) ) ; } public String run ( ) { return "0" ; } }
| def compute ( ) : NEW_LINE INDENT return "0" NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40236 | import java . math . BigInteger ; public final class p076 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p076 ( ) . run ( ) ) ; } public String run ( ) { return partitions ( 100 , 1 ) . subtract ( BigInteger . ONE ) . toString ( ) ; } private static BigInteger partitions ( int n , int k ) { BigInteger [ ] [ ] table = new BigInteger [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = n ; j >= 0 ; j -- ) { if ( j == i ) table [ i ] [ j ] = BigInteger . ONE ; else if ( j > i ) table [ i ] [ j ] = BigInteger . ZERO ; else if ( j == 0 ) table [ i ] [ j ] = table [ i ] [ j + 1 ] ; else table [ i ] [ j ] = table [ i ] [ j + 1 ] . add ( table [ i - j ] [ j ] ) ; } } return table [ n ] [ k ] ; } }
| def compute ( ) : NEW_LINE INDENT LIMIT = 100 NEW_LINE partitions = [ ] NEW_LINE for i in range ( LIMIT + 1 ) : NEW_LINE INDENT partitions . append ( [ None ] * ( LIMIT + 1 ) ) NEW_LINE for j in reversed ( range ( LIMIT + 1 ) ) : NEW_LINE INDENT if j == i : NEW_LINE INDENT val = 1 NEW_LINE DEDENT elif j > i : NEW_LINE INDENT val = 0 NEW_LINE DEDENT elif j == 0 : NEW_LINE INDENT val = partitions [ i ] [ j + 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT val = partitions [ i ] [ j + 1 ] + partitions [ i - j ] [ j ] NEW_LINE DEDENT partitions [ i ] [ j ] = val NEW_LINE DEDENT DEDENT ans = partitions [ LIMIT ] [ 1 ] - 1 NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40237 | public final class p357 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p357 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 8 ) ; private boolean [ ] isPrime ; public String run ( ) { isPrime = Library . listPrimality ( LIMIT + 1 ) ; long sum = 0 ; for ( int n = 0 ; n <= LIMIT ; n ++ ) { if ( isPrime [ n + 1 ] && isPrimeGenerating ( n ) ) sum += n ; } return Long . toString ( sum ) ; } private boolean isPrimeGenerating ( int n ) { for ( int i = 1 , end = Library . sqrt ( n ) ; i <= end ; i ++ ) { if ( n % i == 0 && ! isPrime [ i + n / i ] ) return false ; } return true ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 8 NEW_LINE isprime = eulerlib . list_primality ( LIMIT + 1 ) NEW_LINE def is_prime_generating ( n ) : NEW_LINE INDENT return all ( ( n % d != 0 or isprime [ d + n // d ] ) for d in range ( 2 , eulerlib . sqrt ( n ) + 1 ) ) NEW_LINE DEDENT ans = sum ( n for n in range ( LIMIT + 1 ) if isprime [ n + 1 ] and is_prime_generating ( n ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40238 | public final class p094 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p094 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 9 ) ; public String run ( ) { long sum = 0 ; for ( int s = 1 ; s * s <= ( LIMIT + 1 ) / 3 ; s += 2 ) { for ( int t = s - 2 ; t > 0 ; t -= 2 ) { if ( Library . gcd ( s , t ) == 1 ) { int a = s * t ; int b = ( s * s - t * t ) / 2 ; int c = ( s * s + t * t ) / 2 ; if ( a * 2 == c - 1 ) { int p = c * 3 - 1 ; if ( p <= LIMIT ) sum += p ; } if ( a * 2 == c + 1 ) { int p = c * 3 + 1 ; if ( p <= LIMIT ) sum += p ; } if ( b * 2 == c - 1 ) { int p = c * 3 - 1 ; if ( p <= LIMIT ) sum += p ; } if ( b * 2 == c + 1 ) { int p = c * 3 + 1 ; if ( p <= LIMIT ) sum += p ; } } } } return Long . toString ( sum ) ; } }
| import eulerlib , fractions , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 9 NEW_LINE ans = 0 NEW_LINE for s in itertools . count ( 1 , 2 ) : NEW_LINE INDENT if s * s > ( LIMIT + 1 ) // 3 : NEW_LINE INDENT break NEW_LINE DEDENT for t in range ( s - 2 , 0 , - 2 ) : NEW_LINE INDENT if fractions . gcd ( s , t ) == 1 : NEW_LINE INDENT a = s * t NEW_LINE b = ( s * s - t * t ) // 2 NEW_LINE c = ( s * s + t * t ) // 2 NEW_LINE if a * 2 == c - 1 : NEW_LINE INDENT p = c * 3 - 1 NEW_LINE if p <= LIMIT : NEW_LINE INDENT ans += p NEW_LINE DEDENT DEDENT if a * 2 == c + 1 : NEW_LINE INDENT p = c * 3 + 1 NEW_LINE if p <= LIMIT : NEW_LINE INDENT ans += p NEW_LINE DEDENT DEDENT if b * 2 == c - 1 : NEW_LINE INDENT p = c * 3 - 1 NEW_LINE if p <= LIMIT : NEW_LINE INDENT ans += p NEW_LINE DEDENT DEDENT if b * 2 == c + 1 : NEW_LINE INDENT p = c * 3 + 1 NEW_LINE if p <= LIMIT : NEW_LINE INDENT ans += p NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40239 | import java . util . Arrays ; public final class p211 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p211 ( ) . run ( ) ) ; } private static final int LIMIT = 64000000 ; public String run ( ) { long [ ] sigma2 = new long [ LIMIT ] ; Arrays . fill ( sigma2 , 1 , sigma2 . length , 1 ) ; for ( int i = 2 ; i < sigma2 . length ; i ++ ) { if ( sigma2 [ i ] == 1 ) { for ( int j = i ; j < sigma2 . length ; j += i ) sigma2 [ j ] *= powerSquareSum ( j , i ) ; } } long sum = 0 ; SquareTester sqt = new SquareTester ( 3 * 5 * 7 * 11 * 13 * 17 ) ; for ( int i = 1 ; i < sigma2 . length ; i ++ ) { if ( sqt . isPerfectSquare ( sigma2 [ i ] ) ) sum += i ; } return Long . toString ( sum ) ; } private static long powerSquareSum ( int n , int k ) { long result = 1 ; long k2 = ( long ) k * k ; while ( n % k == 0 ) { n /= k ; result = result * k2 + 1 ; } return result ; } private static final class SquareTester { private boolean [ ] isResidue ; public SquareTester ( int modulus ) { if ( modulus < 1 ) throw new IllegalArgumentException ( ) ; isResidue = new boolean [ modulus ] ; for ( int i = 0 ; i < modulus ; i ++ ) isResidue [ ( int ) ( ( long ) i * i % modulus ) ] = true ; } public boolean isPerfectSquare ( long x ) { if ( ! isResidue [ ( int ) ( x % isResidue . length ) ] ) return false ; long y = 0 ; for ( long i = 1L << 31 ; i != 0 ; i >>>= 1 ) { y |= i ; if ( y > 3037000499L || y * y > x ) y ^= i ; } return y * y == x ; } } }
| import array , eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 64000000 NEW_LINE RESIDUE_TEST = 3 * 5 * 7 * 11 * 13 NEW_LINE isresidue = [ False ] * RESIDUE_TEST NEW_LINE for i in range ( RESIDUE_TEST ) : NEW_LINE INDENT isresidue [ i * i % RESIDUE_TEST ] = True NEW_LINE DEDENT def is_perfect_square ( x ) : NEW_LINE INDENT return isresidue [ x % RESIDUE_TEST ] and eulerlib . is_square ( x ) NEW_LINE DEDENT sigma2 = list_sigma2 ( LIMIT - 1 ) NEW_LINE ans = sum ( i for i in range ( 1 , LIMIT ) if is_perfect_square ( sigma2 [ i ] ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def list_sigma2 ( n ) : NEW_LINE INDENT sqrt = eulerlib . sqrt ( n ) NEW_LINE quasiprimefactor = array . array ( " H " , ( 0 for _ in range ( n + 1 ) ) ) NEW_LINE for i in range ( 2 , sqrt + 1 ) : NEW_LINE INDENT if quasiprimefactor [ i ] == 0 : NEW_LINE INDENT quasiprimefactor [ i ] = i NEW_LINE for j in range ( i * i , n + 1 , i ) : NEW_LINE INDENT if quasiprimefactor [ j ] == 0 : NEW_LINE INDENT quasiprimefactor [ j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT sigma2 = array . array ( " Q " , ( 0 for _ in range ( n + 1 ) ) ) NEW_LINE sigma2 [ 1 ] = 1 NEW_LINE for i in range ( 2 , len ( sigma2 ) ) : NEW_LINE INDENT p = quasiprimefactor [ i ] NEW_LINE if p == 0 : NEW_LINE INDENT p = i NEW_LINE DEDENT sum = 1 NEW_LINE j = i NEW_LINE p2 = p * p NEW_LINE k = p2 NEW_LINE while j % p == 0 : NEW_LINE INDENT sum += k NEW_LINE j //= p NEW_LINE k *= p2 NEW_LINE DEDENT sigma2 [ i ] = sum * sigma2 [ j ] NEW_LINE DEDENT return sigma2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40240 | public final class p012 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p012 ( ) . run ( ) ) ; } public String run ( ) { int triangle = 0 ; for ( int i = 1 ; ; i ++ ) { if ( Integer . MAX_VALUE - triangle < i ) throw new ArithmeticException ( " Overflow " ) ; triangle += i ; if ( countDivisors ( triangle ) > 500 ) return Integer . toString ( triangle ) ; } } private static int countDivisors ( int n ) { int count = 0 ; int end = Library . sqrt ( n ) ; for ( int i = 1 ; i < end ; i ++ ) { if ( n % i == 0 ) count += 2 ; } if ( end * end == n ) count ++ ; return count ; } }
| import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT triangle = 0 NEW_LINE for i in itertools . count ( 1 ) : NEW_LINE INDENT triangle += i NEW_LINE if num_divisors ( triangle ) > 500 : NEW_LINE INDENT return str ( triangle ) NEW_LINE DEDENT DEDENT DEDENT def num_divisors ( n ) : NEW_LINE INDENT end = eulerlib . sqrt ( n ) NEW_LINE result = sum ( 2 for i in range ( 1 , end + 1 ) if n % i == 0 ) NEW_LINE if end ** 2 == n : NEW_LINE INDENT result -= 1 NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40241 | public final class p002 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p002 ( ) . run ( ) ) ; } public String run ( ) { int sum = 0 ; int x = 1 ; int y = 2 ; while ( x <= 4000000 ) { if ( x % 2 == 0 ) sum += x ; int z = x + y ; x = y ; y = z ; } return Integer . toString ( sum ) ; } }
| def compute ( ) : NEW_LINE INDENT ans = 0 NEW_LINE x = 1 NEW_LINE y = 2 NEW_LINE while x <= 4000000 : NEW_LINE INDENT if x % 2 == 0 : NEW_LINE INDENT ans += x NEW_LINE DEDENT x , y = y , x + y NEW_LINE DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40242 | public final class p033 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p033 ( ) . run ( ) ) ; } public String run ( ) { int numer = 1 ; int denom = 1 ; for ( int d = 10 ; d < 100 ; d ++ ) { for ( int n = 10 ; n < d ; n ++ ) { int n0 = n % 10 , n1 = n / 10 ; int d0 = d % 10 , d1 = d / 10 ; if ( n1 == d0 && n0 * d == n * d1 || n0 == d1 && n1 * d == n * d0 ) { numer *= n ; denom *= d ; } } } return Integer . toString ( denom / Library . gcd ( numer , denom ) ) ; } }
| import fractions NEW_LINE def compute ( ) : NEW_LINE INDENT numer = 1 NEW_LINE denom = 1 NEW_LINE for d in range ( 10 , 100 ) : NEW_LINE INDENT for n in range ( 10 , d ) : NEW_LINE INDENT n0 = n % 10 NEW_LINE n1 = n // 10 NEW_LINE d0 = d % 10 NEW_LINE d1 = d // 10 NEW_LINE if ( n1 == d0 and n0 * d == n * d1 ) or ( n0 == d1 and n1 * d == n * d0 ) : NEW_LINE INDENT numer *= n NEW_LINE denom *= d NEW_LINE DEDENT DEDENT DEDENT return str ( denom // fractions . gcd ( numer , denom ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40243 | public final class p091 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p091 ( ) . run ( ) ) ; } private static final int LIMIT = 50 ; public String run ( ) { int count = 0 ; for ( int x1 = 0 ; x1 <= LIMIT ; x1 ++ ) { for ( int y1 = 0 ; y1 <= LIMIT ; y1 ++ ) { for ( int x2 = 0 ; x2 <= LIMIT ; x2 ++ ) { for ( int y2 = 0 ; y2 <= LIMIT ; y2 ++ ) { if ( y2 * x1 < y1 * x2 && isRightTriangle ( x1 , y1 , x2 , y2 ) ) count ++ ; } } } } return Integer . toString ( count ) ; } private static boolean isRightTriangle ( int x1 , int y1 , int x2 , int y2 ) { int a = x1 * x1 + y1 * y1 ; int b = x2 * x2 + y2 * y2 ; int c = ( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 ) ; return a + b == c || b + c == a || c + a == b ; } }
| def compute ( ) : NEW_LINE INDENT LIMIT = 51 NEW_LINE ans = sum ( 1 for x1 in range ( LIMIT ) for y1 in range ( LIMIT ) for x2 in range ( LIMIT ) for y2 in range ( LIMIT ) if y2 * x1 < y1 * x2 and is_right_triangle ( x1 , y1 , x2 , y2 ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def is_right_triangle ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT a = x1 ** 2 + y1 ** 2 NEW_LINE b = x2 ** 2 + y2 ** 2 NEW_LINE c = ( x2 - x1 ) ** 2 + ( y2 - y1 ) ** 2 NEW_LINE return ( a + b == c ) or ( b + c == a ) or ( c + a == b ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40244 | import java . math . BigInteger ; public final class p113 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p113 ( ) . run ( ) ) ; } private static final int DIGITS = 100 ; public String run ( ) { BigInteger increasing = Library . binomial ( DIGITS + 9 , 9 ) . subtract ( BigInteger . ONE ) ; BigInteger decreasing = Library . binomial ( DIGITS + 10 , 10 ) . subtract ( BigInteger . valueOf ( DIGITS + 1 ) ) ; BigInteger flat = BigInteger . valueOf ( DIGITS * 9 ) ; return increasing . add ( decreasing ) . subtract ( flat ) . toString ( ) ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT DIGITS = 100 NEW_LINE increasing = eulerlib . binomial ( DIGITS + 9 , 9 ) - 1 NEW_LINE decreasing = eulerlib . binomial ( DIGITS + 10 , 10 ) - ( DIGITS + 1 ) NEW_LINE flat = DIGITS * 9 NEW_LINE ans = increasing + decreasing - flat NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40245 | public final class p071 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p071 ( ) . run ( ) ) ; } private static final int LIMIT = 1000000 ; public String run ( ) { int maxN = 0 ; int maxD = 1 ; for ( int d = 1 ; d <= LIMIT ; d ++ ) { int n = d * 3 / 7 ; if ( d % 7 == 0 ) n -- ; if ( ( long ) n * maxD > ( long ) maxN * d ) { maxN = n ; maxD = d ; } } return Integer . toString ( maxN ) ; } }
| def compute ( ) : NEW_LINE INDENT LIMIT = 1000000 NEW_LINE maxnumer = 0 NEW_LINE maxdenom = 1 NEW_LINE for d in range ( 1 , LIMIT + 1 ) : NEW_LINE INDENT n = d * 3 // 7 NEW_LINE if d % 7 == 0 : NEW_LINE INDENT n -= 1 NEW_LINE DEDENT if n * maxdenom > d * maxnumer : NEW_LINE INDENT maxnumer = n NEW_LINE maxdenom = d NEW_LINE DEDENT DEDENT return str ( maxnumer ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40246 | public final class p003 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p003 ( ) . run ( ) ) ; } public String run ( ) { long n = 600851475143L ; while ( true ) { long p = smallestFactor ( n ) ; if ( p < n ) n /= p ; else return Long . toString ( n ) ; } } private static long smallestFactor ( long n ) { if ( n <= 1 ) throw new IllegalArgumentException ( ) ; for ( long i = 2 , end = Library . sqrt ( n ) ; i <= end ; i ++ ) { if ( n % i == 0 ) return i ; } return n ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT n = 600851475143 NEW_LINE while True : NEW_LINE INDENT p = smallest_prime_factor ( n ) NEW_LINE if p < n : NEW_LINE INDENT n //= p NEW_LINE DEDENT else : NEW_LINE INDENT return str ( n ) NEW_LINE DEDENT DEDENT DEDENT def smallest_prime_factor ( n ) : NEW_LINE INDENT assert n >= 2 NEW_LINE for i in range ( 2 , eulerlib . sqrt ( n ) + 1 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return n NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40247 | import java . util . HashSet ; import java . util . Set ; public final class p125 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p125 ( ) . run ( ) ) ; } public String run ( ) { Set < Integer > nums = new HashSet < > ( ) ; for ( int i = 1 ; i <= 10000 ; i ++ ) { int sum = i * i ; for ( int j = i + 1 ; ; j ++ ) { sum += j * j ; if ( sum >= 100000000 ) break ; if ( Library . isPalindrome ( sum ) ) nums . add ( sum ) ; } } long sum = 0 ; for ( int x : nums ) sum += x ; return Long . toString ( sum ) ; } }
| import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT nums = set ( ) NEW_LINE for i in range ( 1 , 10001 ) : NEW_LINE INDENT sigma = i * i NEW_LINE for j in itertools . count ( i + 1 ) : NEW_LINE INDENT sigma += j * j NEW_LINE if sigma >= 100000000 : NEW_LINE INDENT break NEW_LINE DEDENT s = str ( sigma ) NEW_LINE if s == s [ : : - 1 ] : NEW_LINE INDENT nums . add ( sigma ) NEW_LINE DEDENT DEDENT DEDENT return str ( sum ( nums ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40248 | import java . math . BigInteger ; public final class p121 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p121 ( ) . run ( ) ) ; } private static final int TURNS = 15 ; public String run ( ) { BigInteger [ ] [ ] ways = new BigInteger [ TURNS + 1 ] [ ] ; ways [ 0 ] = new BigInteger [ ] { BigInteger . ONE } ; for ( int i = 1 ; i <= TURNS ; i ++ ) { ways [ i ] = new BigInteger [ i + 1 ] ; for ( int j = 0 ; j <= i ; j ++ ) { BigInteger temp = BigInteger . ZERO ; if ( j < i ) temp = ways [ i - 1 ] [ j ] . multiply ( BigInteger . valueOf ( i ) ) ; if ( j > 0 ) temp = temp . add ( ways [ i - 1 ] [ j - 1 ] ) ; ways [ i ] [ j ] = temp ; } } BigInteger numer = BigInteger . ZERO ; for ( int i = TURNS / 2 + 1 ; i <= TURNS ; i ++ ) numer = numer . add ( ways [ TURNS ] [ i ] ) ; BigInteger denom = Library . factorial ( TURNS + 1 ) ; return denom . divide ( numer ) . toString ( ) ; } }
| import math NEW_LINE def compute ( ) : NEW_LINE INDENT TURNS = 15 NEW_LINE ways = [ [ 1 ] ] NEW_LINE for i in range ( 1 , TURNS + 1 ) : NEW_LINE INDENT row = [ ] NEW_LINE for j in range ( i + 1 ) : NEW_LINE INDENT temp = 0 NEW_LINE if j < i : NEW_LINE INDENT temp = ways [ i - 1 ] [ j ] * i NEW_LINE DEDENT if j > 0 : NEW_LINE INDENT temp += ways [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT row . append ( temp ) NEW_LINE DEDENT ways . append ( row ) NEW_LINE DEDENT numer = sum ( ways [ TURNS ] [ i ] for i in range ( TURNS // 2 + 1 , TURNS + 1 ) ) NEW_LINE denom = math . factorial ( TURNS + 1 ) NEW_LINE return str ( denom // numer ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40249 | import java . math . BigInteger ; public final class p025 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p025 ( ) . run ( ) ) ; } private static final int DIGITS = 1000 ; public String run ( ) { BigInteger lowerThres = BigInteger . TEN . pow ( DIGITS - 1 ) ; BigInteger upperThres = BigInteger . TEN . pow ( DIGITS ) ; BigInteger prev = BigInteger . ONE ; BigInteger cur = BigInteger . ZERO ; for ( int i = 0 ; ; i ++ ) { if ( cur . compareTo ( upperThres ) >= 0 ) throw new RuntimeException ( " Not β found " ) ; else if ( cur . compareTo ( lowerThres ) >= 0 ) return Integer . toString ( i ) ; BigInteger temp = cur . add ( prev ) ; prev = cur ; cur = temp ; } } }
| import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT DIGITS = 1000 NEW_LINE prev = 1 NEW_LINE cur = 0 NEW_LINE for i in itertools . count ( ) : NEW_LINE INDENT if len ( str ( cur ) ) > DIGITS : NEW_LINE INDENT raise RuntimeError ( " Not β found " ) NEW_LINE DEDENT elif len ( str ( cur ) ) == DIGITS : NEW_LINE INDENT return str ( i ) NEW_LINE DEDENT prev , cur = cur , prev + cur NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40250 | import java . util . Arrays ; public final class p127 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p127 ( ) . run ( ) ) ; } private static final int LIMIT = 120000 ; public String run ( ) { int [ ] rads = new int [ LIMIT ] ; Arrays . fill ( rads , 1 , rads . length , 1 ) ; for ( int i = 2 ; i < rads . length ; i ++ ) { if ( rads [ i ] == 1 ) { for ( int j = i ; j < rads . length ; j += i ) rads [ j ] *= i ; } } long sum = 0 ; for ( int c = 2 ; c < LIMIT ; c ++ ) { if ( rads [ c ] == c ) continue ; for ( int a = 1 , end = ( c - 1 ) / 2 ; a <= end ; a ++ ) { int b = c - a ; assert a < b ; if ( ( long ) rads [ a ] * rads [ b ] * rads [ c ] < c && Library . gcd ( a , b ) == 1 ) sum += c ; } } return Long . toString ( sum ) ; } }
| import fractions NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 120000 NEW_LINE rads = [ 0 ] + [ 1 ] * ( LIMIT - 1 ) NEW_LINE for i in range ( 2 , len ( rads ) ) : NEW_LINE INDENT if rads [ i ] == 1 : NEW_LINE INDENT for j in range ( i , len ( rads ) , i ) : NEW_LINE INDENT rads [ j ] *= i NEW_LINE DEDENT DEDENT DEDENT sortedrads = sorted ( ( rad , n ) for ( n , rad ) in enumerate ( rads ) ) NEW_LINE sortedrads = sortedrads [ 1 : ] NEW_LINE ans = 0 NEW_LINE for c in range ( 2 , LIMIT ) : NEW_LINE INDENT for ( rad , a ) in sortedrads : NEW_LINE INDENT rad *= rads [ c ] NEW_LINE if rad >= c : NEW_LINE INDENT break NEW_LINE DEDENT b = c - a NEW_LINE if a < b and rad * rads [ b ] < c and fractions . gcd ( a , b ) == 1 : NEW_LINE INDENT ans += c NEW_LINE DEDENT DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40251 | public final class p120 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p120 ( ) . run ( ) ) ; } public String run ( ) { int sum = 0 ; for ( int a = 3 ; a <= 1000 ; a ++ ) sum += a * ( a - ( a % 2 == 0 ? 2 : 1 ) ) ; return Integer . toString ( sum ) ; } }
| def compute ( ) : NEW_LINE INDENT ans = sum ( i * ( i - ( 2 if i % 2 == 0 else 1 ) ) for i in range ( 3 , 1001 ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40252 | import java . util . Arrays ; public final class p124 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p124 ( ) . run ( ) ) ; } private static final int LIMIT = 100000 ; public String run ( ) { int [ ] rads = new int [ LIMIT + 1 ] ; Arrays . fill ( rads , 1 , rads . length , 1 ) ; for ( int i = 2 ; i < rads . length ; i ++ ) { if ( rads [ i ] == 1 ) { for ( int j = i ; j < rads . length ; j += i ) rads [ j ] *= i ; } } IntPair [ ] data = new IntPair [ LIMIT ] ; for ( int i = 0 ; i < data . length ; i ++ ) data [ i ] = new IntPair ( rads [ i + 1 ] , i + 1 ) ; Arrays . sort ( data ) ; return Integer . toString ( data [ 10000 - 1 ] . b ) ; } private static final class IntPair implements Comparable < IntPair > { public final int a ; public final int b ; public IntPair ( int a , int b ) { this . a = a ; this . b = b ; } public int compareTo ( IntPair other ) { if ( a != other . a ) return Integer . compare ( a , other . a ) ; else return Integer . compare ( b , other . b ) ; } } }
| def compute ( ) : NEW_LINE INDENT LIMIT = 100000 NEW_LINE rads = [ 0 ] + [ 1 ] * LIMIT NEW_LINE for i in range ( 2 , len ( rads ) ) : NEW_LINE INDENT if rads [ i ] == 1 : NEW_LINE INDENT for j in range ( i , len ( rads ) , i ) : NEW_LINE INDENT rads [ j ] *= i NEW_LINE DEDENT DEDENT DEDENT data = sorted ( ( rad , i ) for ( i , rad ) in enumerate ( rads ) ) NEW_LINE return str ( data [ 10000 ] [ 1 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40253 | import java . math . BigInteger ; import java . util . ArrayList ; import java . util . List ; public final class p172 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p172 ( ) . run ( ) ) ; } private static final int LENGTH = 18 ; private static final int MAX_COUNT = 3 ; private static final int BASE = 10 ; public String run ( ) { BigInteger ways = partitionAndCount ( LENGTH , MAX_COUNT , new ArrayList < Integer > ( ) ) ; BigInteger BASE_BI = BigInteger . valueOf ( BASE ) ; ways = ways . multiply ( BASE_BI . subtract ( BigInteger . ONE ) ) ; ways = divideExactly ( ways , BASE_BI ) ; return ways . toString ( ) ; } private BigInteger partitionAndCount ( int sum , int max , List < Integer > terms ) { if ( terms . size ( ) == BASE ) { if ( sum == 0 ) return countWays ( terms ) ; else return BigInteger . ZERO ; } else { BigInteger result = BigInteger . ZERO ; for ( int i = Math . min ( max , sum ) ; i >= 0 ; i -- ) { terms . add ( i ) ; result = result . add ( partitionAndCount ( sum - i , i , terms ) ) ; terms . remove ( terms . size ( ) - 1 ) ; } return result ; } } private BigInteger countWays ( List < Integer > freqs ) { int [ ] histogram = new int [ MAX_COUNT + 1 ] ; for ( int x : freqs ) histogram [ x ] ++ ; BigInteger ways = Library . factorial ( BASE ) ; for ( int x : histogram ) ways = ways . divide ( Library . factorial ( x ) ) ; ways = ways . multiply ( Library . factorial ( LENGTH ) ) ; for ( int x : freqs ) ways = ways . divide ( Library . factorial ( x ) ) ; return ways ; } private static BigInteger divideExactly ( BigInteger x , BigInteger y ) { BigInteger [ ] temp = x . divideAndRemainder ( y ) ; if ( temp [ 1 ] . signum ( ) != 0 ) throw new IllegalArgumentException ( " Not β divisible " ) ; return temp [ 0 ] ; } }
| import math NEW_LINE LENGTH = 18 NEW_LINE MAX_COUNT = 3 NEW_LINE BASE = 10 NEW_LINE def compute ( ) : NEW_LINE INDENT ans = partition_and_count ( LENGTH , MAX_COUNT , [ ] ) NEW_LINE ans = divide_exactly ( ans * ( BASE - 1 ) , BASE ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def partition_and_count ( sum , max , terms ) : NEW_LINE INDENT if len ( terms ) == BASE : NEW_LINE INDENT return count_ways ( terms ) if ( sum == 0 ) else 0 NEW_LINE DEDENT else : NEW_LINE INDENT result = 0 NEW_LINE for i in reversed ( range ( min ( max , sum ) + 1 ) ) : NEW_LINE INDENT terms . append ( i ) NEW_LINE result += partition_and_count ( sum - i , i , terms ) NEW_LINE terms . pop ( ) NEW_LINE DEDENT return result NEW_LINE DEDENT DEDENT def count_ways ( freqs ) : NEW_LINE INDENT histogram = [ 0 ] * ( MAX_COUNT + 1 ) NEW_LINE for x in freqs : NEW_LINE INDENT histogram [ x ] += 1 NEW_LINE DEDENT ways = math . factorial ( BASE ) NEW_LINE for x in histogram : NEW_LINE INDENT ways //= math . factorial ( x ) NEW_LINE DEDENT ways *= math . factorial ( LENGTH ) NEW_LINE for x in freqs : NEW_LINE INDENT ways //= math . factorial ( x ) NEW_LINE DEDENT return ways NEW_LINE DEDENT def divide_exactly ( x , y ) : NEW_LINE INDENT if x % y != 0 : NEW_LINE INDENT raise ValueError ( " Not β divisible " ) NEW_LINE DEDENT return x // y NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40254 | import java . math . BigInteger ; public final class p013 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p013 ( ) . run ( ) ) ; } public String run ( ) { BigInteger sum = BigInteger . ZERO ; for ( String num : NUMBERS ) sum = sum . add ( new BigInteger ( num ) ) ; return sum . toString ( ) . substring ( 0 , 10 ) ; } private static String [ ] NUMBERS = { "37107287533902102798797998220837590246510135740250" , "46376937677490009712648124896970078050417018260538" , "74324986199524741059474233309513058123726617309629" , "91942213363574161572522430563301811072406154908250" , "23067588207539346171171980310421047513778063246676" , "89261670696623633820136378418383684178734361726757" , "28112879812849979408065481931592621691275889832738" , "44274228917432520321923589422876796487670272189318" , "47451445736001306439091167216856844588711603153276" , "70386486105843025439939619828917593665686757934951" , "62176457141856560629502157223196586755079324193331" , "64906352462741904929101432445813822663347944758178" , "92575867718337217661963751590579239728245598838407" , "58203565325359399008402633568948830189458628227828" , "80181199384826282014278194139940567587151170094390" , "35398664372827112653829987240784473053190104293586" , "86515506006295864861532075273371959191420517255829" , "71693888707715466499115593487603532921714970056938" , "54370070576826684624621495650076471787294438377604" , "53282654108756828443191190634694037855217779295145" , "36123272525000296071075082563815656710885258350721" , "45876576172410976447339110607218265236877223636045" , "17423706905851860660448207621209813287860733969412" , "81142660418086830619328460811191061556940512689692" , "51934325451728388641918047049293215058642563049483" , "62467221648435076201727918039944693004732956340691" , "15732444386908125794514089057706229429197107928209" , "55037687525678773091862540744969844508330393682126" , "18336384825330154686196124348767681297534375946515" , "80386287592878490201521685554828717201219257766954" , "78182833757993103614740356856449095527097864797581" , "16726320100436897842553539920931837441497806860984" , "48403098129077791799088218795327364475675590848030" , "87086987551392711854517078544161852424320693150332" , "59959406895756536782107074926966537676326235447210" , "69793950679652694742597709739166693763042633987085" , "41052684708299085211399427365734116182760315001271" , "65378607361501080857009149939512557028198746004375" , "35829035317434717326932123578154982629742552737307" , "94953759765105305946966067683156574377167401875275" , "88902802571733229619176668713819931811048770190271" , "25267680276078003013678680992525463401061632866526" , "36270218540497705585629946580636237993140746255962" , "24074486908231174977792365466257246923322810917141" , "91430288197103288597806669760892938638285025333403" , "34413065578016127815921815005561868836468420090470" , "23053081172816430487623791969842487255036638784583" , "11487696932154902810424020138335124462181441773470" , "63783299490636259666498587618221225225512486764533" , "67720186971698544312419572409913959008952310058822" , "95548255300263520781532296796249481641953868218774" , "76085327132285723110424803456124867697064507995236" , "37774242535411291684276865538926205024910326572967" , "23701913275725675285653248258265463092207058596522" , "29798860272258331913126375147341994889534765745501" , "18495701454879288984856827726077713721403798879715" , "38298203783031473527721580348144513491373226651381" , "34829543829199918180278916522431027392251122869539" , "40957953066405232632538044100059654939159879593635" , "29746152185502371307642255121183693803580388584903" , "41698116222072977186158236678424689157993532961922" , "62467957194401269043877107275048102390895523597457" , "23189706772547915061505504953922979530901129967519" , "86188088225875314529584099251203829009407770775672" , "11306739708304724483816533873502340845647058077308" , "82959174767140363198008187129011875491310547126581" , "97623331044818386269515456334926366572897563400500" , "42846280183517070527831839425882145521227251250327" , "55121603546981200581762165212827652751691296897789" , "32238195734329339946437501907836945765883352399886" , "75506164965184775180738168837861091527357929701337" , "62177842752192623401942399639168044983993173312731" , "32924185707147349566916674687634660915035914677504" , "99518671430235219628894890102423325116913619626622" , "73267460800591547471830798392868535206946944540724" , "76841822524674417161514036427982273348055556214818" , "97142617910342598647204516893989422179826088076852" , "87783646182799346313767754307809363333018982642090" , "10848802521674670883215120185883543223812876952786" , "71329612474782464538636993009049310363619763878039" , "62184073572399794223406235393808339651327408011116" , "66627891981488087797941876876144230030984490851411" , "60661826293682836764744779239180335110989069790714" , "85786944089552990653640447425576083659976645795096" , "66024396409905389607120198219976047599490197230297" , "64913982680032973156037120041377903785566085089252" , "16730939319872750275468906903707539413042652315011" , "94809377245048795150954100921645863754710598436791" , "78639167021187492431995700641917969777599028300699" , "15368713711936614952811305876380278410754449733078" , "40789923115535562561142322423255033685442488917353" , "44889911501440648020369068063960672322193204149535" , "41503128880339536053299340368006977710650566631954" , "81234880673210146739058568557934581403627822703280" , "82616570773948327592232845941706525094512325230608" , "22918802058777319719839450180888072429661980811197" , "77158542502016545090413245809786882778948721859617" , "72107838435069186155435662884062257473692284509516" , "20849603980134001723930671666823555245252804609722" , "53503534226472524250874054075591789781264330331690" , } ; }
| def compute ( ) : NEW_LINE INDENT return str ( sum ( NUMBERS ) ) [ : 10 ] NEW_LINE DEDENT NUMBERS = [ 37107287533902102798797998220837590246510135740250 , 46376937677490009712648124896970078050417018260538 , 74324986199524741059474233309513058123726617309629 , 91942213363574161572522430563301811072406154908250 , 23067588207539346171171980310421047513778063246676 , 89261670696623633820136378418383684178734361726757 , 28112879812849979408065481931592621691275889832738 , 44274228917432520321923589422876796487670272189318 , 47451445736001306439091167216856844588711603153276 , 70386486105843025439939619828917593665686757934951 , 62176457141856560629502157223196586755079324193331 , 64906352462741904929101432445813822663347944758178 , 92575867718337217661963751590579239728245598838407 , 58203565325359399008402633568948830189458628227828 , 80181199384826282014278194139940567587151170094390 , 35398664372827112653829987240784473053190104293586 , 86515506006295864861532075273371959191420517255829 , 71693888707715466499115593487603532921714970056938 , 54370070576826684624621495650076471787294438377604 , 53282654108756828443191190634694037855217779295145 , 36123272525000296071075082563815656710885258350721 , 45876576172410976447339110607218265236877223636045 , 17423706905851860660448207621209813287860733969412 , 81142660418086830619328460811191061556940512689692 , 51934325451728388641918047049293215058642563049483 , 62467221648435076201727918039944693004732956340691 , 15732444386908125794514089057706229429197107928209 , 55037687525678773091862540744969844508330393682126 , 18336384825330154686196124348767681297534375946515 , 80386287592878490201521685554828717201219257766954 , 78182833757993103614740356856449095527097864797581 , 16726320100436897842553539920931837441497806860984 , 48403098129077791799088218795327364475675590848030 , 87086987551392711854517078544161852424320693150332 , 59959406895756536782107074926966537676326235447210 , 69793950679652694742597709739166693763042633987085 , 41052684708299085211399427365734116182760315001271 , 65378607361501080857009149939512557028198746004375 , 35829035317434717326932123578154982629742552737307 , 94953759765105305946966067683156574377167401875275 , 88902802571733229619176668713819931811048770190271 , 25267680276078003013678680992525463401061632866526 , 36270218540497705585629946580636237993140746255962 , 24074486908231174977792365466257246923322810917141 , 91430288197103288597806669760892938638285025333403 , 34413065578016127815921815005561868836468420090470 , 23053081172816430487623791969842487255036638784583 , 11487696932154902810424020138335124462181441773470 , 63783299490636259666498587618221225225512486764533 , 67720186971698544312419572409913959008952310058822 , 95548255300263520781532296796249481641953868218774 , 76085327132285723110424803456124867697064507995236 , 37774242535411291684276865538926205024910326572967 , 23701913275725675285653248258265463092207058596522 , 29798860272258331913126375147341994889534765745501 , 18495701454879288984856827726077713721403798879715 , 38298203783031473527721580348144513491373226651381 , 34829543829199918180278916522431027392251122869539 , 40957953066405232632538044100059654939159879593635 , 29746152185502371307642255121183693803580388584903 , 41698116222072977186158236678424689157993532961922 , 62467957194401269043877107275048102390895523597457 , 23189706772547915061505504953922979530901129967519 , 86188088225875314529584099251203829009407770775672 , 11306739708304724483816533873502340845647058077308 , 82959174767140363198008187129011875491310547126581 , 97623331044818386269515456334926366572897563400500 , 42846280183517070527831839425882145521227251250327 , 55121603546981200581762165212827652751691296897789 , 32238195734329339946437501907836945765883352399886 , 75506164965184775180738168837861091527357929701337 , 62177842752192623401942399639168044983993173312731 , 32924185707147349566916674687634660915035914677504 , 99518671430235219628894890102423325116913619626622 , 73267460800591547471830798392868535206946944540724 , 76841822524674417161514036427982273348055556214818 , 97142617910342598647204516893989422179826088076852 , 87783646182799346313767754307809363333018982642090 , 10848802521674670883215120185883543223812876952786 , 71329612474782464538636993009049310363619763878039 , 62184073572399794223406235393808339651327408011116 , 66627891981488087797941876876144230030984490851411 , 60661826293682836764744779239180335110989069790714 , 85786944089552990653640447425576083659976645795096 , 66024396409905389607120198219976047599490197230297 , 64913982680032973156037120041377903785566085089252 , 16730939319872750275468906903707539413042652315011 , 94809377245048795150954100921645863754710598436791 , 78639167021187492431995700641917969777599028300699 , 15368713711936614952811305876380278410754449733078 , 40789923115535562561142322423255033685442488917353 , 44889911501440648020369068063960672322193204149535 , 41503128880339536053299340368006977710650566631954 , 81234880673210146739058568557934581403627822703280 , 82616570773948327592232845941706525094512325230608 , 22918802058777319719839450180888072429661980811197 , 77158542502016545090413245809786882778948721859617 , 72107838435069186155435662884062257473692284509516 , 20849603980134001723930671666823555245252804609722 , 53503534226472524250874054075591789781264330331690 , ] NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40255 | public final class p587 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p587 ( ) . run ( ) ) ; } public String run ( ) { double lSectionArea = 1 - Math . PI / 4 ; for ( int i = 1 ; ; i ++ ) { double slope = 1.0 / i ; double a = slope * slope + 1 ; double b = - 2 * ( slope + 1 ) ; double c = 1 ; double x = ( 2 * c ) / ( - b + Math . sqrt ( b * b - 4 * a * c ) ) ; double concaveTriangleArea = ( x * x * slope / 2 ) + ( integral ( 1 ) - integral ( x ) ) ; if ( concaveTriangleArea / lSectionArea < 0.001 ) return Integer . toString ( i ) ; if ( i == Integer . MAX_VALUE ) throw new AssertionError ( ) ; } } private static double integral ( double x ) { double t = x - 1 ; return t - ( Math . sqrt ( x * ( 2 - x ) ) * t + Math . asin ( t ) ) / 2 ; } }
| import itertools , math NEW_LINE def compute ( ) : NEW_LINE INDENT def integral ( x ) : NEW_LINE INDENT t = x - 1.0 NEW_LINE return t - ( math . sqrt ( x * ( 2.0 - x ) ) * t + math . asin ( t ) ) / 2.0 NEW_LINE DEDENT lsectionarea = 1.0 - math . pi / 4.0 NEW_LINE for i in itertools . count ( 1 ) : NEW_LINE INDENT slope = 1.0 / i NEW_LINE a = slope ** 2 + 1.0 NEW_LINE b = - 2.0 * ( slope + 1.0 ) NEW_LINE c = 1.0 NEW_LINE x = ( 2.0 * c ) / ( - b + math . sqrt ( b * b - 4 * a * c ) ) NEW_LINE concavetrianglearea = ( x ** 2 * slope / 2 ) + ( integral ( 1.0 ) - integral ( x ) ) NEW_LINE if concavetrianglearea / lsectionarea < 0.001 : NEW_LINE INDENT return str ( i ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40256 | import java . math . BigInteger ; public final class p106 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p106 ( ) . run ( ) ) ; } private static final int SET_SIZE = 12 ; public String run ( ) { BigInteger ans = BigInteger . ZERO ; for ( int i = 2 ; i * 2 <= SET_SIZE ; i ++ ) { BigInteger x = Library . binomial ( SET_SIZE , i * 2 ) ; BigInteger y = Library . binomial ( i * 2 , i ) . shiftRight ( 1 ) ; BigInteger z = Library . binomial ( i * 2 , i ) . divide ( BigInteger . valueOf ( i + 1 ) ) ; ans = ans . add ( x . multiply ( y . subtract ( z ) ) ) ; } return ans . toString ( ) ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT SET_SIZE = 12 NEW_LINE def catalan ( n ) : NEW_LINE INDENT return eulerlib . binomial ( n * 2 , n ) // ( n + 1 ) NEW_LINE DEDENT ans = sum ( eulerlib . binomial ( SET_SIZE , i * 2 ) * ( eulerlib . binomial ( i * 2 , i ) // 2 - catalan ( i ) ) for i in range ( 2 , SET_SIZE // 2 + 1 ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40257 | public final class p017 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p017 ( ) . run ( ) ) ; } public String run ( ) { int sum = 0 ; for ( int i = 1 ; i <= 1000 ; i ++ ) sum += toEnglish ( i ) . length ( ) ; return Integer . toString ( sum ) ; } private static String toEnglish ( int n ) { if ( 0 <= n && n < 20 ) return ONES [ n ] ; else if ( 20 <= n && n < 100 ) return TENS [ n / 10 ] + ( n % 10 != 0 ? ONES [ n % 10 ] : " " ) ; else if ( 100 <= n && n < 1000 ) return ONES [ n / 100 ] + " hundred " + ( n % 100 != 0 ? " and " + toEnglish ( n % 100 ) : " " ) ; else if ( 1000 <= n && n < 1000000 ) return toEnglish ( n / 1000 ) + " thousand " + ( n % 1000 != 0 ? toEnglish ( n % 1000 ) : " " ) ; else throw new IllegalArgumentException ( ) ; } private static String [ ] ONES = { " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " } ; private static String [ ] TENS = { " " , " " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " } ; }
| def compute ( ) : NEW_LINE INDENT ans = sum ( len ( to_english ( i ) ) for i in range ( 1 , 1001 ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def to_english ( n ) : NEW_LINE INDENT if 0 <= n < 20 : NEW_LINE INDENT return ONES [ n ] NEW_LINE DEDENT elif 20 <= n < 100 : NEW_LINE INDENT return TENS [ n // 10 ] + ( ONES [ n % 10 ] if ( n % 10 != 0 ) else " " ) NEW_LINE DEDENT elif 100 <= n < 1000 : NEW_LINE INDENT return ONES [ n // 100 ] + " hundred " + ( ( " and " + to_english ( n % 100 ) ) if ( n % 100 != 0 ) else " " ) NEW_LINE DEDENT elif 1000 <= n < 1000000 : NEW_LINE INDENT return to_english ( n // 1000 ) + " thousand " + ( to_english ( n % 1000 ) if ( n % 1000 != 0 ) else " " ) NEW_LINE DEDENT else : NEW_LINE INDENT raise ValueError ( ) NEW_LINE DEDENT DEDENT ONES = [ " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " ] NEW_LINE TENS = [ " " , " " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " ] NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40258 | public final class p046 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p046 ( ) . run ( ) ) ; } public String run ( ) { for ( int i = 9 ; ; i += 2 ) { if ( ! satisfiesConjecture ( i ) ) return Integer . toString ( i ) ; } } private static boolean satisfiesConjecture ( int n ) { if ( n % 2 == 0 || Library . isPrime ( n ) ) return true ; for ( int i = 1 ; i * i * 2 <= n ; i ++ ) { if ( Library . isPrime ( n - i * i * 2 ) ) return true ; } return false ; } }
| import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT ans = next ( itertools . filterfalse ( test_goldbach , itertools . count ( 9 , 2 ) ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def test_goldbach ( n ) : NEW_LINE INDENT if n % 2 == 0 or eulerlib . is_prime ( n ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in itertools . count ( 1 ) : NEW_LINE INDENT k = n - 2 * i * i NEW_LINE if k <= 0 : NEW_LINE INDENT return False NEW_LINE DEDENT elif eulerlib . is_prime ( k ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40259 | import java . util . Arrays ; public final class p038 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p038 ( ) . run ( ) ) ; } public String run ( ) { int max = - 1 ; for ( int n = 2 ; n <= 9 ; n ++ ) { for ( int i = 1 ; i < Library . pow ( 10 , 9 / n ) ; i ++ ) { String concat = " " ; for ( int j = 1 ; j <= n ; j ++ ) concat += i * j ; if ( isPandigital ( concat ) ) max = Math . max ( Integer . parseInt ( concat ) , max ) ; } } return Integer . toString ( max ) ; } 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" ) ; } }
| def compute ( ) : NEW_LINE INDENT ans = " " NEW_LINE for n in range ( 2 , 10 ) : NEW_LINE INDENT for i in range ( 1 , 10 ** ( 9 // n ) ) : NEW_LINE INDENT s = " " . join ( str ( i * j ) for j in range ( 1 , n + 1 ) ) NEW_LINE if " " . join ( sorted ( s ) ) == "123456789" : NEW_LINE INDENT ans = max ( s , ans ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40260 | public final class p130 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p130 ( ) . run ( ) ) ; } public String run ( ) { int sum = 0 ; int found = 0 ; for ( int i = 7 ; found < 25 ; i += 2 ) { if ( i % 5 != 0 && ! Library . isPrime ( i ) && ( i - 1 ) % findLeastDivisibleRepunit ( i ) == 0 ) { sum += i ; found ++ ; } } return Integer . toString ( sum ) ; } private static int findLeastDivisibleRepunit ( int n ) { if ( n % 2 == 0 || n % 5 == 0 ) return 0 ; if ( n > Integer . MAX_VALUE / 10 ) throw new IllegalArgumentException ( " Arithmetic β overflow " ) ; int sum = 1 ; int pow = 1 ; int k = 1 ; while ( sum % n != 0 ) { k ++ ; pow = pow * 10 % n ; sum = ( sum + pow ) % n ; } return k ; } }
| import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT cond = lambda i : ( i % 5 != 0 ) and ( not eulerlib . is_prime ( i ) ) and ( ( i - 1 ) % find_least_divisible_repunit ( i ) == 0 ) NEW_LINE ans = sum ( itertools . islice ( filter ( cond , itertools . count ( 7 , 2 ) ) , 25 ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def find_least_divisible_repunit ( n ) : NEW_LINE INDENT if n % 2 == 0 or n % 5 == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT sum = 1 NEW_LINE pow = 1 NEW_LINE k = 1 NEW_LINE while sum % n != 0 : NEW_LINE INDENT k += 1 NEW_LINE pow = pow * 10 % n NEW_LINE sum = ( sum + pow ) % n NEW_LINE DEDENT return k NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40261 | import java . math . BigInteger ; public final class p287 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p287 ( ) . run ( ) ) ; } private static final int N = 24 ; private static final long RADIUS_SQUARED = 1L << ( 2 * N - 2 ) ; public String run ( ) { int temp = 1 << ( N - 1 ) ; return compressedLength ( N , - temp , temp , - temp , temp ) . toString ( ) ; } private BigInteger compressedLength ( int n , int xStart , int xEnd , int yStart , int yEnd ) { assert n >= 0 ; assert xStart < xEnd && xEnd - xStart == 1 << n ; assert yStart < yEnd && yEnd - yStart == 1 << n ; int minAbsX = xStart <= 0 && 0 < xEnd ? 0 : Math . min ( Math . abs ( xStart ) , Math . abs ( xEnd - 1 ) ) ; int minAbsY = yStart <= 0 && 0 < yEnd ? 0 : Math . min ( Math . abs ( yStart ) , Math . abs ( yEnd - 1 ) ) ; int maxAbsX = Math . max ( Math . abs ( xStart ) , Math . abs ( xEnd - 1 ) ) ; int maxAbsY = Math . max ( Math . abs ( yStart ) , Math . abs ( yEnd - 1 ) ) ; long minRadius = ( long ) minAbsX * minAbsX + ( long ) minAbsY * minAbsY ; long maxRadius = ( long ) maxAbsX * maxAbsX + ( long ) maxAbsY * maxAbsY ; if ( maxRadius <= RADIUS_SQUARED || minRadius > RADIUS_SQUARED ) return BigInteger . valueOf ( 2 ) ; else { int xMid = ( xStart + xEnd ) / 2 ; int yMid = ( yStart + yEnd ) / 2 ; return BigInteger . ONE . add ( compressedLength ( n - 1 , xStart , xMid , yMid , yEnd ) ) . add ( compressedLength ( n - 1 , xMid , xEnd , yMid , yEnd ) ) . add ( compressedLength ( n - 1 , xStart , xMid , yStart , yMid ) ) . add ( compressedLength ( n - 1 , xMid , xEnd , yStart , yMid ) ) ; } } }
| def compute ( ) : NEW_LINE INDENT N = 24 NEW_LINE RADIUS_SQUARED = 2 ** ( 2 * N - 2 ) NEW_LINE def compressed_length ( xstart , xend , ystart , yend ) : NEW_LINE INDENT if xstart * xstart + ystart * ystart > RADIUS_SQUARED : NEW_LINE INDENT return 2 NEW_LINE DEDENT elif ( xend - 1 ) * ( xend - 1 ) + ( yend - 1 ) * ( yend - 1 ) <= RADIUS_SQUARED : NEW_LINE INDENT return 2 NEW_LINE DEDENT else : NEW_LINE INDENT xmid = ( xstart + xend ) >> 1 NEW_LINE ymid = ( ystart + yend ) >> 1 NEW_LINE return ( 1 + compressed_length ( xstart , xmid , ymid , yend ) + compressed_length ( xmid , xend , ymid , yend ) + compressed_length ( xstart , xmid , ystart , ymid ) + compressed_length ( xmid , xend , ystart , ymid ) ) NEW_LINE DEDENT DEDENT temp = 2 ** ( N - 1 ) NEW_LINE return str ( 1 + compressed_length ( 0 , temp , 0 , temp ) + compressed_length ( 0 , temp , 1 , temp + 1 ) + compressed_length ( 1 , temp + 1 , 0 , temp ) + compressed_length ( 1 , temp + 1 , 1 , temp + 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40262 | public final class p112 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p112 ( ) . run ( ) ) ; } public String run ( ) { int bouncy = 0 ; for ( int i = 1 ; ; i ++ ) { if ( isBouncy ( i ) ) bouncy ++ ; if ( bouncy * 100 == i * 99 ) return Integer . toString ( i ) ; } } private static boolean isBouncy ( int x ) { if ( x < 100 ) return false ; else { boolean nonincreasing = true ; boolean nondecreasing = true ; int lastDigit = x % 10 ; x /= 10 ; while ( x != 0 ) { int digit = x % 10 ; if ( digit > lastDigit ) nondecreasing = false ; else if ( digit < lastDigit ) nonincreasing = false ; lastDigit = digit ; x /= 10 ; } return ! nonincreasing && ! nondecreasing ; } } }
| import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT count = 0 NEW_LINE for i in itertools . count ( 1 ) : NEW_LINE INDENT s = str ( i ) NEW_LINE t = " " . join ( sorted ( s ) ) NEW_LINE if s != t and s [ : : - 1 ] != t : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if count * 100 == 99 * i : NEW_LINE INDENT return str ( i ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40263 | public final class p315 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p315 ( ) . run ( ) ) ; } public String run ( ) { boolean [ ] isPrime = Library . listPrimality ( 20000000 ) ; int sum = 0 ; for ( int i = 10000000 ; i < isPrime . length ; i ++ ) { if ( isPrime [ i ] ) sum += samTransitionsMinusMaxTransitions ( i ) ; } return Integer . toString ( sum ) ; } private static int samTransitionsMinusMaxTransitions ( int n ) { int samTrans = 0 ; int maxTrans = 0 ; long segmentState = 0 ; while ( true ) { long newState = numberToSegments ( n ) ; if ( newState == segmentState ) break ; maxTrans += Long . bitCount ( newState ^ segmentState ) ; segmentState = newState ; samTrans += 2 * Long . bitCount ( newState ) ; n = digitSum ( n ) ; } maxTrans += Long . bitCount ( segmentState ) ; return samTrans - maxTrans ; } private static long numberToSegments ( int n ) { if ( n < 0 || n > 999999999 ) throw new IllegalArgumentException ( ) ; long result = 0 ; int i = 0 ; do { result |= ( long ) DECIMAL_DIGIT_TO_SEGMENT [ n % 10 ] << ( i * 7 ) ; n /= 10 ; i ++ ; } while ( n != 0 ) ; return result ; } private static int digitSum ( int n ) { if ( n < 0 ) throw new IllegalArgumentException ( ) ; int result = 0 ; while ( n != 0 ) { result += n % 10 ; n /= 10 ; } return result ; } private static final int [ ] DECIMAL_DIGIT_TO_SEGMENT = { 0x77 , 0x12 , 0x5D , 0x5B , 0x3A , 0x6B , 0x6F , 0x72 , 0x7F , 0x7B } ; }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT isprime = eulerlib . list_primality ( 20000000 ) NEW_LINE ans = sum ( sam_transitions_minus_max_transitions ( i ) for ( i , p ) in enumerate ( isprime ) if i >= 10000000 and p ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def sam_transitions_minus_max_transitions ( n ) : NEW_LINE INDENT samtrans = 0 NEW_LINE maxtrans = 0 NEW_LINE segmentstate = 0 NEW_LINE while True : NEW_LINE INDENT newstate = number_to_segments ( n ) NEW_LINE if newstate == segmentstate : NEW_LINE INDENT break NEW_LINE DEDENT maxtrans += eulerlib . popcount ( newstate ^ segmentstate ) NEW_LINE segmentstate = newstate NEW_LINE samtrans += 2 * eulerlib . popcount ( newstate ) NEW_LINE n = digit_sum ( n ) NEW_LINE DEDENT maxtrans += eulerlib . popcount ( segmentstate ) NEW_LINE return samtrans - maxtrans NEW_LINE DEDENT def number_to_segments ( n ) : NEW_LINE INDENT if n < 0 : NEW_LINE INDENT raise ValueError ( ) NEW_LINE DEDENT result = 0 NEW_LINE i = 0 NEW_LINE while True : NEW_LINE INDENT result |= DECIMAL_DIGIT_TO_SEGMENT [ n % 10 ] << ( i * 7 ) NEW_LINE n //= 10 NEW_LINE i += 1 NEW_LINE if n == 0 : NEW_LINE INDENT return result NEW_LINE DEDENT DEDENT DEDENT def digit_sum ( n ) : NEW_LINE INDENT if n < 0 : NEW_LINE INDENT raise ValueError ( ) NEW_LINE DEDENT result = 0 NEW_LINE while n != 0 : NEW_LINE INDENT result += n % 10 NEW_LINE n //= 10 NEW_LINE DEDENT return result NEW_LINE DEDENT DECIMAL_DIGIT_TO_SEGMENT = [ 0b1110111 , 0b0010010 , 0b1011101 , 0b1011011 , 0b0111010 , 0b1101011 , 0b1101111 , 0b1110010 , 0b1111111 , 0b1111011 ] NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40264 | import java . math . BigInteger ; import java . util . ArrayList ; import java . util . HashSet ; import java . util . List ; import java . util . Set ; public final class p271 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p271 ( ) . run ( ) ) ; } private static final int [ ] FACTORS = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 } ; private List < Set < Integer > > factorSolutions = new ArrayList < > ( ) ; public String run ( ) { for ( int fact : FACTORS ) { Set < Integer > sols = new HashSet < > ( ) ; for ( int j = 1 ; j < fact ; j ++ ) { if ( Library . powMod ( j , 3 , fact ) == 1 ) sols . add ( j ) ; } factorSolutions . add ( sols ) ; } BigInteger sum = buildAndSumSolutions ( 0 , BigInteger . ZERO , BigInteger . ONE ) ; return sum . subtract ( BigInteger . ONE ) . toString ( ) ; } private BigInteger buildAndSumSolutions ( int factorIndex , BigInteger x , BigInteger m ) { if ( factorIndex == FACTORS . length ) return x ; else { BigInteger result = BigInteger . ZERO ; BigInteger fact = BigInteger . valueOf ( FACTORS [ factorIndex ] ) ; for ( int sol : factorSolutions . get ( factorIndex ) ) { BigInteger newx = chineseRemainderTheorem ( x , m , BigInteger . valueOf ( sol ) , fact ) ; BigInteger temp = buildAndSumSolutions ( factorIndex + 1 , newx , m . multiply ( fact ) ) ; result = result . add ( temp ) ; } return result ; } } private static BigInteger chineseRemainderTheorem ( BigInteger a , BigInteger p , BigInteger b , BigInteger q ) { return a . add ( b . subtract ( a ) . multiply ( p . modInverse ( q ) ) . multiply ( p ) ) . mod ( p . multiply ( q ) ) ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT FACTORS = ( 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 ) NEW_LINE factorsols = [ [ j for j in range ( fact ) if pow ( j , 3 , fact ) == 1 ] for fact in FACTORS ] NEW_LINE def build_and_sum_solutions ( i , x , mod ) : NEW_LINE INDENT if i == len ( FACTORS ) : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT fact = FACTORS [ i ] NEW_LINE return sum ( build_and_sum_solutions ( i + 1 , chinese_remainder_theorem ( x , mod , sol , fact ) , mod * fact ) for sol in factorsols [ i ] ) NEW_LINE DEDENT DEDENT ans = build_and_sum_solutions ( 0 , 0 , 1 ) - 1 NEW_LINE return str ( ans ) NEW_LINE DEDENT def chinese_remainder_theorem ( a , p , b , q ) : NEW_LINE INDENT return ( a + ( b - a ) * eulerlib . reciprocal_mod ( p % q , q ) * p ) % ( p * q ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40265 | public final class p231 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p231 ( ) . run ( ) ) ; } private static final int N = 20000000 ; private static final int K = 15000000 ; public String run ( ) { smallestPrimeFactor = Library . listSmallestPrimeFactors ( N ) ; return Long . toString ( factorialPrimeFactorSum ( N ) - factorialPrimeFactorSum ( K ) - factorialPrimeFactorSum ( N - K ) ) ; } private int [ ] smallestPrimeFactor ; private long factorialPrimeFactorSum ( int n ) { long sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int j = i ; while ( j > 1 ) { int p = smallestPrimeFactor [ j ] ; sum += p ; j /= p ; } } return sum ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT N = 20000000 NEW_LINE K = 15000000 NEW_LINE smallestprimefactor = eulerlib . list_smallest_prime_factors ( N ) NEW_LINE def factorial_prime_factor_sum ( n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT j = i NEW_LINE while j > 1 : NEW_LINE INDENT p = smallestprimefactor [ j ] NEW_LINE result += p NEW_LINE j //= p NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT ans = factorial_prime_factor_sum ( N ) - factorial_prime_factor_sum ( K ) - factorial_prime_factor_sum ( N - K ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40266 | import java . util . Arrays ; public final class p225 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p225 ( ) . run ( ) ) ; } private static final int INDEX = 124 ; public String run ( ) { int count = 0 ; for ( int i = 1 ; ; i += 2 ) { if ( ! hasTribonacciMultiple ( i ) ) { count ++ ; if ( count == INDEX ) return Integer . toString ( i ) ; } } } private static boolean hasTribonacciMultiple ( int modulus ) { int [ ] slow = { 1 , 1 , 1 } ; int [ ] fast = slow . clone ( ) ; for ( boolean head = true ; ; head = false ) { if ( slow [ 0 ] % modulus == 0 ) return true ; if ( ! head && Arrays . equals ( slow , fast ) ) return false ; tribonacci ( slow , modulus ) ; tribonacci ( fast , modulus ) ; tribonacci ( fast , modulus ) ; } } private static void tribonacci ( int [ ] state , int mod ) { int a = state [ 0 ] ; int b = state [ 1 ] ; int c = state [ 2 ] ; state [ 0 ] = b ; state [ 1 ] = c ; state [ 2 ] = ( a + b + c ) % mod ; } }
| import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT INDEX = 124 NEW_LINE stream = ( i for i in itertools . count ( 1 , 2 ) if not has_tribonacci_multiple ( i ) ) NEW_LINE ans = next ( itertools . islice ( stream , INDEX - 1 , None ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def has_tribonacci_multiple ( i ) : NEW_LINE INDENT seen = set ( ) NEW_LINE a , b , c = 1 , 1 , 1 NEW_LINE while True : NEW_LINE INDENT key = ( a , b , c ) NEW_LINE if key in seen : NEW_LINE INDENT return False NEW_LINE DEDENT seen . add ( key ) NEW_LINE if a % i == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT a , b , c = b , c , ( a + b + c ) % i NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40267 | import java . util . ArrayList ; import java . util . Arrays ; import java . util . Collections ; import java . util . HashMap ; import java . util . List ; import java . util . Map ; public final class p151 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p151 ( ) . run ( ) ) ; } public String run ( ) { List < Integer > startState = Arrays . asList ( 1 ) ; return String . format ( " % .6f " , getExpectedSingles ( startState ) - 2 ) ; } private Map < List < Integer > , Double > expectedSingles = new HashMap < > ( ) ; private double getExpectedSingles ( List < Integer > state ) { if ( expectedSingles . containsKey ( state ) ) return expectedSingles . get ( state ) ; double result = 0 ; if ( ! state . isEmpty ( ) ) { for ( int i = 0 ; i < state . size ( ) ; i ++ ) { List < Integer > newState = new ArrayList < > ( state ) ; int sheet = state . get ( i ) ; newState . remove ( i ) ; for ( int j = sheet + 1 ; j <= 5 ; j ++ ) newState . add ( j ) ; Collections . sort ( newState ) ; result += getExpectedSingles ( newState ) ; } result /= state . size ( ) ; if ( state . size ( ) == 1 ) result ++ ; } expectedSingles . put ( state , result ) ; return result ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT ans = get_expected_singles ( ( 1 , ) ) - 2 NEW_LINE return f " { ans : .6f } " NEW_LINE DEDENT @ eulerlib . memoize NEW_LINE def get_expected_singles ( state ) : NEW_LINE INDENT result = 0.0 NEW_LINE if len ( state ) > 0 : NEW_LINE INDENT for i in range ( len ( state ) ) : NEW_LINE INDENT tempstate = list ( state ) NEW_LINE sheet = state [ i ] NEW_LINE del tempstate [ i ] NEW_LINE for j in range ( sheet + 1 , 6 ) : NEW_LINE INDENT tempstate . append ( j ) NEW_LINE DEDENT tempstate . sort ( ) NEW_LINE result += get_expected_singles ( tuple ( tempstate ) ) NEW_LINE DEDENT result /= len ( state ) NEW_LINE if len ( state ) == 1 : NEW_LINE INDENT result += 1.0 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40268 | import java . util . Arrays ; public final class p187 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p187 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 8 ) - 1 ; public String run ( ) { int count = 0 ; int [ ] primes = Library . listPrimes ( LIMIT / 2 ) ; for ( int i = 0 , sqrt = Library . sqrt ( LIMIT ) ; i < primes . length && primes [ i ] <= sqrt ; i ++ ) { int end = Arrays . binarySearch ( primes , LIMIT / primes [ i ] ) ; if ( end >= 0 ) end ++ ; else end = - end - 1 ; count += end - i ; } return Integer . toString ( count ) ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 8 - 1 NEW_LINE ans = 0 NEW_LINE primes = eulerlib . list_primes ( LIMIT // 2 ) NEW_LINE sqrt = eulerlib . sqrt ( LIMIT ) NEW_LINE for ( i , p ) in enumerate ( primes ) : NEW_LINE INDENT if p > sqrt : NEW_LINE INDENT break NEW_LINE DEDENT end = binary_search ( primes , LIMIT // p ) NEW_LINE ans += ( end + 1 if end >= 0 else - end - 1 ) - i NEW_LINE DEDENT return str ( ans ) NEW_LINE DEDENT def binary_search ( lst , x ) : NEW_LINE INDENT start = 0 NEW_LINE end = len ( lst ) NEW_LINE while start < end : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE if x < lst [ mid ] : NEW_LINE INDENT end = mid NEW_LINE DEDENT elif x > lst [ mid ] : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT elif x == lst [ mid ] : NEW_LINE INDENT return mid NEW_LINE DEDENT else : NEW_LINE INDENT raise AssertionError ( ) NEW_LINE DEDENT DEDENT return - start - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40269 | import java . math . BigInteger ; public final class p133 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p133 ( ) . run ( ) ) ; } public String run ( ) { long sum = 0 ; for ( int p : Library . listPrimes ( 100000 ) ) { if ( p == 2 || p == 5 || ! hasDivisibleRepunit ( p ) ) sum += p ; } return Long . toString ( sum ) ; } private static final BigInteger EXPONENT = BigInteger . TEN . pow ( 16 ) ; private static boolean hasDivisibleRepunit ( int p ) { return ( BigInteger . TEN . modPow ( EXPONENT , BigInteger . valueOf ( p * 9 ) ) . intValue ( ) - 1 ) / 9 % p == 0 ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT primes = eulerlib . list_primes ( 100000 ) NEW_LINE ans = sum ( p for p in primes if p == 2 or p == 5 or not has_divisible_repunit ( p ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def has_divisible_repunit ( p ) : NEW_LINE INDENT return ( pow ( 10 , 10 ** 16 , p * 9 ) - 1 ) // 9 % p == 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40270 | import java . util . PriorityQueue ; import java . util . Queue ; public final class p500 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p500 ( ) . run ( ) ) ; } private static final int TARGET = 500500 ; private static final long MODULUS = 500500507 ; public String run ( ) { Queue < Long > queue = new PriorityQueue < > ( ) ; int nextPrime = 2 ; queue . add ( ( long ) nextPrime ) ; long product = 1 ; for ( int i = 0 ; i < TARGET ; i ++ ) { long item = queue . remove ( ) ; product *= item % MODULUS ; product %= MODULUS ; queue . add ( item * item ) ; if ( item == nextPrime ) { do nextPrime ++ ; while ( ! Library . isPrime ( nextPrime ) ) ; queue . add ( ( long ) nextPrime ) ; } } return Long . toString ( product ) ; } }
| import eulerlib , heapq NEW_LINE def compute ( ) : NEW_LINE INDENT TARGET = 500500 NEW_LINE MODULUS = 500500507 NEW_LINE isprime = eulerlib . list_primality ( 7376507 ) NEW_LINE queue = [ ] NEW_LINE nextprime = 2 NEW_LINE heapq . heappush ( queue , nextprime ) NEW_LINE ans = 1 NEW_LINE for _ in range ( TARGET ) : NEW_LINE INDENT item = heapq . heappop ( queue ) NEW_LINE ans *= item NEW_LINE ans %= MODULUS NEW_LINE heapq . heappush ( queue , item ** 2 ) NEW_LINE if item == nextprime : NEW_LINE INDENT nextprime += 1 NEW_LINE while not isprime [ nextprime ] : NEW_LINE INDENT nextprime += 1 NEW_LINE DEDENT heapq . heappush ( queue , nextprime ) NEW_LINE DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40271 | public final class p429 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p429 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 8 ) ; private static final int MODULUS = 1000000009 ; public String run ( ) { int [ ] primes = Library . listPrimes ( LIMIT ) ; long sum = 1 ; for ( int p : primes ) { int power = countFactors ( LIMIT , p ) ; sum *= 1 + Library . powMod ( p , power * 2 , MODULUS ) ; sum %= MODULUS ; } return Long . toString ( sum ) ; } private static int countFactors ( int n , int p ) { if ( n == 0 ) return 0 ; else return n / p + countFactors ( n / p , p ) ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 8 NEW_LINE MOD = 1000000009 NEW_LINE ans = 1 NEW_LINE for p in eulerlib . prime_generator ( LIMIT ) : NEW_LINE INDENT power = count_factors ( LIMIT , p ) NEW_LINE ans *= 1 + pow ( p , power * 2 , MOD ) NEW_LINE ans %= MOD NEW_LINE DEDENT return str ( ans ) NEW_LINE DEDENT def count_factors ( n , p ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return n // p + count_factors ( n // p , p ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40272 | public final class p007 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p007 ( ) . run ( ) ) ; } public String run ( ) { for ( int i = 2 , count = 0 ; ; i ++ ) { if ( Library . isPrime ( i ) ) { count ++ ; if ( count == 10001 ) return Integer . toString ( i ) ; } } } }
| import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT ans = next ( itertools . islice ( filter ( eulerlib . is_prime , itertools . count ( 2 ) ) , 10000 , None ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40273 | import java . util . Arrays ; public final class p052 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p052 ( ) . run ( ) ) ; } public String run ( ) { for ( int i = 1 ; ; i ++ ) { if ( i > Integer . MAX_VALUE / 6 ) throw new ArithmeticException ( " Overflow " ) ; if ( multiplesHaveSameDigits ( i ) ) return Integer . toString ( i ) ; } } private static boolean multiplesHaveSameDigits ( int x ) { for ( int i = 2 ; i <= 6 ; i ++ ) { if ( ! Arrays . equals ( toSortedDigits ( x ) , toSortedDigits ( i * x ) ) ) return false ; } return true ; } private static char [ ] toSortedDigits ( int x ) { char [ ] result = Integer . toString ( x ) . toCharArray ( ) ; Arrays . sort ( result ) ; return result ; } }
| import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT cond = lambda i : all ( sorted ( str ( i ) ) == sorted ( str ( j * i ) ) for j in range ( 2 , 7 ) ) NEW_LINE ans = next ( i for i in itertools . count ( 1 ) if cond ( i ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40274 | public final class p043 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p043 ( ) . run ( ) ) ; } private static int [ ] DIVISIBILITY_TESTS = { 2 , 3 , 5 , 7 , 11 , 13 , 17 } ; public String run ( ) { long sum = 0 ; int [ ] digits = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; outer : do { for ( int i = 0 ; i < DIVISIBILITY_TESTS . length ; i ++ ) { if ( toInteger ( digits , i + 1 , 3 ) % DIVISIBILITY_TESTS [ i ] != 0 ) continue outer ; } sum += toInteger ( digits , 0 , digits . length ) ; } while ( Library . nextPermutation ( digits ) ) ; return Long . toString ( sum ) ; } private static long toInteger ( int [ ] digits , int off , int len ) { long result = 0 ; for ( int i = off ; i < off + len ; i ++ ) result = result * 10 + digits [ i ] ; return result ; } }
| import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT ans = sum ( int ( " " . join ( map ( str , num ) ) ) for num in itertools . permutations ( list ( range ( 10 ) ) ) if is_substring_divisible ( num ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT DIVISIBILITY_TESTS = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 ] NEW_LINE def is_substring_divisible ( num ) : NEW_LINE INDENT return all ( ( num [ i + 1 ] * 100 + num [ i + 2 ] * 10 + num [ i + 3 ] ) % p == 0 for ( i , p ) in enumerate ( DIVISIBILITY_TESTS ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40275 | import java . util . HashSet ; import java . util . Set ; public final class p075 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p075 ( ) . run ( ) ) ; } private static final int LIMIT = 1500000 ; public String run ( ) { Set < IntTriple > triples = new HashSet < > ( ) ; for ( int s = 3 ; s * s <= LIMIT ; s += 2 ) { for ( int t = s - 2 ; t > 0 ; t -= 2 ) { if ( Library . gcd ( s , t ) == 1 ) { int a = s * t ; int b = ( s * s - t * t ) / 2 ; int c = ( s * s + t * t ) / 2 ; if ( a + b + c <= LIMIT ) triples . add ( new IntTriple ( a , b , c ) ) ; } } } byte [ ] ways = new byte [ LIMIT + 1 ] ; for ( IntTriple triple : triples ) { int sum = triple . a + triple . b + triple . c ; for ( int i = sum ; i < ways . length ; i += sum ) ways [ i ] = ( byte ) Math . min ( ways [ i ] + 1 , 2 ) ; } int count = 0 ; for ( int x : ways ) { if ( x == 1 ) count ++ ; } return Integer . toString ( count ) ; } private static final class IntTriple { public final int a ; public final int b ; public final int c ; public IntTriple ( int a , int b , int c ) { this . a = a ; this . b = b ; this . c = c ; } public boolean equals ( Object obj ) { if ( ! ( obj instanceof IntTriple ) ) return false ; else { IntTriple other = ( IntTriple ) obj ; return a == other . a && b == other . b && c == other . c ; } } public int hashCode ( ) { return a + b + c ; } } }
| import eulerlib , fractions NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 1500000 NEW_LINE triples = set ( ) NEW_LINE for s in range ( 3 , eulerlib . sqrt ( LIMIT ) + 1 , 2 ) : NEW_LINE INDENT for t in range ( s - 2 , 0 , - 2 ) : NEW_LINE INDENT if fractions . gcd ( s , t ) == 1 : NEW_LINE INDENT a = s * t NEW_LINE b = ( s * s - t * t ) // 2 NEW_LINE c = ( s * s + t * t ) // 2 NEW_LINE if a + b + c <= LIMIT : NEW_LINE INDENT triples . add ( ( a , b , c ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT ways = [ 0 ] * ( LIMIT + 1 ) NEW_LINE for triple in triples : NEW_LINE INDENT sigma = sum ( triple ) NEW_LINE for i in range ( sigma , len ( ways ) , sigma ) : NEW_LINE INDENT ways [ i ] += 1 NEW_LINE DEDENT DEDENT ans = ways . count ( 1 ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40276 | public final class p205 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p205 ( ) . run ( ) ) ; } private static final int [ ] PYRAMIDAL_DIE_PDF = { 0 , 1 , 1 , 1 , 1 } ; private static final int [ ] CUBIC_DIE_PDF = { 0 , 1 , 1 , 1 , 1 , 1 , 1 } ; public String run ( ) { int [ ] ninePyramidalPdf = { 1 } ; for ( int i = 0 ; i < 9 ; i ++ ) ninePyramidalPdf = convolve ( ninePyramidalPdf , PYRAMIDAL_DIE_PDF ) ; int [ ] sixCubicPdf = { 1 } ; for ( int i = 0 ; i < 6 ; i ++ ) sixCubicPdf = convolve ( sixCubicPdf , CUBIC_DIE_PDF ) ; long numer = 0 ; for ( int i = 0 ; i < ninePyramidalPdf . length ; i ++ ) numer += ( long ) ninePyramidalPdf [ i ] * sum ( sixCubicPdf , 0 , i ) ; long denom = ( long ) sum ( ninePyramidalPdf , 0 , ninePyramidalPdf . length ) * sum ( sixCubicPdf , 0 , sixCubicPdf . length ) ; return String . format ( " % .7f " , ( double ) numer / denom ) ; } private static int [ ] convolve ( int [ ] a , int [ ] b ) { int [ ] c = new int [ a . length + b . length - 1 ] ; for ( int i = 0 ; i < a . length ; i ++ ) { for ( int j = 0 ; j < b . length ; j ++ ) c [ i + j ] += a [ i ] * b [ j ] ; } return c ; } private static int sum ( int [ ] array , int start , int end ) { int sum = 0 ; for ( int i = start ; i < end ; i ++ ) sum += array [ i ] ; return sum ; } }
| def compute ( ) : NEW_LINE INDENT nine_pyramidal_pdf = [ 1 ] NEW_LINE PYRAMIDAL_DIE_PDF = [ 0 , 1 , 1 , 1 , 1 ] NEW_LINE for i in range ( 9 ) : NEW_LINE INDENT nine_pyramidal_pdf = convolve ( nine_pyramidal_pdf , PYRAMIDAL_DIE_PDF ) NEW_LINE DEDENT six_cubic_pdf = [ 1 ] NEW_LINE CUBIC_DIE_PDF = [ 0 , 1 , 1 , 1 , 1 , 1 , 1 ] NEW_LINE for i in range ( 6 ) : NEW_LINE INDENT six_cubic_pdf = convolve ( six_cubic_pdf , CUBIC_DIE_PDF ) NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( len ( nine_pyramidal_pdf ) ) : NEW_LINE INDENT ans += nine_pyramidal_pdf [ i ] * sum ( six_cubic_pdf [ : i ] ) NEW_LINE DEDENT ans = float ( ans ) / ( sum ( nine_pyramidal_pdf ) * sum ( six_cubic_pdf ) ) NEW_LINE return f " { ans : .7f } " NEW_LINE DEDENT def convolve ( a , b ) : NEW_LINE INDENT c = [ 0 ] * ( len ( a ) + len ( b ) - 1 ) NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT for j in range ( len ( b ) ) : NEW_LINE INDENT c [ i + j ] += a [ i ] * b [ j ] NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40277 | public final class p348 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p348 ( ) . run ( ) ) ; } private static final int TARGET_WAYS = 4 ; private static final int TARGET_COUNT = 5 ; static { assert 0 <= TARGET_COUNT ; assert 0 <= TARGET_WAYS && TARGET_WAYS <= Byte . MAX_VALUE - 1 ; } public String run ( ) { for ( long limit = 1 ; ; limit *= 10 ) { if ( limit > Integer . MAX_VALUE ) throw new AssertionError ( " Overflow " ) ; long answer = trySearch ( ( int ) limit ) ; if ( answer != - 1 ) return Long . toString ( answer ) ; } } private static long trySearch ( int limit ) { byte [ ] ways = new byte [ limit ] ; for ( int i = cbrt ( limit - 1 ) ; i > 1 ; i -- ) { int cube = i * i * i ; for ( int j = Library . sqrt ( limit - 1 - cube ) ; j > 1 ; j -- ) { int index = cube + j * j ; ways [ index ] = ( byte ) Math . min ( ways [ index ] + 1 , TARGET_WAYS + 1 ) ; } } long result = 0 ; int count = 0 ; for ( int i = 0 ; i < ways . length ; i ++ ) { if ( ways [ i ] == TARGET_WAYS && Library . isPalindrome ( i ) ) { result += i ; count ++ ; if ( count == TARGET_COUNT ) return result ; } } return - 1 ; } private static int cbrt ( int x ) { if ( x < 0 ) throw new IllegalArgumentException ( " Not β implemented " ) ; int y = 0 ; for ( int i = 1 << 10 ; i != 0 ; i >>>= 1 ) { y |= i ; if ( y > 1290 || y * y * y > x ) y ^= i ; } return y ; } }
| import eulerlib , itertools NEW_LINE TARGET_WAYS = 4 NEW_LINE TARGET_COUNT = 5 NEW_LINE def compute ( ) : NEW_LINE INDENT for i in itertools . count ( ) : NEW_LINE INDENT limit = 10 ** i NEW_LINE ans = try_search ( limit ) NEW_LINE if ans is not None : NEW_LINE INDENT return str ( ans ) NEW_LINE DEDENT DEDENT DEDENT def try_search ( limit ) : NEW_LINE INDENT ways = { } NEW_LINE for i in itertools . count ( 2 ) : NEW_LINE INDENT cube = i ** 3 NEW_LINE if cube >= limit : NEW_LINE INDENT break NEW_LINE DEDENT for j in range ( 2 , eulerlib . sqrt ( limit - 1 - cube ) + 1 ) : NEW_LINE INDENT index = cube + j ** 2 NEW_LINE ways [ index ] = ways . get ( index , 0 ) + 1 NEW_LINE DEDENT DEDENT result = 0 NEW_LINE count = 0 NEW_LINE for i in sorted ( ways . keys ( ) ) : NEW_LINE INDENT if ways [ i ] == TARGET_WAYS and is_palindrome ( i ) : NEW_LINE INDENT result += i NEW_LINE count += 1 NEW_LINE if count == TARGET_COUNT : NEW_LINE INDENT return result NEW_LINE DEDENT DEDENT DEDENT return None NEW_LINE DEDENT def is_palindrome ( x ) : NEW_LINE INDENT s = str ( x ) NEW_LINE return s == s [ : : - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40278 | public final class p078 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p078 ( ) . run ( ) ) ; } private static final int MODULUS = Library . pow ( 10 , 6 ) ; public String run ( ) { for ( int limit = 1 ; ; limit *= 2 ) { int result = search ( limit ) ; if ( result != - 1 ) return Integer . toString ( result ) ; } } private static int search ( int limit ) { int [ ] partitions = new int [ limit ] ; partitions [ 0 ] = 1 ; for ( int i = 1 ; i < limit ; i ++ ) { for ( int j = i ; j < limit ; j ++ ) partitions [ j ] = ( partitions [ j ] + partitions [ j - i ] ) % MODULUS ; } for ( int i = 0 ; i < limit ; i ++ ) { if ( partitions [ i ] == 0 ) return i ; } return - 1 ; } }
| import itertools NEW_LINE MODULUS = 10 ** 6 NEW_LINE def compute ( ) : NEW_LINE INDENT partitions = [ 1 ] NEW_LINE for i in itertools . count ( len ( partitions ) ) : NEW_LINE INDENT item = 0 NEW_LINE for j in itertools . count ( 1 ) : NEW_LINE INDENT sign = - 1 if j % 2 == 0 else + 1 NEW_LINE index = ( j * j * 3 - j ) // 2 NEW_LINE if index > i : NEW_LINE INDENT break NEW_LINE DEDENT item += partitions [ i - index ] * sign NEW_LINE index += j NEW_LINE if index > i : NEW_LINE INDENT break NEW_LINE DEDENT item += partitions [ i - index ] * sign NEW_LINE item %= MODULUS NEW_LINE DEDENT if item == 0 : NEW_LINE INDENT return str ( i ) NEW_LINE DEDENT partitions . append ( item ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40279 | public final class p173 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p173 ( ) . run ( ) ) ; } private static final int TILES = 1000000 ; public String run ( ) { int count = 0 ; for ( int n = 3 ; n <= TILES / 4 + 1 ; n ++ ) { for ( int k = n - 2 ; k >= 1 ; k -= 2 ) { if ( ( long ) n * n - ( long ) k * k > TILES ) break ; count ++ ; } } return Integer . toString ( count ) ; } }
| def compute ( ) : NEW_LINE INDENT TILES = 10 ** 6 NEW_LINE ans = 0 NEW_LINE for n in range ( 3 , TILES // 4 + 2 ) : NEW_LINE INDENT for k in range ( n - 2 , 0 , - 2 ) : NEW_LINE INDENT if n * n - k * k > TILES : NEW_LINE INDENT break NEW_LINE DEDENT ans += 1 NEW_LINE DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40280 | import java . util . HashMap ; import java . util . Map ; public final class p026 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p026 ( ) . run ( ) ) ; } public String run ( ) { int bestNumber = 0 ; int bestLength = 0 ; for ( int i = 1 ; i <= 1000 ; i ++ ) { int len = getCycleLength ( i ) ; if ( len > bestLength ) { bestNumber = i ; bestLength = len ; } } return Integer . toString ( bestNumber ) ; } private static int getCycleLength ( int n ) { Map < Integer , Integer > stateToIter = new HashMap < > ( ) ; int state = 1 ; for ( int iter = 0 ; ; iter ++ ) { if ( stateToIter . containsKey ( state ) ) return iter - stateToIter . get ( state ) ; else { stateToIter . put ( state , iter ) ; state = state * 10 % n ; } } } }
| import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT ans = max ( range ( 1 , 1000 ) , key = reciprocal_cycle_len ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def reciprocal_cycle_len ( n ) : NEW_LINE INDENT seen = { } NEW_LINE x = 1 NEW_LINE for i in itertools . count ( ) : NEW_LINE INDENT if x in seen : NEW_LINE INDENT return i - seen [ x ] NEW_LINE DEDENT else : NEW_LINE INDENT seen [ x ] = i NEW_LINE x = x * 10 % n NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40281 | public final class p135 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p135 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 6 ) ; public String run ( ) { int [ ] solutions = new int [ LIMIT ] ; for ( int m = 1 ; m < LIMIT * 2 ; m ++ ) { for ( int k = m / 5 + 1 ; k * 2 < m ; k ++ ) { long temp = ( long ) ( m - k ) * ( k * 5 - m ) ; if ( temp >= solutions . length ) break ; solutions [ ( int ) temp ] ++ ; } } int count = 0 ; for ( int x : solutions ) { if ( x == 10 ) count ++ ; } return Integer . toString ( count ) ; } }
| def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 6 NEW_LINE solutions = [ 0 ] * LIMIT NEW_LINE for m in range ( 1 , LIMIT * 2 ) : NEW_LINE INDENT for k in range ( m // 5 + 1 , ( m + 1 ) // 2 ) : NEW_LINE INDENT temp = ( m - k ) * ( k * 5 - m ) NEW_LINE if temp >= LIMIT : NEW_LINE INDENT break NEW_LINE DEDENT solutions [ temp ] += 1 NEW_LINE DEDENT DEDENT ans = solutions . count ( 10 ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40282 | public final class p139 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p139 ( ) . run ( ) ) ; } private static final int LIMIT = 100000000 ; public String run ( ) { int count = 0 ; for ( int s = 3 ; s * s / 2 < LIMIT ; s += 2 ) { for ( int t = 1 ; t < s ; t += 2 ) { int a = s * t ; int b = ( s * s - t * t ) / 2 ; int c = ( s * s + t * t ) / 2 ; int p = a + b + c ; if ( p >= LIMIT ) break ; if ( c % ( a - b ) == 0 && Library . gcd ( s , t ) == 1 ) count += ( LIMIT - 1 ) / p ; } } return Integer . toString ( count ) ; } }
| import eulerlib , fractions NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 100000000 NEW_LINE ans = 0 NEW_LINE for s in range ( 3 , eulerlib . sqrt ( LIMIT * 2 ) , 2 ) : NEW_LINE INDENT for t in range ( 1 , s , 2 ) : NEW_LINE INDENT a = s * t NEW_LINE b = ( s * s - t * t ) // 2 NEW_LINE c = ( s * s + t * t ) // 2 NEW_LINE p = a + b + c NEW_LINE if p >= LIMIT : NEW_LINE INDENT break NEW_LINE DEDENT if c % ( a - b ) == 0 and fractions . gcd ( s , t ) == 1 : NEW_LINE INDENT ans += ( LIMIT - 1 ) // p NEW_LINE DEDENT DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40283 | public final class p073 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p073 ( ) . run ( ) ) ; } public String run ( ) { return Integer . toString ( sternBrocotCount ( 1 , 3 , 1 , 2 ) ) ; } private static int sternBrocotCount ( int leftN , int leftD , int rightN , int rightD ) { int n = leftN + rightN ; int d = leftD + rightD ; if ( d > 12000 ) return 0 ; else return 1 + sternBrocotCount ( leftN , leftD , n , d ) + sternBrocotCount ( n , d , rightN , rightD ) ; } }
| def compute ( ) : NEW_LINE INDENT ans = 0 NEW_LINE stack = [ ( 1 , 3 , 1 , 2 ) ] NEW_LINE while len ( stack ) > 0 : NEW_LINE INDENT leftn , leftd , rightn , rightd = stack . pop ( ) NEW_LINE d = leftd + rightd NEW_LINE if d <= 12000 : NEW_LINE INDENT n = leftn + rightn NEW_LINE ans += 1 NEW_LINE stack . append ( ( n , d , rightn , rightd ) ) NEW_LINE stack . append ( ( leftn , leftd , n , d ) ) NEW_LINE DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40284 | import java . math . BigInteger ; import java . util . Arrays ; import java . util . HashMap ; import java . util . Map ; public final class p062 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p062 ( ) . run ( ) ) ; } public String run ( ) { int numDigits = 0 ; Map < String , Integer > lowest = new HashMap < > ( ) ; Map < String , Integer > counts = new HashMap < > ( ) ; for ( int i = 0 ; ; i ++ ) { String numClass = getCubeNumberClass ( i ) ; if ( numClass . length ( ) > numDigits ) { int min = Integer . MAX_VALUE ; for ( String nc : counts . keySet ( ) ) { if ( counts . get ( nc ) == 5 ) min = Math . min ( lowest . get ( nc ) , min ) ; } if ( min != Integer . MAX_VALUE ) return cube ( min ) . toString ( ) ; lowest . clear ( ) ; counts . clear ( ) ; numDigits = numClass . length ( ) ; } if ( ! lowest . containsKey ( numClass ) ) { lowest . put ( numClass , i ) ; counts . put ( numClass , 0 ) ; } counts . put ( numClass , counts . get ( numClass ) + 1 ) ; } } private static String getCubeNumberClass ( int x ) { char [ ] digits = cube ( x ) . toString ( ) . toCharArray ( ) ; Arrays . sort ( digits ) ; return new String ( digits ) ; } private static BigInteger cube ( int x ) { return BigInteger . valueOf ( x ) . pow ( 3 ) ; } }
| import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT numdigits = 0 NEW_LINE data = { } NEW_LINE for i in itertools . count ( ) : NEW_LINE INDENT digits = [ int ( c ) for c in str ( i ** 3 ) ] NEW_LINE digits . sort ( ) NEW_LINE numclass = " " . join ( str ( d ) for d in digits ) NEW_LINE if len ( numclass ) > numdigits : NEW_LINE INDENT candidates = [ lowest for ( lowest , count ) in data . values ( ) if count == 5 ] NEW_LINE if len ( candidates ) > 0 : NEW_LINE INDENT return str ( min ( candidates ) ** 3 ) NEW_LINE DEDENT data = { } NEW_LINE numdigits = len ( numclass ) NEW_LINE DEDENT lowest , count = data . get ( numclass , ( i , 0 ) ) NEW_LINE data [ numclass ] = ( lowest , count + 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40285 | public final class p214 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p214 ( ) . run ( ) ) ; } private static final int LIMIT = 40000000 ; public String run ( ) { int [ ] totient = Library . listTotients ( LIMIT - 1 ) ; int [ ] totientChainLength = new int [ totient . length ] ; totientChainLength [ 0 ] = 0 ; long sum = 0 ; for ( int i = 1 ; i < totient . length ; i ++ ) { int chainlen = totientChainLength [ totient [ i ] ] + 1 ; totientChainLength [ i ] = chainlen ; if ( chainlen == 25 && totient [ i ] == i - 1 ) sum += i ; } return Long . toString ( sum ) ; } }
| import array NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 40000000 NEW_LINE totient = list_totients ( LIMIT - 1 ) NEW_LINE totientchainlen = array . array ( " L " , [ 0 , 1 ] ) NEW_LINE ans = 0 NEW_LINE for i in range ( len ( totientchainlen ) , len ( totient ) ) : NEW_LINE INDENT chainlen = totientchainlen [ totient [ i ] ] + 1 NEW_LINE totientchainlen . append ( chainlen ) NEW_LINE if chainlen == 25 and totient [ i ] == i - 1 : NEW_LINE INDENT ans += i NEW_LINE DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT def list_totients ( n ) : NEW_LINE INDENT assert n < ( 1 << 32 ) NEW_LINE result = array . array ( " L " , range ( n + 1 ) ) NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if result [ i ] == i : NEW_LINE INDENT for j in range ( i , n + 1 , i ) : NEW_LINE INDENT result [ j ] = result [ j ] // i * ( i - 1 ) NEW_LINE DEDENT DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40286 | public final class p041 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p041 ( ) . run ( ) ) ; } public String run ( ) { for ( int n = 9 ; n >= 1 ; n -- ) { int [ ] digits = new int [ n ] ; for ( int i = 0 ; i < digits . length ; i ++ ) digits [ i ] = i + 1 ; int result = - 1 ; do { if ( Library . isPrime ( toInteger ( digits ) ) ) result = toInteger ( digits ) ; } while ( Library . nextPermutation ( digits ) ) ; if ( result != - 1 ) return Integer . toString ( result ) ; } throw new RuntimeException ( " Not β found " ) ; } private static int toInteger ( int [ ] digits ) { int result = 0 ; for ( int x : digits ) result = result * 10 + x ; return result ; } }
| import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT for n in reversed ( range ( 2 , 10 ) ) : NEW_LINE INDENT arr = list ( reversed ( range ( 1 , n + 1 ) ) ) NEW_LINE while True : NEW_LINE INDENT if arr [ - 1 ] not in NONPRIME_LAST_DIGITS : NEW_LINE INDENT n = int ( " " . join ( str ( x ) for x in arr ) ) NEW_LINE if eulerlib . is_prime ( n ) : NEW_LINE INDENT return str ( n ) NEW_LINE DEDENT DEDENT if not prev_permutation ( arr ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT raise AssertionError ( ) NEW_LINE DEDENT NONPRIME_LAST_DIGITS = { 0 , 2 , 4 , 5 , 6 , 8 } NEW_LINE def prev_permutation ( arr ) : NEW_LINE INDENT i = len ( arr ) - 1 NEW_LINE while i > 0 and arr [ i - 1 ] <= arr [ i ] : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT if i <= 0 : NEW_LINE INDENT return False NEW_LINE DEDENT j = len ( arr ) - 1 NEW_LINE while arr [ j ] >= arr [ i - 1 ] : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT arr [ i - 1 ] , arr [ j ] = arr [ j ] , arr [ i - 1 ] NEW_LINE arr [ i : ] = arr [ len ( arr ) - 1 : i - 1 : - 1 ] NEW_LINE return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40287 | public final class p019 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p019 ( ) . run ( ) ) ; } public String run ( ) { int count = 0 ; for ( int y = 1901 ; y <= 2000 ; y ++ ) { for ( int m = 1 ; m <= 12 ; m ++ ) { if ( dayOfWeek ( y , m , 1 ) == 0 ) count ++ ; } } return Integer . toString ( count ) ; } private static int dayOfWeek ( int year , int month , int day ) { if ( year < 0 || year > 10000 || month < 1 || month > 12 || day < 1 || day > 31 ) throw new IllegalArgumentException ( ) ; int m = ( month - 3 + 4800 ) % 4800 ; int y = ( year + m / 12 ) % 400 ; m %= 12 ; return ( y + y / 4 - y / 100 + ( 13 * m + 2 ) / 5 + day + 2 ) % 7 ; } }
| import datetime NEW_LINE def compute ( ) : NEW_LINE INDENT ans = sum ( 1 for y in range ( 1901 , 2001 ) for m in range ( 1 , 13 ) if datetime . date ( y , m , 1 ) . weekday ( ) == 6 ) NEW_LINE return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40288 | import java . util . HashSet ; import java . util . Set ; public final class p095 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p095 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 6 ) ; public String run ( ) { int [ ] divisorSum = new int [ LIMIT + 1 ] ; for ( int i = 1 ; i <= LIMIT ; i ++ ) { for ( int j = i * 2 ; j <= LIMIT ; j += i ) divisorSum [ j ] += i ; } int maxChainLen = 0 ; int minChainElem = - 1 ; for ( int i = 0 ; i <= LIMIT ; i ++ ) { Set < Integer > visited = new HashSet < > ( ) ; for ( int count = 1 , cur = i ; ; count ++ ) { visited . add ( cur ) ; int next = divisorSum [ cur ] ; if ( next == i ) { if ( count > maxChainLen ) { minChainElem = i ; maxChainLen = count ; } break ; } else if ( next > LIMIT || visited . contains ( next ) ) break ; else cur = next ; } } return Integer . toString ( minChainElem ) ; } }
| import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 6 NEW_LINE divisorsum = [ 0 ] * ( LIMIT + 1 ) NEW_LINE for i in range ( 1 , LIMIT + 1 ) : NEW_LINE INDENT for j in range ( i * 2 , LIMIT + 1 , i ) : NEW_LINE INDENT divisorsum [ j ] += i NEW_LINE DEDENT DEDENT maxchainlen = 0 NEW_LINE ans = - 1 NEW_LINE for i in range ( LIMIT + 1 ) : NEW_LINE INDENT visited = set ( ) NEW_LINE cur = i NEW_LINE for count in itertools . count ( 1 ) : NEW_LINE INDENT visited . add ( cur ) NEW_LINE next = divisorsum [ cur ] NEW_LINE if next == i : NEW_LINE INDENT if count > maxchainlen : NEW_LINE INDENT ans = i NEW_LINE maxchainlen = count NEW_LINE DEDENT break NEW_LINE DEDENT elif next > LIMIT or next in visited : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT cur = next NEW_LINE DEDENT DEDENT DEDENT return str ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT
|
T40289 | 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
|
T40290 | 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
|
T40291 | 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
|
T40292 | 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
|
T40293 | 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
|
T40294 | 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
|
T40295 | 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
|
T40296 | 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
|
T40297 | 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
|
T40298 | 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
|
T40299 | 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
|