промежуточный
The provided Python script is designed to find prime numbers that satisfy a specific condition, referred to as 'Mirror 10', and that also lie on a particular arithmetic progression, or 'Ray 45'. It employs a segmented Sieve of Eratosthenes for efficient prime number generation, combined with Numba for performance optimization.
Let's break down the key components:
#### 1. `is_mirror_10(n)` function:
* **Purpose:** This function checks if a given integer `n` is a 'Mirror 10' number. A number is considered 'Mirror 10' if the sum of its digits equals 10, 19, 28, 37, 46, 55, 64, 73, 82, or 91.
* **How it works:**
* It initializes a sum `s` to 0.
* It iteratively extracts the last digit of `n` using the modulo operator (`% 10`) and adds it to `s`.
* It then removes the last digit from `n` by integer division (`//= 10`).
* This process continues until `n` becomes 0, at which point `s` holds the sum of all digits of the original number.
* Finally, it checks if `s` is one of the predefined 'Mirror 10' sums and returns `True` or `False` accordingly.
#### 2. `calculate_ray_fixed(limit, step, start_point)` function:
* **Purpose:** This is the main engine that finds prime numbers up to a `limit` that also fall into an arithmetic sequence (a 'ray') defined by a `step` and a `start_point`. It then filters these primes using the `is_mirror_10` condition.
* **How it works:**
* **Numba Optimization:** The `@njit(fastmath=True)` decorator from the Numba library compiles this Python function into highly optimized machine code, significantly speeding up its execution, especially for numerical operations.
* **Segmented Sieve of Eratosthenes:** To find primes efficiently up to a large `limit`, it uses a segmented Sieve.
* **Base Sieve:** First, it generates all prime numbers up to the square root of `limit` using a standard Sieve of Eratosthenes. These 'small primes' are used to mark multiples in larger segments.
* **Segmentation:** The main range `(0, limit)` is divided into smaller `seg_size` (1 MB) segments. This prevents memory issues when dealing with very large limits.
* For each segment, a boolean array `seg_sieve` is created, initially marking all numbers as potentially prime.
* It then iterates through the `small_primes`. For each `p`, it marks all its multiples within the current segment as not prime.
* **Ray Filtering:**
* It calculates the `first_n` which is the first number of the 'ray' (defined by `start_point` and `step`) that falls within the current segment.
* It then iterates through numbers `n` in the segment, starting from `first_n` and incrementing by `step`.
* For each `n`, it checks two conditions:
1. `seg_sieve[n - low]`: Is `n` prime according to the segmented sieve?
2. `is_mirror_10(n)`: Does `n` satisfy the 'Mirror 10' condition?
* If both conditions are true, `n` is added to the `results` array.
* **Pre-allocation of Results:** The `results` array is pre-allocated with a generous size (`int(limit * 0.01)`) to avoid frequent reallocations, which can be slow.
* Finally, it returns only the filled portion of the `results` array.
#### In summary:
The script efficiently identifies prime numbers within a given range that adhere to both an arithmetic progression ('Ray 45') and a specific digit sum criterion ('Mirror 10') by combining optimized prime generation with custom number property checks.
Свидетельство о публикации №126032402740