Posts

Showing posts from February, 2020

Sub-string divisibility | Project Euler | Problem #43

URL to the problem page:  https://projecteuler.net/problem=43 The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property. Let d 1  be the 1 st  digit, d 2  be the 2 n d  digit, and so on. In this way, we note the following: d 2 d 3 d 4 =406 is divisible by 2 d 3 d 4 d 5 =063 is divisible by 3 d 4 d 5 d 6 =635 is divisible by 5 d 5 d 6 d 7 =357 is divisible by 7 d 6 d 7 d 8 =572 is divisible by 11 d 7 d 8 d9=728 is divisible by 13 d 8 d 9 d 1 0 =289 is divisible by 17 Find the sum of all 0 to 9 pandigital numbers with this property. #include   <iostream> using   namespace   std ; long   long   power ( long   long   a ,  long   long   b ) {      long   long  result =  1 ;      for  ( long   long  i =  0 ; i < b; i++) {         result *= a;     }      return  result; } int   main () {      long   long  i, j, a, b, c, cnt, number, 

Concealed Square | Project Euler | Problem #206

URL to the problem page:  https://projecteuler.net/problem=206 Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, where each “_” is a single digit. #include   <iostream> using   namespace   std ; long   long   int   power ( long   long   int   a ,  long   long   int   b ) {      long   long   int  result =  1 ;      for  ( int  i =  0 ; i < b; i++) {         result *= a;     }      return  result; } int   main () {      long   long   int  i, j,  digits [ 19 ] = {  0  }, a, cnt, number;      for  (i =  1000000000 ; i <=  10000000000 ; i +=  10 ) {         number = i * i;         cnt =  1 ;          while  (number >=  10 ) {             number /=  10 ;             cnt++;         }          if  (cnt !=  19 ) {              continue ;         }         number = i * i;         cnt =  0 ;         a =  1 ;          for  (j =  19 ; j >  0 ; j--) {              digits [j -  1 ] = (number %  power ( 10 , j)) / ( power ( 10 , (j -  1 )));  

Square digit chains | Project Euler | Problem #92

URL to the problem page:  https://projecteuler.net/problem=92 A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before. For example, 44 → 32 → 13 → 10 → 1 → 1 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. What is most amazing is that EVERY starting number will eventually arrive at 1 or 89. How many starting numbers below ten million will arrive at 89? #include   <iostream> using   namespace   std ; int   finddigits ( int   a ) {      int  cnt =  1 ;      while  (a >=  10 ) {         a /=  10 ;         cnt++;     }      return  cnt; } int   power ( int   a ,  int   b ) {      int  result =  1 ;      for  ( int  i =  0 ; i < b; i++) {         result *= a;     }      return  result; } int   main () {      int  i, j, number, cnt =  0 , digitnumber,  digits [ 8 ] = {  0  };      for  (i =  1 ; i <

Goldbach's other conjecture | Project Euler | Problem #46

URL to the problem page:  https://projecteuler.net/problem=46 It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×1 2 15 = 7 + 2×2 2 21 = 3 + 2×3 2 25 = 7 + 2×3 2 27 = 19 + 2×2 2 33 = 31 + 2×1 2 It turns out that the conjecture was false. What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? #include   <iostream> using   namespace   std ; int   power ( int   a ,  int   b ) {      int  result =  1 ;      for  ( int  i =  0 ; i < b; i++) {         result *= a;     }      return  result; } int   main () {      int  i, j, a, b, n, cnt, cnt2 =  0 , result =  0 ;      for  (i =  3 ; i <  10000 ; i +=  2 ) {         cnt =  0 ;          for  (j =  2 ; j <=  sqrt (i); j++) {              if  (i % j ==  0 ) {                 cnt++;                  break ;             }         }          if  (cnt !=  0 ) {             cnt2++;     

Truncatable primes | Project Euler | Problem #37

URL to the problem page:  https://projecteuler.net/problem=37 The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. #include   <iostream> using   namespace   std ; int   power ( int   a ,  int   b ) {      int  result =  1 ;      for  ( int  i =  0 ; i < b; i++) {         result = result * a;     }      return  result; } int   finddigits ( int   a ) {      int  cnt =  1 ;      while  (a >=  10 ) {         a /=  10 ;         cnt++;     }      return  cnt; } int   main () {      int  i, j, a, b, n, digitnumber, cnt, cnt2 =  0 , cnt3, cnt4, cnt5, cnt6, result =  0 ;      for  (i =  22 ; cnt2 <  11 ; i+
My photo
Ercan Tomac
instagram.com/ercantomac