Index: recipes/list_primes.py |
diff --git a/recipes/list_primes.py b/recipes/list_primes.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6a93434b41f048734c1250b6229761c699db5256 |
--- /dev/null |
+++ b/recipes/list_primes.py |
@@ -0,0 +1,50 @@ |
+from recipe_engine.recipe_api import Property |
+from recipe_engine import config |
+import math |
+ |
+# This recipe computes all the primes up to the value up_to. |
+ |
+DEPS = [ |
+ 'recipe_engine/properties', |
+] |
+ |
+PROPERTIES = { |
+ 'up_to': Property(kind=int, default=50), |
+} |
+ |
+RETURN_SCHEMA = config.ReturnSchema( |
+ result=config.List(int) |
+) |
+ |
+def is_prime(val): |
+ if val % 2 == 0: |
+ return False |
+ for div in range(3, math.ceil(math.sqrt(val))): |
+ if val % div == 0: |
+ return False |
+ return True |
+ |
+def RunSteps(api, up_to): |
+ all_primes = [] |
+ prime = 2 |
+ while prime < up_to: |
+ all_primes.append(prime) |
+ res = api.depend_on('next_prime', {'curr_prime': prime}) |
+ prime = res.result |
+ if not is_prime(prime): |
+ raise api.StepFailure("OH NOES") |
+ |
+ return RETURN_SCHEMA(result=all_primes) |
+ |
+def GenTests(api): |
+ yield ( |
+ api.test('basic') + |
+ api.properties(up_to=3) + |
+ api.depend_on('next_prime', {'result': 3}) |
+ ) |
+ |
+ yield ( |
+ api.test('failure') + |
+ api.properties(up_to=10) + |
+ api.depend_on('next_prime', {'result': 4}) |
+ ) |