BEST SOLUTION
I an unsure if I understand the concept of Time complexity: O(sqrt(n))
and Space complexity: O(1)
in this context but the
function prime(n)
is probably the fastest way (least iterations)
to calculate if a number is prime number of any size.
https://github.com/ganeshkbhat/fastprimenumbers, https://www.npmjs.com/package/fast-prime
This probably is the BEST solution in the internet as of today 11th
March 2022. Feedback and usage is welcome.
This same code can be applied in any languages like C, C++, Go Lang,
Java, .NET, Python, Rust, etc with the same logic and have performance
benefits. It is pretty fast. I have not seen this implemented before
and has been indigenously done.
If you are looking at the speed and performance here is the """BEST"""
hopeful solution I can give:
Max iterations 16666 for n == 100000 instead of 100000 of conventional
way
The codes can also be found here: https://github.com/ganeshkbhat/fastprimecalculations
If you use it for your project please spend 2 minutes of your time crediting me by letting me know by either sending me an email, or logging an Github issue with subject heading [User]
, or star
my Github project. But let me know here https://github.com/ganeshkbhat/fastprimecalculations. I would love to know the fans and users of the code logic
function prime(n) {
if ((n === 2 || n === 3 || n === 5 || n === 7)) {
return true
}
if (n === 1 || ((n > 7) && (n % 5 == 0 || n % 7 == 0 || n % 2 == 0 || n % 3 == 0))) {
return false
}
if ((Number.isInteger(((n - 1) / 6))) || (Number.isInteger((n + 1) / 6))) {
for (let i = 1; i < n; i++) {
let factorsix = (i * 6)
let five = n / (5 + factorsix), seven = n / (7 + factorsix)
if (((five > 1) && Number.isInteger(five)) || ((seven > 1) && (Number.isInteger(seven)))) {
return false;
}
if (factorsix > n) {
break;
}
}
return true
}
return false
}
Here is an analysis of all the ways of calculation:
Conventional way of checking for prime:
function isPrimeConventionalWay(n) {
count = 0
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
// Max iterations 99998 for n == 100000
for (let i = 2; i < n; i++) {
// Counting Iterations
count += 1
if (n % i == 0) {
console.log("count: Prime Conventional way", count)
return false;
}
}
console.log("count: Prime Conventional way", count)
return true;
}
SQUAREROOT way of checking for prime:
function isPrimeSquareRootWay(num) {
count = 0
// if not is_number num return false
if (num < 2) {
console.log("count: Prime Squareroot way", count)
return false
}
for (let i = 2, s = Math.sqrt(num); i <= s; i++) {
// Counting Iterations
count += 1
if (num % i === 0) {
console.log("count: Prime Squareroot way", count)
return false
}
}
console.log("count: Prime Squareroot way", count)
return true
}
SUGGESTED way of checking for prime:
function prime(n) {
let count = 0
if ((n === 2 || n === 3 || n === 5 || n === 7)) {
// console.log("count: Prime Unconventional way", count)
return true
}
if (n === 1 || ((n > 7) && (n % 5 == 0 || n % 7 == 0 || n % 2 == 0 || n % 3 == 0))) {
// console.log("count: Prime Unconventional way", count)
return false
}
if ((Number.isInteger(((n - 1) / 6))) || (Number.isInteger((n + 1) / 6))) {
for (let i = 1; i < n; i++) {
// Counting Iterations
count += 1
let factorsix = (i * 6)
let five = n / (5 + factorsix), seven = n / (7 + factorsix)
if (((five > 1) && Number.isInteger(five)) || ((seven > 1) && (Number.isInteger(seven)))) {
// console.log("count: Prime Unconventional way", count)
return false;
}
if (factorsix > n) {
// Max iterations 16666 for n == 100000 instead of 100000
break;
}
}
// console.log("count: Prime Unconventional way", count)
return true
}
// console.log("count: Prime Unconventional way", count)
return false
}
Tests to compare with the traditional way of checking for prime numbers.
function test_primecalculations() {
let count = 0, iterations = 100000;
let arr = [];
for (let i = 1; i <= iterations; i++) {
let traditional = isPrimeConventionalWay(i), newer = prime(i);
if (traditional == newer) {
count = count + 1
} else {
arr.push([traditional, newer, i])
}
}
console.log("[count, iterations, arr] list: ", count, iterations, arr)
if (count === iterations) {
return true;
}
return false;
}
console.log("Tests Passed: ", test_primecalculations())
You will see the results of count of number of iterations as below for check of prime number: 100007
:
console.log("Is Prime 100007: ", isPrimeConventionalWay(100007))
console.log("Is Prime 100007: ", isPrimeSquareRootWay(100007))
console.log("Is Prime 100007: ", prime(100007))
count: Prime Conventional way 96
Is Prime 100007: false
count: Prime Squareroot way 96
Is Prime 100007: false
count: Prime Unconventional way 15
Is Prime 100007: false
Performance Tests:
let iterations = 1000000;
let isPrimeConventionalWayArr = [], isPrimeSquarerootWayArr = [], primeArr = [], isprimeAKSWayArr = []
let performance = require('perf_hooks').performance;
// let performance = window.performance;
function tests_performance_isPrimeConventionalWayArr(){
for (let i = 1; i <= iterations; i++){
let start = performance.now()
isPrimeConventionalWay(30000239)
let end = performance.now()
isPrimeConventionalWayArr.push(end - start)
}
}
tests_performance_isPrimeConventionalWayArr()
function tests_performance_isPrimeSquarerootWayArr(){
for (let i = 1; i <= iterations; i++){
let start = performance.now()
isPrimeSquarerootWay(30000239)
let end = performance.now()
isPrimeSquarerootWayArr.push(end - start)
}
}
tests_performance_isPrimeSquarerootWayArr()
function tests_performance_primeArr(){
for (let i = 1; i <= iterations; i++){
let start = performance.now()
prime(30000239)
let end = performance.now()
primeArr.push(end - start)
}
}
tests_performance_primeArr()
function calculateAverage(array) {
var total = 0;
var count = 0;
array.forEach(function(item, index) {
total += item;
count++;
});
return total / count;
}
console.log("isPrimeConventionalWayArr: ", calculateAverage(isPrimeConventionalWayArr))
console.log("isPrimeSquarerootWayArr: ", calculateAverage(isPrimeSquarerootWayArr))
console.log("primeArr (suggested): ", calculateAverage(primeArr))
Sample 1 Million Iterations
Iteration 1:
isPrimeConventionalWayArr: 0.00011065770072489977
isPrimeSquarerootWayArr: 0.0001288754000402987
primeArr (suggested): 0.00005511959937214852
isPrimeSquarerootWayTwoArr: 0.00010504549999162554
Iteration 2:
isPrimeConventionalWayArr: 0.00011061320009082556
isPrimeSquarerootWayArr: 0.00012810260016098618
primeArr (suggested): 0.00005620509984344244
isPrimeSquarerootWayTwoArr: 0.00010411459982022643
Iteration 3:
isPrimeConventionalWayArr: 0.00010952920047193766
isPrimeSquarerootWayArr: 0.0001286292002275586
primeArr (suggested): 0.00005520999948307872
isPrimeSquarerootWayTwoArr: 0.00010410030033439397
Iteration 4:
isPrimeConventionalWayArr: 0.00011091169972717762
isPrimeSquarerootWayArr: 0.00012648080018162728
primeArr (suggested): 0.00005570890004560351
isPrimeSquarerootWayTwoArr: 0.00010492690009251237
Iteration 5:
isPrimeConventionalWayArr: 0.00010998740004003048
isPrimeSquarerootWayArr: 0.00012748069976270199
primeArr (suggested): 0.000060324400294572114
isPrimeSquarerootWayTwoArr: 0.00010445670029893518
Iteration 6:
isPrimeConventionalWayArr: 0.00011286130072548985
isPrimeSquarerootWayArr: 0.00012876759915798902
primeArr (suggested): 0.00005682649992406368
isPrimeSquarerootWayTwoArr: 0.0001073473998978734
Iteration 7:
isPrimeConventionalWayArr: 0.0001092233005464077
isPrimeSquarerootWayArr: 0.0001272089005112648
primeArr (suggested): 0.00006196610003709793
isPrimeSquarerootWayTwoArr: 0.000105714200142771
Iteration 8:
isPrimeConventionalWayArr: 0.00010890220178663731
isPrimeSquarerootWayArr: 0.00012892659988626836
primeArr (suggested): 0.000055275400444865224
isPrimeSquarerootWayTwoArr: 0.00010486920177191496
Iteration 9:
isPrimeConventionalWayArr: 0.00011153739924356342
isPrimeSquarerootWayArr: 0.00012576029987260699
primeArr (suggested): 0.00005680049995332956
isPrimeSquarerootWayTwoArr: 0.00010467480102181434
Iteration 10:
isPrimeConventionalWayArr: 0.00011035799815505743
isPrimeSquarerootWayArr: 0.0001265768006257713
primeArr (suggested): 0.00005575320014730096
isPrimeSquarerootWayTwoArr: 0.00010596680009737611
Sample 10 Million (10000000) Iterations
Iteration 1:
isPrimeConventionalWayArr: 0.00011928780986890197
isPrimeSquarerootWayArr: 0.00012412049009799956
primeArr (suggested): 0.00005793778010234237
isPrimeSquarerootWayTwoArr: 0.00010363322006165981
Iteration 2:
isPrimeConventionalWayArr: 0.00010635640006065368
isPrimeSquarerootWayArr: 0.00012505082993544638
primeArr (suggested): 0.000053171040023490784
isPrimeSquarerootWayTwoArr: 0.00010545557955764234
Iteration 3:
isPrimeConventionalWayArr: 0.00010894328009858727
isPrimeSquarerootWayArr: 0.00012658657005652784
primeArr (suggested): 0.00005493023999705911
isPrimeSquarerootWayTwoArr: 0.00010782274951003491
JSFiddle Link as example:
https://jsfiddle.net/ganeshsurfs/y683v5s2/
Results I used to get for 10 Million iterations
are as below:
Iteration 1:
"isPrimeConventionalWayArr: ", 0.0002012
"isPrimeSquarerootWayArr: ", 0.0002838
"primeArr (suggested): ", 0.0002132
"isPrimeSquarerootWayTwoArr: ", 0.0002189
Iteration 2:
"isPrimeConventionalWayArr: ", 0.0002242
"isPrimeSquarerootWayArr: ", 0.0002486
"primeArr (suggested): ", 0.000181
"isPrimeSquarerootWayTwoArr: ", 0.0002145