prime_numbers.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_PRIME_NUMBERS_H
  3. #define __LINUX_PRIME_NUMBERS_H
  4. #include <linux/types.h>
  5. bool is_prime_number(unsigned long x);
  6. unsigned long next_prime_number(unsigned long x);
  7. /**
  8. * for_each_prime_number - iterate over each prime upto a value
  9. * @prime: the current prime number in this iteration
  10. * @max: the upper limit
  11. *
  12. * Starting from the first prime number 2 iterate over each prime number up to
  13. * the @max value. On each iteration, @prime is set to the current prime number.
  14. * @max should be less than ULONG_MAX to ensure termination. To begin with
  15. * @prime set to 1 on the first iteration use for_each_prime_number_from()
  16. * instead.
  17. */
  18. #define for_each_prime_number(prime, max) \
  19. for_each_prime_number_from((prime), 2, (max))
  20. /**
  21. * for_each_prime_number_from - iterate over each prime upto a value
  22. * @prime: the current prime number in this iteration
  23. * @from: the initial value
  24. * @max: the upper limit
  25. *
  26. * Starting from @from iterate over each successive prime number up to the
  27. * @max value. On each iteration, @prime is set to the current prime number.
  28. * @max should be less than ULONG_MAX, and @from less than @max, to ensure
  29. * termination.
  30. */
  31. #define for_each_prime_number_from(prime, from, max) \
  32. for (prime = (from); prime <= (max); prime = next_prime_number(prime))
  33. #endif /* !__LINUX_PRIME_NUMBERS_H */