Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added isCompositeNumber.js #1063

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
tehliang wants to merge 12 commits into TheAlgorithms:master from tehliang:master
Closed

Added isCompositeNumber.js #1063

tehliang wants to merge 12 commits into TheAlgorithms:master from tehliang:master

Conversation

Copy link
Contributor

@tehliang tehliang commented Jul 25, 2022
edited
Loading

Open in Gitpod know more

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
    Example:UserProfile.js is allowed but userprofile.js,Userprofile.js,user-Profile.js,userProfile.js are not
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@tehliang tehliang changed the title (削除) Added isCompositeNumber,js (削除ここまで) (追記) Added isCompositeNumber.js (追記ここまで) Jul 25, 2022
Copy link
Collaborator

@appgurueu appgurueu left a comment
edited
Loading

Choose a reason for hiding this comment

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

(削除) Isn't this the same as the already implemented check for "perfect numbers"? (削除ここまで)

NVM, should've read carefully.

Copy link
Contributor Author

Isn't this the same as the already implemented check for "perfect numbers"?

No, they are not the same.
From Wikipedia and the internet.
A perfect number is a positive integer equal to the sum of its positive divisors, excluding the number itself.
For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.

A composite number is a positive integer that can be formed by multiplying two smaller positive integers. Equivalently, it is a positive integer that has at least one divisor other than 1 and itself.
For example, the integer 14 is a composite number because it is the product of the two smaller integers 2 ×ばつ 7.

Example:
35 is a composite number but not a perfect number because
1 + 5 + 7 not equal to 35
while 35 has more than two factors, i.e. 1, 5, 7, 35.
Which makes 35 a composite number because 35 has more than two factors.

appgurueu reacted with thumbs up emoji

tehliang added 3 commits July 26, 2022 20:50
Updated isCompositeNumber with a different approach
Copy link
Collaborator

@appgurueu appgurueu left a comment

Choose a reason for hiding this comment

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

Looks good to me. You could perhaps deduplicate the factorization code (if the current APIs aren't too clunky to use).

tehliang reacted with thumbs up emoji
Copy link
Member

In my opinion, this is largely a duplication of the PrimeCheck function. Using the fact that all prime numbers other than 2 and 3 are in the form 6n ± 1 is definitely an improvement, but it can be made on the PrimeCheck function itself. @appgurueu?

Copy link
Collaborator

appgurueu commented Jul 27, 2022
edited
Loading

In my opinion, this is largely a duplication of the PrimeCheck function. Using the fact that all prime numbers other than 2 and 3 are in the form 6n ± 1 is definitely an improvement, but it can be made on the PrimeCheck function itself. @appgurueu?

Yes. isCompositeNumber(n) should just be !PrimeCheck(n) (with the exception of 1).

Copy link
Member

raklaptudirm commented Jul 27, 2022
edited
Loading

I think instead of duplicating the logic the 6n ± 1 improvement should be made on the PrimeCheck function itself.

appgurueu reacted with thumbs up emoji

Copy link
Contributor Author

So any suggestions on how can I improve the algorithm?

Copy link
Member

You should remove the isComposite function and instead make your changes to the PrimeCheck function.

Copy link
Contributor Author

tehliang commented Jul 28, 2022
edited
Loading

You should remove the isComposite function and instead make your changes to the PrimeCheck function.

What changes should I make? I am a bit clueless... I am just proposing an algorithm to check composite numbers.

Copy link
Collaborator

You should remove the isComposite function and instead make your changes to the PrimeCheck function.

What changes should I make? I am a bit clueless... I am just proposing an algorithm to check composite numbers.

Yes, but composite numbers are numbers that aren't prime, with the exception of one. You're thus duplicating primality sieve logic here. You should instead implement isComposite using PrimeCheck - or get rid of isComposite altogether - and improve the PrimeCheck function.

Copy link
Contributor Author

tehliang commented Jul 28, 2022
edited
Loading

You should remove the isComposite function and instead make your changes to the PrimeCheck function.

What changes should I make? I am a bit clueless... I am just proposing an algorithm to check composite numbers.

Yes, but composite numbers are numbers that aren't prime, with the exception of one. You're thus duplicating primality sieve logic here. You should instead implement isComposite using PrimeCheck - or get rid of isComposite altogether - and improve the PrimeCheck function.

Hmm, I still do not quite understand. PrimeCheck checks the prime number while the IsComposite checks the composite number. Yes, what the algo doing was reverse of each other, but the code is not the same. How can I implement PrimeCheck to check whether the number is composite using the PrimeCheck algorithm?

Copy link
Collaborator

Hmm, I still do not quite understand. PrimeCheck checks the prime number while the IsComposite checks the composite number. Yes, what the algo doing was reverse of each other, but the code is not the same. How can I implement PrimeCheck to check whether the number is composite using the PrimeCheck algorithm?

function isComposite(number) {
 return number > 1 && !PrimeCheck(number)
}

there you go.

Copy link

stale bot commented Aug 13, 2022

This issue has been automatically marked as stale 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 Closed due to age label Aug 13, 2022
@stale stale bot removed the stale Closed due to age label Aug 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@appgurueu appgurueu appgurueu left review comments

@raklaptudirm raklaptudirm raklaptudirm left review comments

Assignees
No one assigned
Labels
algorithm Adds or improves an algorithm
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /