Skip to content

Miller–Rabin primality test#2139

Merged
josdejong merged 5 commits into
josdejong:developfrom
Yaffle:develop
Apr 10, 2021
Merged

Miller–Rabin primality test#2139
josdejong merged 5 commits into
josdejong:developfrom
Yaffle:develop

Conversation

@Yaffle
Copy link
Copy Markdown
Contributor

@Yaffle Yaffle commented Mar 21, 2021

Comment thread src/function/utils/isPrime.js Outdated
if (n.lt('3317044064679887385961981')) {
bases = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41].filter(x => x < n)
} else {
const max = Math.min(n.toNumber() - 2, Math.floor(2 * Math.pow(n.decimalPlaces() * Math.log(10), 2)))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

For this calculation of the max (or min?), you transform Decimal into a regular number. Should these calculations be done as Decimal?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it should work with conversion to a number even when it will be converted to Infinity as the second part should be small

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Ah yes you're right, then it works just fine indeed 👍

@josdejong
Copy link
Copy Markdown
Owner

Thanks Viktor 👍 , and sorry for the late reply.

The code looks good, and it's nice that we can utilize the bignumber library that's already in the library.

For reference: #2133

@Yaffle
Copy link
Copy Markdown
Contributor Author

Yaffle commented Mar 31, 2021

I have to use some strange thing like const Decimal = n.constructor.clone({ precision: n.constructor.precision * 2 }) to work with integers with the precision to store the numbers big as square of the original exactly....
btw... this code looks wrong, probably, I should use const Decimal = n.constructor.clone({ precision: n.decimalPlaces() * 2 })

@josdejong
Copy link
Copy Markdown
Owner

I have to use some strange thing like const Decimal = n.constructor.clone({ precision: n.constructor.precision * 2 }) to work with integers with the precision to store the numbers big as square of the original exactly....
btw... this code looks wrong, probably, I should use const Decimal = n.constructor.clone({ precision: n.decimalPlaces() * 2 })

I'm not sure if decimalPlaces is what you want, that's giving the actual number of digits of the current number, not the configured precision 64 digits by default. Which of the two do you actually need here? the digits of the number, or the configured precision?

@Yaffle
Copy link
Copy Markdown
Contributor Author

Yaffle commented Mar 31, 2021

Another thing here: for small integers (< 2**26 or something) the trial division is still faster... especially if to convert from Decimal to a number... can the "BigNumber code" call the "number code" for small integers?

@Yaffle
Copy link
Copy Markdown
Contributor Author

Yaffle commented Mar 31, 2021

I have to use some strange thing like const Decimal = n.constructor.clone({ precision: n.constructor.precision * 2 }) to work with integers with the precision to store the numbers big as square of the original exactly....
btw... this code looks wrong, probably, I should use const Decimal = n.constructor.clone({ precision: n.decimalPlaces() * 2 })

I'm not sure if decimalPlaces is what you want, that's giving the actual number of digits of the current number, not the configured precision 64 digits by default. Which of the two do you actually need here? the digits of the number, or the configured precision?

the digits of the number, but if that number is always less the precision digits, then it will work as well

@josdejong
Copy link
Copy Markdown
Owner

the digits of the number, but if that number is always less the precision digits, then it will work as well

yes, an actual number cannot have more digits than the configured maximum (64 by default).

Ok then I think all is fine and can be merged, right?

@Yaffle
Copy link
Copy Markdown
Contributor Author

Yaffle commented Mar 31, 2021

@josdejong , ops... n.decimalPlaces() is the number of decimal places after the decimal comma... this needs to be fixed...
I thought, it is the number of decimal digits

@josdejong
Copy link
Copy Markdown
Owner

👍 good we're checking this, I'll wait with merging then

@josdejong
Copy link
Copy Markdown
Owner

@Yaffle I see you made a few commits, is the PR ready to be merged?

@Yaffle
Copy link
Copy Markdown
Contributor Author

Yaffle commented Apr 5, 2021

@josdejong , yes
btw, if it is based on pseudo code from Wikipedia, is there some restrictions?

@hsmyers
Copy link
Copy Markdown

hsmyers commented Apr 5, 2021 via email

@josdejong
Copy link
Copy Markdown
Owner

@josdejong , yes
btw, if it is based on pseudo code from Wikipedia, is there some restrictions?

I think you can just use the code, but should keep attribution in place (not sure if you must though). See for example https://opensource.stackexchange.com/questions/8262/proper-use-of-wikipedia-code-sample-in-open-source-projects.

You already included the link to the source to wikipedia in the PR, so I think all is fine, right?

@Yaffle
Copy link
Copy Markdown
Contributor Author

Yaffle commented Apr 7, 2021

You already included the link to the source to wikipedia in the PR, so I think all is fine, right?

@josdejong , the linking is fine

@josdejong
Copy link
Copy Markdown
Owner

Agree, merging now. Thanks Victor!

@josdejong josdejong merged commit 91141bb into josdejong:develop Apr 10, 2021
joshhansen pushed a commit to LearnSomethingTeam/mathjs that referenced this pull request Sep 16, 2021
* Miller–Rabin primality test

* add tests for some big numbers

* Update isPrime.js

* Update isPrime.js

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants