TheAlgorithms/JavaScript · error · Error
Invalid input
Error message
Invalid input
What it means
Thrown by `problem35(n)` (Project Euler #35, circular primes) when `n < 2`. The function calls `sieveOfEratosthenes(n)` which needs at least 2 to produce primes, so smaller bounds are rejected. The message is generic. Non-number inputs bypass the guard.
Source
Thrown at Project-Euler/Problem035.js:16
/**
* 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/SieveOfEratosthenes'
function problem35(n) {
if (n < 2) {
throw new Error('Invalid input')
}
// Get a list of primes without 0, 2, 4, 5, 6, 8; this discards the circular primes 2 & 5
const list = sieveOfEratosthenes(n).filter(
(prime) => !prime.toString().match(/[024568]/)
)
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
})View on GitHub (pinned to 5c39e87a9a)
Solutions
- Validate `n >= 2` and is a finite integer upstream.
- Coerce with `Number(n)` so string/undefined inputs are caught.
- Pick a sensible default (e.g. 1000000) when the field is missing.
Example fix
// before
problem35(bound) // bound could be 1
// after
const n = Number(bound)
if (!Number.isInteger(n) || n < 2) throw new RangeError('n must be an integer >= 2')
problem35(n) Defensive patterns
Strategy: validation
Validate before calling
const n = Number(bound)
if (!Number.isInteger(n) || n < 2) {
throw new RangeError('n must be an integer >= 2')
}
problem35(n) Type guard
const isIntAtLeast2 = (x) => typeof x === 'number' && Number.isInteger(x) && x >= 2
Prevention
- Coerce with Number() so undefined/NaN do not bypass the guard.
- Pick a sensible default (e.g. 1000000) when the bound is missing.
- Validate at the boundary, not at the call site.
When it happens
Trigger: Call `problem35(0)`, `problem35(1)`, `problem35(-10)`. `n = 2` passes and the sieve returns [2] (then filtered out by the digit regex).
Common situations: Defaulting a 'primes below N' field to 0 or 1, off-by-one boundary confusion, or a request param parsed as 1.
Related errors
- Invalid Input
- No natural numbers exist below 1
- Fibonacci sequence limit can't be less than 1
- Please provide number greater that 1
- Invalid Input
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/b3f14e1378c16885.
Report an issue: GitHub.