-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
feat: added a program to check a number is perfect square or not #1412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
This can be done easier by checking only whether the number modulo four equals zero or one. Indeed, this is a theorem from Number Theory that says that every perfect square has the form 4k+1 or 4k and k is an integer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please enable GitHub Actions in your repository of this fork in this link: https://github.com/AmanDeep-21/C-Plus-Plus/actions
I agree that perfect squares are of the form 4k or 4k+1 where k belongs to non-negative integers, but it does not guarantee that if a number is of the form 4k or 4k+1, then its definitely a perfect square, for instance, 13,17,21,24,28,29,32,33 all these numbers follow either 4k or 4k+1 case, but none of them is a perfect square. |
Yeah, It's true, sorry for that. |
No issues |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please enable GitHub Actions in your repository of this fork in this link: https://github.com/AmanDeep-21/C-Plus-Plus/actions
| #include <cassert> // for assert | ||
| #include <iostream> // for io operations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| #include <cassert> // for assert | |
| #include <iostream> // for io operations | |
| #include <cassert> /// for assert | |
| #include <iostream> /// for io operations |
| @@ -0,0 +1,117 @@ | |||
| /** | |||
| * @file | |||
| * @brief This program aims at checking that the given number is perfect square or not | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * @brief This program aims at checking that the given number is perfect square or not | |
| * @brief This program aims at checking that the given number is [perfect square](https://leetcode.com/problems/perfect-squares/) or not |
| #include <cassert> // for assert | ||
| #include <iostream> // for io operations | ||
|
|
||
| /** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /** | |
| /** | |
| * @namespace math | |
| * @brief Mathematical algorithms | |
| */ | |
| namespace math { | |
| /** | |
| * @namespace check_perfect_square | |
| * @brief Functions for [Perfect Square](https://leetcode.com/problems/perfect-squares/) implementation | |
| */ | |
| namespace check_perfect_square { | |
| /** |
|
|
||
| int64_t value = get_integral_square_root(num); | ||
| return value * value == num; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| } | |
| } | |
| } // namespace check_perfect_square | |
| } // namespace math |
| * @returns void | ||
| */ | ||
| static void test1(){ | ||
| int64_t test_case_1 = check_perfect_square(-4); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| int64_t test_case_1 = check_perfect_square(-4); | |
| int64_t test_case_1 = math::check_perfect_square::check_perfect_square(-4); |
| * @returns void | ||
| */ | ||
| static void test2(){ | ||
| int64_t test_case_2 = check_perfect_square(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| int64_t test_case_2 = check_perfect_square(1); | |
| int64_t test_case_2 = math::check_perfect_square::check_perfect_square(1); |
| * @returns void | ||
| */ | ||
| static void test3(){ | ||
| int64_t test_case_3 = check_perfect_square(2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| int64_t test_case_3 = check_perfect_square(2); | |
| int64_t test_case_3 = math::check_perfect_square::check_perfect_square(2); |
| * @returns void | ||
| */ | ||
| static void test4(){ | ||
| int64_t test_case_4 = check_perfect_square(4); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| int64_t test_case_4 = check_perfect_square(4); | |
| int64_t test_case_4 = math::check_perfect_square::check_perfect_square(4); |
| * @returns void | ||
| */ | ||
| static void test5(){ | ||
| int64_t test_case_5 = check_perfect_square(8); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| int64_t test_case_5 = check_perfect_square(8); | |
| int64_t test_case_5 = math::check_perfect_square::check_perfect_square(8); |
| * @returns 0 on exit | ||
| */ | ||
| int main() { | ||
| test(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| test(); | |
| test(); // execute the tests |
|
This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
|
Please ping one of the maintainers once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to ask for help in our Gitter channel. Thank you for your contributions! |
Description of Change
Created a program to check a number is a perfect square or not
Checklist
Notes: