/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_find_next_prime.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tosuman +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/30 17:03:18 by tosuman #+# #+# */ /* Updated: 2023/03/30 17:03:19 by tosuman ### ########.fr */ /* */ /* ************************************************************************** */ #define INT_MAX_SQRT 46340 #define FIRST_PRIMES_LEN 2 #define SIEVE_LEN 2 #define WHEEL 6 int ft_int_sqrt(int nb) { int sqrt; int old_sqrt; int max; int min; min = 0; max = nb; if (nb > INT_MAX_SQRT) max = INT_MAX_SQRT; sqrt = max; while (1) { old_sqrt = sqrt; if (sqrt * sqrt == nb) return (sqrt); if (sqrt * sqrt > nb) max = sqrt; if (sqrt * sqrt < nb) min = sqrt; sqrt = (max + min) / 2; if (sqrt == old_sqrt) break ; } return (sqrt); } int first_divisor(int nb) { int wheel; int idx; int int_sqrt; int first_primes[FIRST_PRIMES_LEN]; int sieve6[SIEVE_LEN]; first_primes[0] = 2; first_primes[1] = 3; sieve6[0] = 5; sieve6[1] = 7; idx = -1; while (++idx < FIRST_PRIMES_LEN) if (nb % first_primes[idx] == 0) return (first_primes[idx]); int_sqrt = ft_int_sqrt(nb); wheel = 0; while (wheel < int_sqrt) { idx = -1; while (++idx < SIEVE_LEN) if (nb % (sieve6[idx] + wheel) == 0) return (sieve6[idx] + wheel); wheel += WHEEL; } return (nb); } int ft_is_prime(int nb) { if (nb < 2) return (0); return (first_divisor(nb) == nb); } int ft_find_next_prime(int nb) { --nb; while (++nb) if (ft_is_prime(nb)) return (nb); return (2); } /* //// #include #include int main(void) { printf("Given %d, next prime is %d\n", INT_MIN, ft_find_next_prime(INT_MIN)); printf("Given %d, next prime is %d\n", -2, ft_find_next_prime(-2)); printf("Given %d, next prime is %d\n", -1, ft_find_next_prime(-1)); printf("Given %d, next prime is %d\n", 0, ft_find_next_prime(0)); printf("Given %d, next prime is %d\n", 1, ft_find_next_prime(1)); printf("Given %d, next prime is %d\n", 2, ft_find_next_prime(2)); printf("Given %d, next prime is %d\n", 3, ft_find_next_prime(3)); printf("Given %d, next prime is %d\n", 4, ft_find_next_prime(4)); printf("Given %d, next prime is %d\n", 5, ft_find_next_prime(5)); printf("Given %d, next prime is %d\n", 6, ft_find_next_prime(6)); printf("Given %d, next prime is %d\n", 7, ft_find_next_prime(7)); printf("Given %d, next prime is %d\n", 8, ft_find_next_prime(8)); printf("Given %d, next prime is %d\n", 9, ft_find_next_prime(9)); printf("Given %d, next prime is %d\n", 10, ft_find_next_prime(10)); printf("Given %d, next prime is %d\n", 11, ft_find_next_prime(11)); printf("Given %d, next prime is %d\n", 12, ft_find_next_prime(12)); printf("Given %d, next prime is %d\n", INT_MAX - 1000, ft_find_next_prime(INT_MAX - 1000)); printf("Given %d, next prime is %d\n", INT_MAX - 3, ft_find_next_prime(INT_MAX - 3)); printf("Given %d, next prime is %d\n", INT_MAX - 2, ft_find_next_prime(INT_MAX - 2)); printf("Given %d, next prime is %d\n", INT_MAX - 1, ft_find_next_prime(INT_MAX - 1)); printf("Given %d, next prime is %d\n", INT_MAX, ft_find_next_prime(INT_MAX)); return (0); } */ ////