primes.b 709 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* Prime number generator, run as:
  2. **
  3. ** bc primes.b
  4. **
  5. ** requires gnu bc
  6. */
  7. ignore = scale(0);
  8. define primes (low, high) {
  9. auto p, i;
  10. if (low < 5) low = 5;
  11. if (low % 2 == 0) low = low - 1;
  12. if (high % 2 == 0) high = high + 1;
  13. print "\nPrimes from ", low, " to ", high, "\n";
  14. for (p=low; p <= high; p += 2) {
  15. isprime = 1;
  16. for (i = 2; i < (p/2); i++) {
  17. if ((p % i) == 0) {
  18. isprime = 0;
  19. break;
  20. }
  21. }
  22. if (isprime) print "\t", p, "\n";
  23. }
  24. }
  25. print "\nSpecify bottom of range (eg 20): ";
  26. bot = read();
  27. print "Specify top of range (eg 90): ";
  28. top = read();
  29. ignore = primes(bot, top);
  30. quit