Posts

Hamming numbers | Rosetta Code | #18

URL to the problem page:  https://rosettacode.org/wiki/Hamming_numbers Hamming numbers are numbers of the form H = 2 i  × 3 j  × 5 k where i, j, k ≥ 0 Hamming numbers are also known as ugly numbers and also 5-smooth numbers (numbers whose prime divisors are less or equal to 5). Show the first twenty Hamming numbers. Show the 1691 st  Hamming number (the last one below 2 3 1 ). #include   <iostream> using   namespace   std ; long   long   int   power ( int   a ,  int   b ) {      long   long   int  result =  1 ;      for  ( int  i =  0 ; i < b; i++) {         result *= a;     }      return  result; } int   main () {      int  cnt, a =  0 ,...

Happy numbers | Rosetta Code | #17

URL to the problem page:  http://rosettacode.org/wiki/Happy_numbers A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers. Find and print the first 8 happy numbers. #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 () {  ...

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 ...

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 <=  100000000...

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 ...

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, cnt...
My photo
Ercan Tomac
instagram.com/ercantomac