Javascript getting the nth prime number - javascript

This should return the nth prime (n being the number given by the user). It works fine for the first few numbers (1 returns 2, 2 returns 3, 3 returns 5) but when 5 is given, it returns 9 which isn't prime (it should be 11). This happens with other numbers as well above this (7 returns 15 when it should be 17).
The 'document' stuff is to do with HTML where I'm getting the userValue and to display the prime number.
function isPrime(value) {
for(var i = 2; i < value; i++) {
if(value % i === 0) {
return false;
}
}
return value > 1;
}
function generatePrime() {
var userValue = document.getElementById("inputValue").value;
var iter = 1;
var returnValue = 2;
//checks for an integer
if (parseInt(userValue) === parseFloat(userValue)) {
//checks if the user inputted a value above 0
if (userValue > 0) {
//loops to find the correct prime
while (iter < userValue) {
if (isPrime(returnValue)) {
returnValue += 1;
iter += 1;
}
if (!isPrime(returnValue)) {
returnValue += 1;
}
}
}
else {
returnValue = "That is not a number above 0!";
}
}
else {
returnValue = "That is not a number!";
}
document.getElementById("returnValue").innerHTML = returnValue;
}
I need help with making this return the correct number.

Try this one.
function nextPrime(value) {
if (value > 2) {
var i, q;
do {
i = 3;
value += 2;
q = Math.floor(Math.sqrt(value));
while (i <= q && value % i) {
i += 2;
}
} while (i <= q);
return value;
}
return value === 2 ? 3 : 2;
}
function generatePrime() {
var userValue = document.getElementById("inputValue").value;
var value = 0, result = [];
for (var i = 0; i < userValue; i++) {
value = nextPrime(value);
result.push(value);
}
document.getElementById("returnValue").innerHTML = result[userValue-1];
}
<!DOCTYPE html>
<html>
<head>
<script>
</script>
</head>
<body>
Input value: <input type="text" name="inputValue" id="inputValue"/>
<button onclick="generatePrime()">Prime number</button>
<div id="returnValue">Test: </div>
</body>
</html>

You can try something like this:
function getNthPrimeNumber(n){
var count = 0;
var num = 2;
while(count++ != n){
num = getNextPrimeNumber(num);
}
return num;
}
function getNextPrimeNumber(n){
for(var i = ++n; i< n*n; i++){
if(isPrime(i)) return i
}
return 0;
}
function isPrime(n){
for(var i = 2; i< n; i++)
if (n%i===0)
return false;
return true;
}
console.log(getNthPrimeNumber(0))
console.log(getNthPrimeNumber(2))
console.log(getNthPrimeNumber(5))

You could reduce the range of iteration for function isPrime() :
function isPrime(value) {
for(var i = 2; i < Math.sqrt(value) + 1; i++) {
if(value % i === 0) {
return false;
}
}
return value > 1;
}

The following is based on Bhaskara Arani's submission, but I added caching.
Note: I also threw in an addition condition to check for palindrome.
This check can be removed via:
while (i <= root && value % i /* && isPalindrome(value) */)
const main = () => {
for (let i = 15; i >= 0; i--) {
console.log(`Palindrome Prime ${i + 1} = ${generatePrime(i)}`);
}
};
const cachedPrimes = [];
const generatePrime = (index) => {
let value = cachedPrimes[cachedPrimes.length - 1] ?? 0;
for (let i = cachedPrimes.length; i <= index; i++) {
value = nextPrime(value);
cachedPrimes.push(value);
}
return cachedPrimes[index];
};
const nextPrime = (value) => {
if (value > 2) {
let i, root;
do {
i = 3; value += 2; root = ~~Math.sqrt(value);
while (i <= root && value % i && isPalindrome(value)) {
i += 2;
}
} while (i <= root);
return value;
}
return value === 2 ? 3 : 2;
};
const isPalindrome = (n) => n === reverse(n);
const reverse = n => +`${n}`.split('').reverse().join('');
main();
.as-console-wrapper { top: 0; max-height: 100% !important; }

Related

Loop failure, values are not cleared when element = maximum a bug is displayed

I am iterating over the matrix object. Which I create inside the loop, then I modify its values through the increment. When the increment reaches its maximum (51), I have to go back to the outer loop and create a new object one more element. Example: {0: 0, 1: 0}. Now I iterate over the last element in the object, if it = maximum, I go to the next one, from "1" to "0", and increment "0", and "1" = 0. Then I have to iterate over "1".
But the values are not cleared when element = maximum a bug is displayed.
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
function check(maxLength) {
for (let matrixLength = 0; matrixLength < maxLength; matrixLength++) {
let a = matrix(matrixLength);
let Indx;
while (Indx!==null) {
Indx = getIndex(a,letters.length);
console.log(increment(a,Indx,Indx));
}
}
}
function matrix(length) {
let matrix = {};
for (let i = 0; i <= length; i++) {
matrix[i] = 0;
}
return matrix;
}
function getIndex(matrix, arrLength) {
for (let i = Object.values(matrix).length - 1; i >= 0; i--) {
if (matrix[i]!==arrLength - 1) {
return i;
}
}
console.log('null');
return null;
}
function increment(matrix, index, prevIndex = null) {
matrix[index]++;
if (prevIndex === null || index === prevIndex) {
return matrix;
}
for (let i = index + 1; i < Object.values(matrix).length; i++) {
matrix[i] = 0;
}
return matrix;
}
check(3);
Check whether Indx is null after calling getIndex, not before the next iteration.
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
function check(maxLength) {
for (let matrixLength = 0; matrixLength < maxLength; matrixLength++) {
let a = matrix(matrixLength);
while (true) {
let Indx = getIndex(a, letters.length);
if (Indx == null) {
break;
}
console.log(increment(a, Indx, Indx));
}
}
}
function matrix(length) {
let matrix = {};
for (let i = 0; i <= length; i++) {
matrix[i] = 0;
}
return matrix;
}
function getIndex(matrix, arrLength) {
for (let i = Object.values(matrix).length - 1; i >= 0; i--) {
if (matrix[i] !== arrLength - 1) {
return i;
}
}
console.log('null');
return null;
}
function increment(matrix, index, prevIndex = null) {
matrix[index]++;
if (prevIndex === null || index === prevIndex) {
return matrix;
}
for (let i = index + 1; i < Object.values(matrix).length; i++) {
matrix[i] = 0;
}
return matrix;
}
check(3);

CodeWars sorting numbers and letters

I am currently doing a codewars problem, and I think I almost got it however, I ran across a problem when sorting index values with the same letter. link to problem is here. https://www.codewars.com/kata/5782dd86202c0e43410001f6
function doMath(s) {
let strSplit = s.split(' ');
let clonedArr = strSplit.slice();
for (let i = 0; i < strSplit.length; i++) {
for (let j = 0; j < strSplit[i].length; j++) {
let current = strSplit[i][j];
if (isNaN(current)) {
let letter = current;
strSplit[i] = strSplit[i].replace(letter, '');
strSplit[i] = letter + strSplit[i];
}
}
}
let sortedArr = strSplit.sort();
console.log(sortedArr);
// ["b900", "y369", "z123", "z246", "z89"]
let noLetterArr = sortedArr.map(x => {
return x.slice(1);
});
let numberArr = noLetterArr.map(y => {
return +y;
})
let firstEl = numberArr[0];
for (let i = 1; i < numberArr.length; i++) {
if (numberArr.indexOf(numberArr[i]) % 4 == 1) {
firstEl += numberArr[i];
}
if (numberArr.indexOf(numberArr[i]) % 4 == 2) {
firstEl -= numberArr[i];
}
if (numberArr.indexOf(numberArr[i]) % 4 == 3) {
firstEl *= numberArr[i];
}
}
return firstEl;
}
console.log(doMath('24z6 1z23 y369 89z 900b'));
I would like to sort the sortedArr the ones with the same letter by how they first appeared in string. So since "z246" appeared first in the original string. I would like to have that before "1z23". I had a hard time creating a function for that.
var al = [];
function doMath(s) {
var ar = s.split(" ");
for (let i = 0; i < ar.length; i++) {
for (let char of ar[i]) {
let temp = char.match(/[a-z]/i);
if (temp) {
al[i] = char;
ar[i] = ar[i].replace(char, '');
ar[i] = char + ar[i];
}
}
}
al = al.sort();
//New Sort Logic to pass above test case and others too
var n = [];
for (let i = 0; i < al.length; i++) {
for (let j = 0; j < ar.length; j++) {
if (ar[j].startsWith(al[i]) && !n.includes(ar[j])) {
n.push(ar[j]);
}
}
}
var result = parseInt(n[0].substr(1)),
count = 1;
for (let i = 1; i < n.length; i++) {
if (count == 1) {
result = result + parseInt(n[i].substr(1));
count++;
} else if (count == 2) {
result = result - parseInt(n[i].substr(1));
count++;
} else if (count == 3) {
result = result * parseInt(n[i].substr(1));
count++;
} else if (count == 4) {
result = result / parseInt(n[i].substr(1));
count = 1;
}
}
return Math.round(result);
}

Calculating average from array - How to return null if empty

I'm new to javascript and I'm working on an assignment to create a function that calculates the average using the numbers in an array. If the array is [], it should return null.
I have the following code so far but it keeps returning NaN instead of null, can someone please explain this to me? Thanks in advance.
function mean(arr) {
if (arr == []) {
return null;
} else {
var sum = 0;
var average = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return round(sum / arr.length);
}
}
console.log(mean([6,2,3,3,110,6,1,0,5])); //returns 4
console.log(mean([])); //currently returning NaN, instead of null
Here you go. Just change arr == [] to arr.length == 0.
function mean(arr) {
if (arr.length == 0) {
return null;
} else {
var sum = 0;
var average = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return Math.round(sum / arr.length);
}
}
console.log(mean([6,2,3,3,110,6,1,0,5])); //returns 4
console.log(mean([])); //currently returning NaN, instead of null
Check the length of the array using arr.length ===0
function mean(arr) {
if (arr.length === 0) {
return null;
} else {
var sum = 0;
var average = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return round(sum / arr.length);
}
}
console.log(mean([]));
Added additional condition to check if the variable is not an array to avoid runtime exception.
function mean(arr) {
If (arr.constructor != Array) return null;
if (arr.length == 0) {
return null;
} else {
var sum = 0;
var average = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return Math.round(sum / arr.length);
}
}

Need help sorting array and displaying it on the HTML page

My HW assignment requires me to have a user enter integers and for it to come out on the HTML in sorted array without the sort method. I have this code and it asks the users input but the integers do not come up on page.
<script type="text/javascript">
var arr = [];
function addNum() {
var n = prompt("How many integrs?", "0");
var num = parseInt(n);
for (var i = 0; i < num; i++) {
arr[i] = parseInt(prompt("Enter next integer: ","0"));
}
var outputSorted = document.getElementById('outputSorted');
outputSorted = "";
outputSorted.append("Input array : ");
for (var i = 0; i < num; i++) {
outputSorted.append(arr[i]+" ");
}
bubbleSort(arr);
outputSorted.append("Sorted array : ");
for (var i = 0; i < num; i++) {
outputSorted.append(arr[i]+" ");
}
}
function bubbleSort(a) {
var swapped;
do {
swapped = false;
for (var i=0; i < a.length - 1; i++) {
if (a[i] > a[i+1]) {
var temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
swapped = true;
}
}
} while (swapped);
}
function searchNum() {
var m = parseInt(prompt("Enter num to be searched: ","0"));
var found = binarySearch(arr, m);
var outputSearch = document.getElementById('outputSearch');
if (found == -1) {
outputSearch.append("Number not found");
} else {
outputSearch.append("Number found at index : " + (found + 1));
}
}
function binarySearch(array, targetValue) {
var min = 0;
var max = array.length - 1;
var guess;
while (min <= max) {
guess = Math.floor((max + min) / 2);
if (array[guess] === targetValue) {
return guess;
}
else if (array[guess] < targetValue) {
min = guess + 1;
}
else {
max = guess - 1;
}
}
return -1;
}
</script>
outputSorted is a DOM element, and append() is not a function of that element. Rather than that, use innerHTML += for each bit you want to add. See below. And note, those are the ONLY changes I made. So your ugly rendered HTML is what it is.
var arr = [];
addNum = function addNum() {
var n = prompt("How many integrs?", "0");
var num = parseInt(n);
for (var i = 0; i < num; i++) {
arr[i] = parseInt(prompt("Enter next integer: ", "0"));
}
var outputSorted = document.getElementById('outputSorted');
outputSorted.innerHTML += "Input array : "
for (var i = 0; i < num; i++) {
outputSorted.innerHTML += arr[i] + " ";
}
bubbleSort(arr);
outputSorted.innerHTML += "Sorted array : ";
for (var i = 0; i < num; i++) {
outputSorted.innerHTML += arr[i] + " ";
}
}
function bubbleSort(a) {
var swapped;
do {
swapped = false;
for (var i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
var temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
}
function searchNum() {
var m = parseInt(prompt("Enter num to be searched: ", "0"));
var found = binarySearch(arr, m);
var outputSearch = document.getElementById('outputSearch');
if (found == -1) {
outputSearch.innerHTML += ("Number not found");
} else {
outputSearch.innerHTML += ("Number found at index : " + (found + 1));
}
}
function binarySearch(array, targetValue) {
var min = 0;
var max = array.length - 1;
var guess;
while (min <= max) {
guess = Math.floor((max + min) / 2);
if (array[guess] === targetValue) {
return guess;
} else if (array[guess] < targetValue) {
min = guess + 1;
} else {
max = guess - 1;
}
}
return -1;
}
<button onclick="addNum()">
Add some!
</button>
<div id="outputSorted">
</div>
<div id="outputSearch">
</div>

How do I sum all prime numbers?

I am working on an excercise to sum all prime numbers from 2 to the parameter. I have worked this far in the code, but am stuck. I believe by using the splice function, I am actually skipping an element because of a changed indices.
function sumPrimes(num) {
var primearray = [];
var sum = 0;
for(var i =2; i <= num; i++){
primearray.push(i);
}
for(var j = 0; j < primearray.length; j++) {
console.log(primearray[j]);
if ((primearray[j]%2===0) && (primearray[j] >2)) {
primearray.splice(j,1);
} else if ((primearray[j]%3===0) && (primearray[j] > 3)) {
primearray.splice(j,1);
console.log(primearray);
} else if ((primearray[j]%5===0) && (primearray[j] > 5)) {
primearray.splice(j,1);
} else if ((primearray[j]%7===0) && (primearray[j] > 7)) {
primearray.splice(j,1);
}
}
sum = primearray.reduce();
return sum;
}
sumPrimes(30);
I haven't utilized the reduce function yet because I am still working on the if else statements.
I found a pretty good solution to the same problem. afmeva was spot on. This is how it works.
function isPrime(val){
//test if number is prime
for(var i=2; i < val; i++){
if(val % i === 0){
return false;
}
}
return true;
}
In the above code we accept a number to determine whether or not it is prime. We then loop from two all the way up until our number minus one because we know that our number will be divisible by itself and one. If the remainder of our value with the current loop value is zero then we know it is not prime so break out and say so.
This article explains very well
function sumPrimes(num) {
var answer = 0;
//loop through all numbers from 2 to input value
for(var i=2; i <= num; i++){
//sum only prime numbers, skip all others
if(isPrime(i)){
answer += i;
}
}
return answer;
}
sumPrimes(977); // 73156
Here's another good resource
function sumPrimes(num) {
let arr = Array.from({length: num+1}, (v, k) => k).slice(2);
let onlyPrimes = arr.filter( (n) => {
let m = n-1;
while (m > 1 && m >= Math.sqrt(n)) {
if ((n % m) === 0)
return false;
m--;
}
return true;
});
return onlyPrimes.reduce((a,b) => a+b);
}
sumPrimes(977);
I have seen lots of people putting all prime numbers into arrays and in order to check if a number is prime, they check from 2 to the number to see if there's a remainder.
You only need to check odd numbers, and only need to count to half the number because a number can't be divisible by any number greater than it has.
Here's my solution:
function sumPrimes(num){
var sum = num>=2?2:0;
for(var i=3;i<=num;i+=2){
var isPrime=true;
for(var j=3;j<(i/2);j++){
if (i%j==0)
{
isPrime=false;
break;
}
}
sum+=isPrime?i:0;
}
return sum;
}
Note: I started from j=2 because we are only checking odd numbers, so they'd never be divisible by 2.
function sumPrimes(num) {
var sumArr= [];
for(var i=0;i<=num;i++){
if(isPrime(i))
sumArr.push(i);
}
sumArr = sumArr.reduce(function(a,b){
return a+b;
})
return sumArr;
}
function isPrime(num) {
if(num < 2) return false;
for (var i = 2; i < num; i++) {
if(num%i === 0)
return false;
}
return true;
}
sumPrimes(10);
something like this?
function isPrime(_num) {
for(var i = 2; i < _num; i++) {
if(!(_num % i)) {
return false
}
}
return true;
}
function sumPrimes(_num) {
var sum = 0;
for(var i = 2; i <= _num; i++) {
if(isPrime(i)) {
sum += i;
}
}
return sum;
}
sumPrimes(20) // 77
sumPrimes(5) // 10
You could do this as well.
function sumPrimes(num) {
var sum = 0;
for (var i = 0; i <= num; i++) {
if (isPrime(i)) {
sum += i;
}
}
return sum;
}
function isPrime(n) {
if (n < 2) { return false; }
if (n !== Math.round(n)) { return false; }
var result = true;
for (var i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) {
result = false;
}
}
return result;
}
Here's my solution. I hope you find it easy to interpret:
function sumPrimes(num) {
// determine if a number is prime
function isPrime(n) {
if (n === 2) return true;
if (n === 3) return true;
if (n % 2 === 0) return false;
if (n % 3 === 0) return false;
var i = 5;
var w = 2;
while (i * i <= n) {
if (n % i === 0) {
return false;
}
i += w;
w = 6 - w;
}
return true;
}
// subtract 1 for 'not being prime' in my context
var sum = isPrime(num) ? num - 1 : -1;
for (var x = 0; x < num; x++) {
if (isPrime(x) === true) {
sum += x;
}
}
return sum;
}
here is my solution to sum of n prime number
function sumOfNPrimeNumber(num){
var sum = 0;
const isPrime = function(n){
if (isNaN(n) || !isFinite(n) || n%1 || n<2) {
return false;
}
if (n%2==0){
return (n==2);
}
var sqrt = Math.sqrt(n);
for (var i = 3; i < sqrt; i+=2) {
if(n%i == 0){
return false;
}
}
return true;
}
const getNextPrime = function* (){
let nextNumber = 2;
while(true){
if(isPrime(nextNumber)){
yield nextNumber;
}
++nextNumber;
}
}
const nextPrime = getNextPrime();
for (var i = 0; i < num; i++) {
sum = sum + nextPrime.next().value;
}
return sum;
}
console.log(sumOfNPrimeNumber(3));
All the above answers make use of helper functions or aren't time efficients.
This is a quick, recursive solution in O(n) time:
// # signature int -> int
// # interpr: returns sum of all prime integers <= num
// assume: num is positive integer
function sumPrimes(num) {
if (num <= 2) {
return 2;
}
let i = 2;
while (i < num) {
if (num % i === 0) {
return sumPrimes(num - 1)
}
i++;
}
return num + sumPrimes(num - 1)
}
// test
sumPrimes(10); // -> 17
function prime_sum(num){
let count=0; *//tracks the no of times number is divided perfectly*
for(let i=1;i<=num;i++){ *//from 1 upto the number*
if(num%i==0){count++};
}
if(count===2){return "Prime"};
return{"Not prime"};
}
console.log(prime_sum(10));//expected output is 17**
//the code receives a number,checks through the range and returns prime if it meets the condition
The following solution uses the Eratosthenes Sieve to sum all prime numbers lower than or equal to num. The first for loop fills an array with size equal to num with true. The second for loop sets to false all non-prime numbers in the array. Then, the last for loop simply iterates through the array to sum all the array indexes i for which the value in the array, i.e., array[i], is equal to true.
/**
* Sum all primes lower or equal than n.
* Uses the Eratosthenes Sieve to find all primes under n.
*/
function sumPrimes(num) {
let array = [];
let output = 0;
// Fill an array of boolean with 'true' from 2 to n.
for (let i = 0; i <= num; i++) {
array.push(true);
}
// Set all multiples of primes to 'false' in the array.
for (let i = 2; i <= Math.sqrt(num); i++) {
if (array[i]) {
for (let j = i * i; j <= num; j += i) {
array[j] = false;
}
}
}
// All array[i] set to 'true' are primes, so we just need to add them all.
for (var i = 2; i <= num; i++) {
if (array[i]) {
output += i;
}
}
return output;
}
console.log(sumPrimes(10)); // 17
console.log(sumPrimes(977)); // 73156
console.log(sumPrimes(250_000_000)); // 197558914577
function sumPrimes(num) {
let output = 0;
// check if num is a prime number
function isPrime(num) {
for(let i = 2; i < num; i++) {
if(num % i === 0) {
return false;
}
}
return true;
}
for (let i = 2; i <= num; i++) {
if (isPrime(i)) {
output += i;
}
}
return output;
}
console.log(sumPrimes(10)); // 17
This is what I've done to get primes. I don't know if it's the most efficient, but it works. This is in Java, but can be easily converted to JavaScript. Hopefully this will help point you in the right direction.
final int TOTAL = 10;
int primes[] = new int[TOTAL];
int arrPos = 2;
boolean prime = false;
primes[0] = 2;
for (int i = 2; i < TOTAL; i++) {
prime = false;
int sqrt = (int) Math.sqrt(i);
for (int j = 1; j < arrPos && primes[j] < sqrt; j++) {
if (i % primes[j] != 0) {
prime = true;
} else {
prime = false;
break;
}
}
if (prime == true) {
primes[arrPos] = i;
arrPos++;
}
}

Categories

Resources