Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
prime consecutive numbers
/** * Created by ankit suman on 22-06-2017. * * Some prime numbers can be expressed as Sum of other consecutive prime numbers. For example 5 = 2 + 3 17 = 2 + 3 + 5 + 7 41 = 2 + 3 + 5 + 7 + 11 + 13 Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2. Write code to find out number of prime numbers that satisfy the above mentioned property in a given range. Input Format: First line contains a number N Output Format: Print the total number of all such prime numbers which are less than or equal to N. Constraints: 1. 2<N<=12,000,000,000 */ /** * the number of prime values under 20 : 2,3,5,7,11,13,17,19 */ /** * steps: finding prime numbers * and then finding summation starting from 2. * */ var stdin = process.openStdin(); console.log("enter the value of N :"); stdin.addListener("data", function (v) { var n = parseInt(v); var arr = [],count=0; if(n>12000000000 || n<3){ console.log("range:<=12,000,000,000"); } for (var i = 2; i < n; i++) { // console.log(i); if (prime(i)) { //console.log(i); arr.push(i); } } //console.log( arr); for(var j=0;j<arr.length;j++){ if(sum(arr,arr[j])) count++; } console.log((count-1)); }); /** * prime * @return 1 for prime */ var prime = function (number) { var flag = 1; for (var i = 2; i < number; i++) { if (number % i == 0) { flag = 0; break; } } return flag; }; var sum = function (ar, num) { var pri = [];var s=0,flag=0; for(var i=0;i<ar.length;++i){ // console.log(s); s+=ar[i]; pri.push(ar[i]); if(s==num) { flag=1; break; } // s+=ar[i]; } //console.log(pri); return flag; };
run
|
edit
|
history
|
help
0
House prices
strCompression
ComposeWordsGrid
Tik
Triangle
color
Nombre en MAYÚSCULAS
Change format Numbers
Javascript is weird
BinaryGap, javascript - Find longest sequence of zeros in binary representation of an integer.