prime.cpp 530 B

1234567891011121314151617181920212223242526272829303132
  1. //-----------------------------------------------------------------------------
  2. //
  3. // Look up the nth prime
  4. //
  5. // Input: n on stack (0 < n < 10001)
  6. //
  7. // Output: nth prime on stack
  8. //
  9. //-----------------------------------------------------------------------------
  10. #include "stdafx.h"
  11. #include "defs.h"
  12. void
  13. eval_prime(void)
  14. {
  15. push(cadr(p1));
  16. eval();
  17. prime();
  18. }
  19. void
  20. prime(void)
  21. {
  22. int n;
  23. n = pop_integer();
  24. if (n < 1 || n > MAXPRIMETAB)
  25. stop("prime: Argument out of range.");
  26. n = primetab[n - 1];
  27. push_integer(n);
  28. }