Posts

Showing posts from October, 2019

Non-abundant sums | Project Euler | Problem #23

URL to the problem page:  https://projecteuler.net/problem=23 A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number. A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n. As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit. Find the sum of all the positive integers which cannot be written as the sum of two abundant ...

Amicable numbers | Project Euler | Problem #21

URL to the problem page:  https://projecteuler.net/problem=21 Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers. For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. Evaluate the sum of all the amicable numbers under 10000. #include   <iostream> using   namespace   std ; int   main () {      long   long  i, j, b, a, result =  0 , counter, counter2, sum, number;      for  (i =  1 ; i <  10000 ; i++) {         counter =  0 ; a =  0 ; sum...

Longest Collatz sequence | Project Euler | Problem #14

URL to the problem page:  https://projecteuler.net/problem=14 The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. Which starting number, under one million, produces the longest chain? NOTE: Once the chain starts the terms are allowed to go above one million. #include   <iostream> using   namespace   std ; int   main () {      long   long  i, tmp, counter, result, biggest =  0 ;      for  (i =  2 ; i <  1000000 ; i++) {   ...

Highly divisible triangular number | Project Euler | Problem #12

URL to the problem page:  https://projecteuler.net/problem=12 The sequence of triangle numbers is generated by adding the natural numbers. So the 7 t h  triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the first seven triangle numbers: 1: 1 3: 1,3 6: 1,2,3,6 10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors. What is the value of the first triangle number to have over five hundred divisors? #include   <iostream> using   namespace   std ; int   main () {      long   long  i =  1 , j, number =  0 , counter =  1 , a;      while  ( true ) {         number =  0 ;      ...

Summation of primes | Project Euler | Problem #10

URL to the problem page:  https://projecteuler.net/problem=10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. #include   <iostream> using   namespace   std ; int   main () {      long   long  sum =  0 , counter, i, j;      for  (i =  3 ; i <  2000000 ; i +=  2 ) {         counter =  0 ;          for  (j =  2 ; j <=  sqrt (i); j++) {              if  (i % j ==  0 ) {                 counter++;              ...

Special Pythagorean triplet | Project Euler | Problem #9

URL to the problem page:  https://projecteuler.net/problem=9 A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a² + b² = c² For example, 3² + 4² = 9 + 16 = 25 = 5². There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. #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 *= a;     }      return  result; } int   main () {      long   long  i, j, k, exit =  0 , exit2 =  0 ;      for  (i =  ...

Largest product in a series | Project Euler | Problem #8

URL to the problem page:  https://projecteuler.net/problem=8 The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403...

10001st prime | Project Euler | Problem #7

URL to the problem page:  https://projecteuler.net/problem=7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? #include   <iostream> using   namespace   std ; int   main () {      long   long  a =  0 , counter, i, j;      for  (i =  2 ; ; i++) {         counter =  0 ;          for  (j =  2 ; j <=  sqrt (i); j++) {              if  (i % j ==  0 ) {                 counter++;                ...

Sum square difference | Project Euler | Problem #6

URL to the problem page:  https://projecteuler.net/problem=6 The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025 Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. #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 *= a;     }      return  resul...

Smallest multiple | Project Euler | Problem #5

URL to the problem page:  https://projecteuler.net/problem=5 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? #include   <iostream> using   namespace   std ; int   main () {      long   long  j =  20 , i, result, counter;      while  ( true ) {         counter =  0 ;          for  (i =  1 ; i <  21 ; i++) {              if  (j % i !=  0 ) {                 counter++;        ...

Largest palindrome product | Project Euler | Problem #4

URL to the problem page:  https://projecteuler.net/problem=4 A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. #include   <iostream> using   namespace   std ; int   main () {      int  i, j, units, tens, hundreds, thousands, tenthousands, hundredthousands, number, result =  0 ;      for  (i =  100 ; i <  1000 ; i++) {          for  (j =  100 ; j <  1000 ; j++) {             number = i * j;             units = number %...

Largest prime factor | Project Euler | Problem #3

URL to the problem page:  https://projecteuler.net/problem=3 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? #include   <iostream> using   namespace   std ; int   main () {      long   long  number =  600851475143 , a, i, counter, j;      for  (i =  2 ; ; i++) {         a = number / i;         counter =  0 ;          if  (number % a ==  0 ) {              for  (j =  2 ; j <=  sqrt (a); j++) {                  if  (a...

Even Fibonacci numbers | Project Euler | Problem #2

URL to the problem page:  https://projecteuler.net/problem=2 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. #include   <iostream> using   namespace   std ; int   main () {      long   long  sum =  0 , f1 =  1 , f2 =  1 , number = f1 + f2;      while  (number <=  4000000 ) {         number = f1 + f2;         f1 = f2;         f2 = number;          if ...

Multiples of 3 and 5 | Project Euler | Problem #1

URL to the problem page:  https://projecteuler.net/problem=1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. #include   <iostream> using   namespace   std ; int   main () {      int  i, number =  0 ;      for  (i =  1 ; i <  1000 ; i++) {          if  (i %  3  ==  0  || i %  5  ==  0 ) {             number += i;         }     }     cout <<  "Sum of all the multiples of 3 or 5 below 1000 is ...
My photo
Ercan Tomac
instagram.com/ercantomac