2025-12-07 22:38:53 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2025 Nicolás Ortega Froysa <nicolas@ortegas.org>
|
|
|
|
|
* Author: Nicolás Ortega Froysa <nicolas@ortegas.org>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Work on a segment.
|
|
|
|
|
*
|
|
|
|
|
* @param start:usize Beginning of the segment (inclusive).
|
2025-12-10 11:46:22 +01:00
|
|
|
* @param end:usize End of the segment (exclusive).
|
2025-12-07 22:38:53 +01:00
|
|
|
*
|
|
|
|
|
* @return List of primes found in segment.
|
|
|
|
|
*/
|
2025-12-10 11:46:22 +01:00
|
|
|
pub fn work_segment(known_primes:&Vec<u64>, start:usize, end:usize) -> Vec<u64> {
|
|
|
|
|
let mut sieve = vec![true; end - start];
|
|
|
|
|
let mut found_primes = Vec::new();
|
|
|
|
|
|
|
|
|
|
for p in known_primes {
|
|
|
|
|
let prime = *p as usize;
|
2025-12-10 13:12:49 +01:00
|
|
|
let modu = start % prime;
|
|
|
|
|
let mut mult = if modu == 0 {
|
|
|
|
|
start
|
|
|
|
|
} else {
|
|
|
|
|
start + prime - modu
|
|
|
|
|
};
|
2025-12-10 11:46:22 +01:00
|
|
|
while mult < end {
|
2025-12-10 13:12:49 +01:00
|
|
|
sieve[mult - start] = false;
|
2025-12-10 11:46:22 +01:00
|
|
|
mult += prime;
|
2025-12-07 22:38:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-10 11:46:22 +01:00
|
|
|
for i in 0..(end - start) {
|
|
|
|
|
if sieve[i] {
|
|
|
|
|
let prime = i + start;
|
|
|
|
|
found_primes.push(prime as u64);
|
2025-12-07 22:38:53 +01:00
|
|
|
|
2025-12-10 11:46:22 +01:00
|
|
|
let mut mult = prime * prime;
|
|
|
|
|
while mult < end {
|
|
|
|
|
sieve[mult - start] = false;
|
|
|
|
|
mult += prime;
|
|
|
|
|
}
|
2025-12-07 22:38:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
found_primes
|
|
|
|
|
}
|