Home Project Euler Teaching Projects Gallery

Problem 47: Goldbach's other conjecture

IThe first two consecutive numbers to have two distinct prime factors are:

                    14 = 2 × 7
                    15 = 3 × 5
                

The first three consecutive numbers to have three distinct prime factors are:

                    644 = 2² × 7 × 23
                    645 = 3 × 5 × 43
                    646 = 2 × 17 × 19.
                

Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?


Algorthim:

This is rather simple question. For every composite, we can either loop through all possible primes upt to the odd composite number. For example, if we want to find 51, then we would loop all the primes up to 51 which is 2, 3, 5, 7 ... 47 and check for each prime if there exists a perfect square after subtracting this prime.

OR we can loop through all possible squares up to this odd composite. So for 51, it would up to 72 and check if after subtracting this square, it leaves a prime. I chose this option!

Code:

Result:

Answer: 5777
Runtime: 3.6 seconds