Cube Root
- Nada program
- Test 1
- Test 2
- Test 3
- Test 4
src/cube_root.py
"""Implementation of the cube root function using the Newton method
with n iterations - set to 41 by default."""
from nada_dsl import *
import time
n_iterations = 41
def nada_main():
party1 = Party(name="Party1")
num = SecretInteger(Input(name="num", party=party1))
guess = num
for _ in range(n_iterations):
div1 = guess
div2 = num / guess
div2 = div2 / guess
guess = Integer(2) * div1 + div2
guess = guess / Integer(3)
return [Output(guess, "my_output", party1)]
if __name__ == "__main__":
nada_main()
tests/cube_root_test1.yaml
---
program: cube_root
inputs:
num: 127324
expected_outputs:
my_output: 50
tests/cube_root_test2.yaml
---
program: cube_root
inputs:
num: 27
expected_outputs:
my_output: 3
tests/cube_root_test3.yaml
---
program: cube_root
inputs:
num: 734523
expected_outputs:
my_output: 90
tests/cube_root_test4.yaml
---
program: cube_root
inputs:
num: 3245
expected_outputs:
my_output: 14
Run and test the cube_root program
1. Open "Nada by Example"
2. Run the program with inputs
from the test file
nada run cube_root_test1
3. Test the program with inputs
from the test file against the expected_outputs
from the test file
nada test cube_root_test1