The problem
Return every prime p with lo <= p <= hi, as a list from smallest to largest.
primes_between(10, 30) # [11, 13, 17, 19, 23, 29]
primes_between(91, 91) # []: 91 = 7 * 13
primes_between(0, 2) # [2]
primes_between(30, 10) # []
Limits
hiis at most 10¹² (a million million), and the windowhi - lois at most about 2,000,000.lomay be below 2, or bigger thanhi.- A sieve up to
hineeds far too much memory, and testing each number by trial division is far too slow. The big tests have 4 seconds each.
Builds on MATH-4 The sieve of Eratosthenes.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
12 tests, not run yet
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | The example | Not run | |
| 2 | Empty window | Not run | |
| 3 | Below 2 | Not run | |
| 4 | One prime | Not run | |
| 5 | One composite | Not run | |
| 6 | A square of a prime | Not run | |
| 7 | Up to 100 | Not run | |
| 8 | Near a billion | Not run | |
| 9 | Up to two million | Not run | |
| 10 | A window below 10¹² | Not run | |
| 11 | A thin window | Not run | |
| 12 | 2 only | Not run |