Skip to content

Commit

Permalink
Add solution for problem 35.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzDiebold committed Oct 20, 2019
1 parent e9023dd commit 83ab650
Show file tree
Hide file tree
Showing 2 changed files with 56 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: 34 problems](https://img.shields.io/badge/solved-34_problems-f93.svg)](./src)
[![solved: 35 problems](https://img.shields.io/badge/solved-35_problems-f93.svg)](./src)
![Python: 3.7](https://img.shields.io/badge/Python-3.7-3776ab.svg)
[![Build Status](https://travis-ci.com/FranzDiebold/project-euler-solutions.svg?branch=master)](https://travis-ci.com/FranzDiebold/project-euler-solutions)
[![license: MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg)](./LICENSE.md)
Expand Down
55 changes: 55 additions & 0 deletions src/p035_circular_primes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Problem 35: Circular primes
http://projecteuler.net/problem=35
The number, 197, is called a circular prime because all rotations of the digits:
197, 971, and 719, are themselves prime.
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
How many circular primes are there below one million?
"""

from typing import Set, Iterable

from src.common.primes import is_prime


def _get_primes_set(threshold: int) -> Set:
"""Get all prime numbers up to a threshold `threshold` as set."""
return {number for number in range(2, threshold) if is_prime(number)}


def _get_circular_numbers(number: int) -> Iterable[int]:
"""Get all circular numbers for a given number `number`.
Example:
1234 -> 1234, 2341, 3412, 4123
"""
number_str = str(number)
for idx in range(len(number_str)):
yield int(number_str[idx:] + number_str[:idx])


def _get_circular_primes(threshold: int) -> Iterable[int]:
"""Get all circular primes up to a threshold `threshold`."""
primes = _get_primes_set(threshold)
for prime in primes:
is_circular_prime = True
for circular_number in _get_circular_numbers(prime):
if circular_number not in primes:
is_circular_prime = False
break
if is_circular_prime:
yield prime


def main() -> None:
"""Main function."""
threshold = int(1e6)
print(f'There are {len(list(_get_circular_primes(threshold))):,} circular primes ' \
f'below {threshold:,}.')


if __name__ == '__main__':
main()

0 comments on commit 83ab650

Please sign in to comment.