diff --git a/Project-Euler/Problem035.js b/Project-Euler/Problem035.js new file mode 100644 index 0000000000..b62a8f0312 --- /dev/null +++ b/Project-Euler/Problem035.js @@ -0,0 +1,34 @@ +/** + * Problem 35 - Circular primes + * + * @see {@link https://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? + * + * @author ddaniel27 + */ +import { sieveOfEratosthenes } from '../Maths/SieveOfEratosthenesIntArray' + +function problem35 (n) { + if (n < 2) { + throw new Error('Invalid input') + } + const list = sieveOfEratosthenes(n).filter(prime => !prime.toString().match(/[024568]/)) // Get a list of primes without 0, 2, 4, 5, 6, 8 + + const result = list.filter((number, _idx, arr) => { + const str = String(number) + for (let i = 0; i < str.length; i++) { // Get all rotations of the number + const rotation = str.slice(i) + str.slice(0, i) + if (!arr.includes(Number(rotation))) { // Check if the rotation is prime + return false + } + } + return true // If all rotations are prime, then the number is circular prime + }) + + return result.length + 1 // Add 2 to the result because 2 is a circular prime +} + +export { problem35 } diff --git a/Project-Euler/test/Problem035.test.js b/Project-Euler/test/Problem035.test.js new file mode 100644 index 0000000000..ebaa4ac46f --- /dev/null +++ b/Project-Euler/test/Problem035.test.js @@ -0,0 +1,18 @@ +import { problem35 } from '../Problem035.js' + +describe('checking circular primes', () => { + it('should be invalid input if number is negative', () => { + expect(() => problem35(-3)).toThrowError('Invalid input') + }) + it('should be invalid input if number is 0', () => { + expect(() => problem35(0)).toThrowError('Invalid input') + }) + // Project Euler Condition Check + test('if the number is equal to 100 result should be 13', () => { + expect(problem35(100)).toBe(13) + }) + // Project Euler Challenge Check + test('if the number is equal to one million result should be 55', () => { + expect(problem35(1000000)).toBe(55) + }) +})
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: