Posts

Showing posts with the label primes

Printing Prime Numbers Till N in C++

Image
Clash Royale CLAN TAG #URR8PPP Printing Prime Numbers Till N in C++ You are given an integer N. You need to print the series of all prime numbers till N. I want to know whats wrong with my code and suggestions will also be of great help. #include<iostream> using namespace std; int main() { int N; cin>>N; int u; for(int i=N;i>0;i--) { u=0; for(int j=2;j<N-1;j++) { if(i%j==0) { u=1; } } if(u==0) { cout<<i<<" "; } } return 0; } Thanks for helping out. :) By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

How do I calculate all prime numbers with a really large max in the browser using JSFiddle

Image
Clash Royale CLAN TAG #URR8PPP How do I calculate all prime numbers with a really large max in the browser using JSFiddle I am working on some of the typical katas for JS and I came across one that wanted all the primes for a really large number. I tried the following in JSFiddle. findPrimes(max){ let dont = , primes = ; for (var i = 2; i <= max; i++) { if (!dont[i]) { primes.push(i); for (var j = i; j <= max; j += i) dont[j] = true; } } } This works relatively good till about this.findPrimes(51475143); , however, if I try say... this.findPrimes(851475143); I get a sad face an the JS engine appears to crash. I know I could probably do straight V8 and squeeze a bit out and maybe even go toward a C-based node module but to keep things simple I would like to keep it in the browser if possible. If not and proof can be provided I will accept that answer. this.findPrimes(51475143); this.findPrimes(851475143); Hrm, well, rega...