Skip to content

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

Closed
wants to merge 3 commits into from

Conversation

AmanDeep-21
Copy link

@AmanDeep-21 AmanDeep-21 commented Oct 29, 2020

Description of Change

Created a program to check a number is a perfect square or not

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Added documentation so that the program is self-explanatory and educational - Doxygen guidelines
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

@Marinovsky
Copy link
Contributor

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.

@Panquesito7 Panquesito7 added the enhancement New feature or request label Oct 29, 2020
Copy link
Member

@Panquesito7 Panquesito7 left a 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

@Panquesito7 Panquesito7 added the requested changes changes have been requested label Oct 29, 2020
@AmanDeep-21
Copy link
Author

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.

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.

@Marinovsky
Copy link
Contributor

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.

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.

@Panquesito7 Panquesito7 changed the title added a program to check a number is perfect square or not feat: added a program to check a number is perfect square or not Oct 30, 2020
@AmanDeep-21
Copy link
Author

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.

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

Copy link
Member

@Panquesito7 Panquesito7 left a 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

Comment on lines +7 to +8
#include <cassert> // for assert
#include <iostream> // for io operations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
/**
* @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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}
} // namespace check_perfect_square
} // namespace math

* @returns void
*/
static void test1(){
int64_t test_case_1 = check_perfect_square(-4);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test();
test(); // execute the tests

@stale
Copy link

stale bot commented Jul 8, 2021

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.

@stale stale bot added the stale Author has not responded to the comments for over 2 weeks label Jul 8, 2021
@stale
Copy link

stale bot commented Jul 21, 2021

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!

@stale stale bot closed this Jul 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request requested changes changes have been requested stale Author has not responded to the comments for over 2 weeks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants