diff --git a/examples/interest/README.md b/examples/interest/README.md index 2cc5d50cac..e431acbed8 100644 --- a/examples/interest/README.md +++ b/examples/interest/README.md @@ -1,13 +1,22 @@ -# interest.aleo +# Calculating Interest -This program calculates interest over a fixed number of periods. +This program provides utilities for calculating interest over a fixed or bounded number of periods. -We note that the Leo code is much more readable and writable than the -corresponding Aleo code, which can be seen in `build/main.aleo` after building. +## Building the Program -## Build Guide - -To compile this Aleo program, run: +To compile this program, run: ```bash -aleo build +leo build ``` + +## Running the Program + +To run this program, run: +```bash +leo run +``` +where `` is one of the following: +* `fixed_period_interest` +* `bounded_period_interest` + +Be sure to update `inputs/interest.in` with the desired inputs. diff --git a/examples/interest/inputs/interest.in b/examples/interest/inputs/interest.in index 96576291e9..9751618c35 100644 --- a/examples/interest/inputs/interest.in +++ b/examples/interest/inputs/interest.in @@ -1,4 +1,9 @@ // The program input for interest/src/main.leo -[main] +[fixed_period_interest_accrued] capital: u32 = 80u32; rate: u32 = 5u32; // 5% + +[bounded_period_interest_accrued] +capital: u32 = 80u32; +rate: u32 = 5u32; // 5% +periods: u8 = 10u8; diff --git a/examples/interest/src/main.leo b/examples/interest/src/main.leo index 3ca31a7e7a..6487f616f4 100644 --- a/examples/interest/src/main.leo +++ b/examples/interest/src/main.leo @@ -1,8 +1,26 @@ -// The 'interest' main function. -function main(capital: u32, public rate: u32) -> u32 { +// This function calculates the interest accrued over ten periods for some `capital` and `rate`. +function fixed_period_interest(capital: u32, public rate: u32) -> u32 { let amount: u32 = capital; - for i:u8 in 0u8..10u8 { // accrue for 10 periods - amount += amount * rate / 100u32; // round down + + // Accrue for exactly 10 periods. + for i:u8 in 0u8..10u8 { + // Note that the added amount is rounded down. + amount += (amount * rate) / 100u32; + } + return amount; +} + +// This function calculates the interest accrued over a variable number of periods (max 50) for some `capital` and `rate`. +function bounded_period_interest(capital: u32, public rate: u32, periods: u8) -> u32 { + console.assert(periods <= 50u8); + let amount: u32 = capital; + + // Accrue for up to 50 periods. + for i:u8 in 0u8..50u8 { + if i < periods { + // Note that the added amount is rounded down. + amount += (amount * rate) / 100u32; + } // Skip the remaining periods. } return amount; } diff --git a/examples/interest2/.gitignore b/examples/interest2/.gitignore deleted file mode 100644 index b28696155d..0000000000 --- a/examples/interest2/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -outputs/ -build/ diff --git a/examples/interest2/README.md b/examples/interest2/README.md deleted file mode 100644 index 875464cf29..0000000000 --- a/examples/interest2/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# interest2.aleo - -This program calculates interest over a fixed number of periods. - -We note that the Leo code is much more readable and writable than the -corresponding Aleo code, which can be seen in `build/main.aleo` after building. - -## Build Guide - -To compile this Aleo program, run: -```bash -aleo build -``` diff --git a/examples/interest2/inputs/interest2.in b/examples/interest2/inputs/interest2.in deleted file mode 100644 index ed83614b72..0000000000 --- a/examples/interest2/inputs/interest2.in +++ /dev/null @@ -1,5 +0,0 @@ -// The program input for interest2/src/main.leo -[main] -capital: u32 = 80u32; -rate: u32 = 5u32; // 5% -periods: u8 = 10u8; diff --git a/examples/interest2/program.json b/examples/interest2/program.json deleted file mode 100644 index 255726b0b4..0000000000 --- a/examples/interest2/program.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "program": "interest2.aleo", - "version": "0.0.0", - "description": "", - "development": { - "private_key": "APrivateKey1zkp4i2UqV6HqAn8ZHY68qrWfwuapNZ86doD3xHYfcTGYNFf", - "address": "aleo1t0t2y04rw8evnmvxzlz4xqt4ydgr9k0yk9qltfk8w0a7yt9lvv9qxnzs7w" - }, - "license": "MIT" -} diff --git a/examples/interest2/src/main.leo b/examples/interest2/src/main.leo deleted file mode 100644 index d4fbdcb857..0000000000 --- a/examples/interest2/src/main.leo +++ /dev/null @@ -1,11 +0,0 @@ -// The 'interest2' main function. -function main(capital: u32, public rate: u32, periods: u8) -> u32 { - console.assert(periods <= 50u8); - let amount: u32 = capital; - for i:u8 in 0u8..50u8 { // accrue for max 50 periods - if i < periods { - amount += amount * rate / 100u32; // round down - } // skip remaining periods - } - return amount; -}