inline int multiply(int x, int y, int counter) { if (y <= 0) return 0; if (y == 1) return x; if (y & 0x1) return x + multiply(x, y - 1, 0); if (counter + counter <= y) return multiply(x + x, y >> 1, counter + counter); else return x + multiply(x, y - counter, 0); } int squared(int number) { return multiply(number, number, 0); }
int sqauring (int num)- inline int multiply(int x, int y, int counter)
- {
int times ;times = num ;int squared ;squared = 0;for (int i = 0 ; i <times ; i++ ){squared = squared + num ;}return squared ;}- if (y <= 0)
- return 0;
- if (y == 1)
- return x;
- if (y & 0x1)
- return x + multiply(x, y - 1, 0);
- if (counter + counter <= y)
- return multiply(x + x, y >> 1, counter + counter);
- else
- return x + multiply(x, y - counter, 0);
- }
- int squared(int number)
- {
- return multiply(number, number, 0);
- }
Describe(squared_tests) { It(squared0_is0) { Assert::That(squared(0), Equals(0)); } It(squared1_is1) { Assert::That(squared(1), Equals(1)); } It(squared2_is4) { Assert::That(squared(2), Equals(4)); } It(squared3_is9) { Assert::That(squared(3), Equals(9)); } It(squared4_is16) { Assert::That(squared(4), Equals(16)); } It(squared10_is100) { Assert::That(squared(10), Equals(100)); } It(squared15_is225) { Assert::That(squared(15), Equals(225)); } It(squared21_is441) { Assert::That(squared(21), Equals(441)); } It(squared101_is10201) { Assert::That(squared(101), Equals(10201)); } };
// TODO: Replace examples and use TDD development by writing your own testsDescribe(any_group_name_you_want)- Describe(squared_tests)
- {
It(should_do_something)- It(squared0_is0)
- {
Assert::That(3, Equals(9));Assert::That (4, Equals(16));Assert::That(5, Equals(25));- Assert::That(squared(0), Equals(0));
- }
};- It(squared1_is1)
- {
- Assert::That(squared(1), Equals(1));
- }
- It(squared2_is4)
- {
- Assert::That(squared(2), Equals(4));
- }
- It(squared3_is9)
- {
- Assert::That(squared(3), Equals(9));
- }
- It(squared4_is16)
- {
- Assert::That(squared(4), Equals(16));
- }
- It(squared10_is100)
- {
- Assert::That(squared(10), Equals(100));
- }
- It(squared15_is225)
- {
- Assert::That(squared(15), Equals(225));
- }
- It(squared21_is441)
- {
- Assert::That(squared(21), Equals(441));
- }
- It(squared101_is10201)
- {
- Assert::That(squared(101), Equals(10201));
- }
- };