Skip to content

Commit

Permalink
Merge pull request #8 from FranzDiebold/feat/add-solution-for-problem-47
Browse files Browse the repository at this point in the history
Add solution for problem 47.
  • Loading branch information
FranzDiebold authored Jun 19, 2020
2 parents 3f7f3a1 + a3023e2 commit 388d4c3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [Project Euler](https://projecteuler.net) Solutions

[![solved: 46 problems](https://img.shields.io/badge/solved-46_problems-f93.svg)](./src)
[![solved: 47 problems](https://img.shields.io/badge/solved-47_problems-f93.svg)](./src)
![Python: 3.8](https://img.shields.io/badge/Python-3.8-3776ab.svg)
[![Lint and Test](https://github.com/FranzDiebold/project-euler-solutions/workflows/Lint%20and%20Test/badge.svg)](https://github.com/FranzDiebold/project-euler-solutions/actions?query=workflow%3A%22Lint+and+Test%22)
[![license: MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg)](./LICENSE.md)
Expand Down
54 changes: 54 additions & 0 deletions src/p047_distinct_primes_factors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Problem 47: Distinct primes factors
https://projecteuler.net/problem=47
The first two consecutive numbers to have two distinct prime factors are:
14 = 2 x 7
15 = 3 x 5
The first three consecutive numbers to have three distinct prime factors are:
644 = 2^2 x 7 x 23
645 = 3 x 5 x 43
646 = 2 x 17 x 19.
Find the first four consecutive integers to have four distinct prime factors each.
What is the first of these numbers?
"""

from typing import Iterable

from src.common.primes import get_prime_factors_map


def get_distinct_prime_factor_integers(number_of_consecutive_integers: int) -> Iterable[int]:
"""Get `number_of_consecutive_integers` consecutive integers
which have `number_of_consecutive_integers` distinct prime factors each."""
consecutive_integers = []

current_number = 1
while True:
number_of_prime_factors = len(get_prime_factors_map(current_number).keys())

if number_of_prime_factors == number_of_consecutive_integers:
consecutive_integers.append(current_number)

if len(consecutive_integers) == number_of_consecutive_integers:
return consecutive_integers
else:
consecutive_integers = []

current_number += 1


def main() -> None:
"""Main function."""
number_of_consecutive_integers = 4
consecutive_integers = get_distinct_prime_factor_integers(number_of_consecutive_integers)
print(f'The first four consecutive integers to have four distinct prime factors each ' \
f'are {consecutive_integers}.')


if __name__ == '__main__':
main()
34 changes: 34 additions & 0 deletions test/test_p047_distinct_primes_factors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Problem 47: Distinct primes factors
https://projecteuler.net/problem=47
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 = 2^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?
"""

import pytest


@pytest.mark.parametrize('test_input_number_of_consecutive_integers,expected_result', [
(2, [14, 15]),
(3, [644, 645, 646])
])
def test_get_distinct_prime_factor_integers(test_input_number_of_consecutive_integers, expected_result):
# arrange
from src.p047_distinct_primes_factors import get_distinct_prime_factor_integers

# act
actual_result_iter = get_distinct_prime_factor_integers(test_input_number_of_consecutive_integers)

# assert
assert list(actual_result_iter) == expected_result

0 comments on commit 388d4c3

Please sign in to comment.