Posts

Triangular, pentagonal, and hexagonal | Project Euler | Problem #45

URL to the problem page:  https://projecteuler.net/problem=45 Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: Triangle T ₙ =n(n+1)/2     1, 3, 6, 10, 15, ... Pentagonal P ₙ =n(3n−1)/2     1, 5, 12, 22, 35, ... Hexagonal H ₙ =n(2n−1)     1, 6, 15, 28, 45, ... It can be verified that T ₂₈₅  = P ₁₆₅  = H ₁₄₃  = 40755. Find the next triangle number that is also pentagonal and hexagonal. #include   <iostream> using   namespace   std ; int   main () {      long   long  i, j, a, result =  0 , triangular, pentagonal, hexagonal;      for  (i =  286 ; result <=  0 ; i++) {         triangular = (i * (i +  1 )) /  2 ;      ...

Digit factorials | Project Euler | Problem #34

URL to the problem page:  https://projecteuler.net/problem=34 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: as 1! = 1 and 2! = 2 are not sums they are not included. #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 = result * a;     }      return  result; } long   long   int   finddigits ( long   long   int   a ) {      long   long   int ...

1000-digit Fibonacci number | Project Euler | Problem #25

URL to the problem page:  https://projecteuler.net/problem=25 The Fibonacci sequence is defined by the recurrence relation: F ₙ  = F ₙ₋₁  + F ₙ₋₂ , where F₁ = 1 and F₂ = 1. Hence the first 12 terms will be: F₁ = 1 F₂ = 1 F₃ = 2 F ₄  = 3 F₅ = 5 F₆ = 8 F₇ = 13 F₈ = 21 F₉ = 34 F₁ ₀  = 55 F₁₁ = 89 F₁₂ = 144 The 12th term, F₁₂, is the first term to contain three digits. What is the index of the first term in the Fibonacci sequence to contain 1000 digits? #include   <iostream> using   namespace   std ; int   main () {      const   int  k =  1000 ;      int   number [ 3 ][k] = {  0  }, carry, i, counter =  2 ;      number [ 2 ][k -  1 ] =  number [ 1 ][k -  1 ] =  1 ;      while  ( number [ 0 ][ 0 ] ==  ...

Factorial digit sum | Project Euler | Problem #20

URL to the problem page:  https://projecteuler.net/problem=20 n! means n × (n − 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. Find the sum of the digits in the number 100! #include   <iostream> using   namespace   std ; int   main () {      const   int  k =  158 ;      int   number [k] = {  0  },  result [ 3 ][k] = {  0  }, carry, carry2, carry3, sum =  0 , i, a,  multiply [ 3 ] = {  0 ,  0 ,  2  };      number [k -  1 ] =  1 ;      for  (a =  1 ; a <  100 ; a++) {         carry = carry2 = carry3...

Power digit sum | Project Euler | Problem #16

URL to the problem page:  https://projecteuler.net/problem=16 2¹⁵ = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 2¹⁰⁰⁰? #include   <iostream> using   namespace   std ; int   main () {      const   int  k =  504 ;      int   number [k] = {  0  },  result [k] = {  0  }, carry, sum =  0 , i, a;      number [k -  1 ] =  2 ;      for  (a =  1 ; a <  1000 ; a++) {         carry =  0 ;          for  (i = k -  1 ; i >=  0 ; i--) {              result [i] =...

C++ ile basit XOX oyunu (Konsol Uygulaması)

Bu yazıda, konsol uygulaması olarak çalışan, C++ dilinde yazılmış ve 160 satırlık koddan oluşan 2 kişilik basit bir XOX oyunu ele alınacaktır. Koda belirli aralıklar ile yorum satırları eklenerek hangi kısımların ne amaçla yazıldığı açıklanmıştır.  Aşağıda bu açıklamaların üstünden biraz daha detaylı bir şekilde geçilecektir. Sayfanın sonunda .cpp dosyasını indirmek için ilgili link yer almaktadır. Uygulama main fonksiyonu haricinde 6 adet fonksiyon içermektedir. Bunlar: Programın temel değişkenlerine başlangıç değeri ataması yapan "hazirla" fonksiyonu. (Bir nevi Classlar'daki "Constructor" fonksiyonu.) Her çağrıldığında mevcut ekranı temizleyen ve oyunun durumuna göre ekranı baştan çizen "ekranciz" fonksiyonu. Oyunculardan girdi almayı sağlayan, alınan girdinin gerekli kriterlere uygunluğunu kontrol eden, uygun olmaması durumunda hata mesajı gösterip tekrar girilmesini isteyen, uygun olması durumunda ise ilgili değişkenlerin değerlerin...
My photo
Ercan Tomac
instagram.com/ercantomac