Skip to content

Instantly share code, notes, and snippets.

@ShenTengTu
Created December 27, 2017 08:12
Show Gist options
  • Save ShenTengTu/711061276df2ed6b25d321aa9956c4b0 to your computer and use it in GitHub Desktop.
Save ShenTengTu/711061276df2ed6b25d321aa9956c4b0 to your computer and use it in GitHub Desktop.
Find least Prime Factor.
function leastPrimeFactor(x){
let seq = Array(x+1);
seq[1] = 1;
for(let i = 2;i<=x;i++){
if(!seq[i]){
seq[i] = i;
for(let j=2*i; j<=x;j+=i){
if(!seq[j])
seq[j] = i;
}
}
}
seq.forEach((e,i)=>{
console.log(`Least Prime factor of ${i}: ${e}`);
})
}
leastPrimeFactor(15);
/*
Least Prime factor of 1: 1
Least Prime factor of 2: 2
Least Prime factor of 3: 3
Least Prime factor of 4: 2
Least Prime factor of 5: 5
Least Prime factor of 6: 2
Least Prime factor of 7: 7
Least Prime factor of 8: 2
Least Prime factor of 9: 3
Least Prime factor of 10: 2
Least Prime factor of 11: 11
Least Prime factor of 12: 2
Least Prime factor of 13: 13
Least Prime factor of 14: 2
Least Prime factor of 15: 3
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment