This repository has been archived on 2024-06-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
coffee.pygments/tests/examplefiles/arrow/primesieve.arw
Oleh Prypin 6f43092173
Also add auto-updatable output-based tests to examplefiles (#1689)
Co-authored-by: Georg Brandl <georg@python.org>
2021-01-20 10:48:45 +01:00

60 lines
1.2 KiB
Text

function
/--> bool[] primeSieve(int max)
| bool[] primes[max]
| primes[0] = false
| primes[1] = false
|
| int i
| i = 2
| /-->
| | primes[i] = true
| | i = i + 1
| \--< i < max
|
| int firstPrime
| firstPrime = 1
| /-->
| | bool firstIsComposite
| | /-->
| | | firstPrime = firstPrime + 1
| | | firstIsComposite = false
| | | /--< firstPrime > max or firstPrime == max
| | | | firstIsComposite = not primes[firstPrime]
| | | \-->
| | \--< firstIsComposite
| | /--< firstPrime > max or firstPrime == max
| | | primes[firstPrime] = true
| | | int composite
| | | composite = firstPrime * 2
| | | /--< composite > max
| | | | /-->
| | | | | primes[composite] = false
| | | | | composite = composite + firstPrime
| | | | \--< composite < max
| | | \-->
| | \-->
| \--< firstPrime < max - 1
^ primes
main
int max
max = 1000
bool[] primes
primes = primeSieve(max)
print "Enter a number and I'll tell you whether or not it's prime!"
int testNum
testNum = input int
/--< testNum < 0 or testNum > max - 1
| /--< not primes[testNum]
| | print "Prime"
| \-->
| /--< primes[testNum]
| | print "Not prime"
| \-->
\-->
/--< not (testNum < 0 or testNum > max - 1)
| print "Out of bounds"
\-->