Problem 47 : Distinct primes factors
The 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 = 22 × 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?
C++ source code
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <iterator>
using namespace std;
class PE0047
{
private:
static const int m_max = 1000000; // one million
bool *m_primesSieve;
int m_primesArray[m_max/10];
void createPrimeSieve();
bool checkPrime(int number) const { return m_primesSieve[number];}
bool findAllPrimeFactors(int number);
public:
PE0047();
~PE0047() { delete [] m_primesSieve; }
int findFirstOfFourConsecutiveIntegers();
};
PE0047::PE0047()
{
m_primesSieve = new bool [m_max+1];
createPrimeSieve();
int i = 0;
for (int number=2; number<m_max; number++)
{
if(true == checkPrime(number))
{
m_primesArray[i++] = number;
}
}
}
// create a prime Sieve of Eratosthenes below max value
void PE0047::createPrimeSieve()
{
memset(m_primesSieve, true, (m_max+1)*sizeof(bool));
m_primesSieve[0] = m_primesSieve[1] = false;
for (int i=2; i<=(int)sqrt((double)m_max); i++)
{
if (true == m_primesSieve[i])
{
for (int j=i*i; j<m_max; j+=i)
{
m_primesSieve[j] = false;
}
}
}
}
bool PE0047::findAllPrimeFactors(int number)
{
map<int, int> primeFactors_mp;
int prime_factor;
int i= 0;
while (number > 1)
{
prime_factor = m_primesArray[i];
while(number % prime_factor == 0)
{
primeFactors_mp[prime_factor]++;
number /= prime_factor;
}
i++;
}
map<int, int>::iterator iter;
int numOfPrimeFactors = 0;
for (iter = primeFactors_mp.begin();iter != primeFactors_mp.end(); iter++)
{
if (primeFactors_mp[iter->first])
{
numOfPrimeFactors++;
}
}
if (4 == numOfPrimeFactors)
{
return true;
}
return false;
}
int PE0047::findFirstOfFourConsecutiveIntegers()
{
for(int n=2*3*5*7; n<m_max; n++)
{
if ((false == checkPrime(n)) &&
(true == findAllPrimeFactors(n)) &&
(true == findAllPrimeFactors(n+1)) &&
(true == findAllPrimeFactors(n+2)) &&
(true == findAllPrimeFactors(n+3)))
{
return n;
}
}
return 0;
}
int main()
{
PE0047 pe0047;
int n = pe0047.findFirstOfFourConsecutiveIntegers();
cout << "The first of first four consecutive integers is " << n << endl;
return 0;
}
Python source code
def createPrimeSieve(n):
"""
create prime sieve
"""
sieve = [True] * n
sieve[0] = sieve[1] = False
for i in range(2, int(n**0.5)+1):
if True == sieve[i]:
for j in range(i*i, n, i):
sieve[j] = False
return sieve
def findAllPrimeFactors(number, primes_list):
"""
find all prime factors.
If number of prime factors is 4, return True, otherwise, return False
"""
i, primeFactors_dict = 0, {}
while number > 1:
prime = primes_list[i]
while number % prime == 0:
if prime not in primeFactors_dict.keys():
primeFactors_dict[prime] = 1
else:
primeFactors_dict[prime] += 1
number /= prime;
i += 1
numOfPrimeFactors = len(primeFactors_dict)
if 4 == numOfPrimeFactors:
return True
return False
def findFirstOfFourConsecutiveIntegers():
"""
find the first four consecutive integers which have four distinct
prime factors each from 2*3*5*7 to one million
"""
max_n = 10**6
prime_sieve = createPrimeSieve(max_n)
primes_list = [ n for n in range(2, max_n) if prime_sieve[n] == True ]
for n in range(2*3*5*7, max_n):
if False == prime_sieve[n] and \
True == findAllPrimeFactors(n, primes_list) and \
True == findAllPrimeFactors(n+1, primes_list) and \
True == findAllPrimeFactors(n+2, primes_list) and \
True == findAllPrimeFactors(n+3, primes_list):
return n
return 0
def main():
print("The first number of the first four consecutive integers ")
print("which have four distinct prime factors each is",\
findFirstOfFourConsecutiveIntegers())
if __name__ == '__main__':
main()