From 6beb3d14fd8466589f074def1d4899885551a31c Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 24 Jun 2024 10:55:50 +1000 Subject: [PATCH] Add examples parallel_sum and sequential_sum --- examples/parallel_sum.bend | 17 +++++++++++++++++ examples/sequential_sum.bend | 14 ++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 examples/parallel_sum.bend create mode 100644 examples/sequential_sum.bend diff --git a/examples/parallel_sum.bend b/examples/parallel_sum.bend new file mode 100644 index 000000000..a4160ec1f --- /dev/null +++ b/examples/parallel_sum.bend @@ -0,0 +1,17 @@ +# Defines the function Sum with two parameters: start and target +def Sum(start, target): + if start == target: + # If the value of start is the same as target, returns start. + return start + else: + # If start is not equal to target, calculate the midpoint (half), + # then recursively call Sum on both halves. + half = (start + target) / 2 + left = Sum(start, half) # (Start -> Half) + right = Sum(half + 1, target) + return left + right + +# A parallelizable sum of numbers from 1 to 1000000 +def main(): + # This translates to (((1 + 2) + (3 + 4)) + ... (999999 + 1000000)...) + return Sum(1, 1_000_000) \ No newline at end of file diff --git a/examples/sequential_sum.bend b/examples/sequential_sum.bend new file mode 100644 index 000000000..65d563d70 --- /dev/null +++ b/examples/sequential_sum.bend @@ -0,0 +1,14 @@ +# Defines the function Sum with two parameters: start and target +def Sum(start, target): + if start == target: + # If the value of start is the same as target, returns start. + return start + else: + # If start is not equal to target, recursively call Sum with + # start incremented by 1, and add the result to start. + return start + Sum(start + 1, target) + +def main(): + # This translates to (1 + (2 + (3 + (...... + (999999 + 1000000))))) + # Note that this will overflow the maximum value of a number in Bend + return Sum(1, 1_000_000) \ No newline at end of file