Posts

Showing posts from November, 2019

Permuted multiples | Project Euler | Problem #52

URL to the problem page:  https://projecteuler.net/problem=52 It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order. Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. #include   <iostream> using   namespace   std ; long   long   power ( long   long   a ,  long   long   b ) {      long   long  result =  1 ;      for  ( int  i =  0 ; i < b; i++) {         result = result * a;     }      return  result; } long   long   finddigits ( long   long   a ) {      long   long  counter =  1 ;      while  (a >=  10 ) {         a /=  10 ;         counter++;     }      return  counter; } int   main () {      long   long   number [ 6 ], i, j, a, b, counter, counter2,  digitnumber [ 6 ],  digits [ 6 ][ 20 ];      for  (i =  1 ; i <  1000000 ; i++) {         counter =  0 ;          for  (j =  0 ; j <  6 ; j++) {              number [j] = i * (j +  1 );          

Pandigital prime | Project Euler | Problem #41

URL to the problem page:  https://projecteuler.net/problem=41 We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? #include   <iostream> using   namespace   std ; long   long   primecontrol ( long   long   a ) {      long   long  i, counter =  0 ;      for  (i =  2 ; i <=  sqrt (a); i++) {          if  (a % i ==  0 ) {             counter++;              break ;         }     }      if  (counter ==  0 ) {          return   1 ;     }      else  {          return  - 1 ;     } } long   long   finddigits ( long   long   a ) {      long   long  counter =  1 ;      while  (a >=  10 ) {         a /=  10 ;         counter++;     }      return  counter; } long   long   power ( long   long   a ,  long   long   b ) {      long   long  result =  1 ;      for  ( int  i =  0 ; i < b; i++) {         result = 

Names scores | Project Euler | Problem #22

URL to the problem page:  https://projecteuler.net/problem=22 Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score. For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714. What is the total of all the name scores in the file? #include   <iostream> #include   <cstring> #include   <fstream> using   namespace   std ; int   main () {     string word, word3;      char   name [ 5164 ][ 32 ],  name2 [ 5164 ][ 32 ],  tmp [ 32 ];      long   long  i =  0 , j =  0 , a, k, biggest, counter, sum, result =  0 ;     ifstream  names ( "names.txt" );     ofstream  names2 (

Powerful digit counts | Project Euler | Problem #63

URL to the problem page:  https://projecteuler.net/problem=63 The 5-digit number, 16807=7⁵, is also a fifth power. Similarly, the 9-digit number, 134217728=8⁹, is a ninth power. How many n-digit positive integers exist which are also an nth power? #include   <iostream> using   namespace   std ; unsigned   long   long   power ( unsigned   long   long   a ,  unsigned   long   long   b ) {      unsigned   long   long  result =  1 , i;      for  (i =  0 ; i < b; i++) {         result = result * a;     }      return  result; } unsigned   long   long   finddigits ( unsigned   long   long   a ) {      unsigned   long   long  counter =  1 ;      while  (a >=  10 ) {         a /=  10 ;         counter++;     }      return  counter; } int   main () {      unsigned   long   long  i, j, counter =  0 ;      for  (i =  0 ; i <=  20 ; i++) {          for  (j =  0 ; j <=  9 ; j++) {              if  (i ==  finddigits ( power (j, i))) {                 counter++;           

Powerful digit sum | Project Euler | Problem #56

URL to the problem page:  https://projecteuler.net/problem=56 A googol (10¹⁰⁰) is a massive number: one followed by one-hundred zeros; 100¹⁰⁰ is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1. Considering natural numbers of the form, a b , where a, b < 100, what is the maximum digital sum? #include   <iostream> using   namespace   std ; int   main () {      const   int  k =  1000 ;      int   result [ 2 ][k],  carry [ 2 ],  number2 [ 2 ] = {  0 ,  0  }, i, j, a, b, c, tmp, carry_4,  number [k], biggest =  0 ;      for  (a =  0 ; a <  99 ; a++) {          for  (j =  1 ; j >=  0 ; j--) {              if  ( number2 [j] ==  9 ) {                  number2 [j] =  0 ;             }              else   if  ( number2 [j] <  9 ) {                  number2 [j]++;                  break ;             }         }          for  (j =  0 ; j <  99 ; j++) {              for  (i =  0 ; i <

Consecutive prime sum | Project Euler | Problem #50

URL to the problem page:  https://projecteuler.net/problem=50 The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953. Which prime, below one-million, can be written as the sum of the most consecutive primes? #include   <iostream> using   namespace   std ; int   main () {      unsigned   long   long   int  i, j, a =  0 , counter, sum,  primes [ 78498 ];      for  (i =  999999 ; i >=  2 ; i--) {         counter =  0 ;          for  (j =  2 ; j <=  sqrt (i); j++) {              if  (i % j ==  0 ) {                 counter++;                  break ;             }         }          if  (counter ==  0 ) {              primes [a] = i;             a++;         }     }      for  (a =  78497 ; a >  0 ; a--) {         sum =

Self powers | Project Euler | Problem #48

URL to the problem page:  https://projecteuler.net/problem=48 The series, 1 ¹  + 2 ²  + 3 ³  + ... + 10 ¹ ⁰  = 10405071317. Find the last ten digits of the series, 1 ¹  + 2 ²  + 3 ³  + ... + 1000 ¹ ⁰ ⁰ ⁰ . #include   <iostream> using   namespace   std ; int   main () {      const   int  k =  10 ;      int   result [ 4 ][k],  carry [ 4 ],  sum [k] = {  0  },  number2 [ 4 ] = {  0 ,  0 ,  0 ,  0  }, i, j, a, b, tmp, carry_4,  number [k];      for  (a =  0 ; a <  1000 ; a++) {          for  (j =  0 ; j < k; j++) {              number [j] =  0 ;         }          for  (j =  3 ; j >=  0 ; j--) {              if  ( number2 [j] ==  9 ) {                  number2 [j] =  0 ;             }              else   if  ( number2 [j] <  9 ) {                  number2 [j]++;                  break ;             }         }          number [k -  1 ] =  number2 [ 3 ];          number [k -  2 ] =  number2 [ 2 ];          number [k -  3 ] =  number2 [ 1 ];          number [k - 
My photo
Ercan Tomac
instagram.com/ercantomac