2
votes
1answer
88 views

Greatest prime number smaller than N where N can be as large as 10^18?

This is code for finding largest prime number smaller than N where N can be as large as 10^18. But it takes 1 minute for 10^18 . I need to pass it in 2-3 sec. What changes should I make to pass it. ...
0
votes
2answers
105 views

C++: Prime Number Generator algorithm optimization

I've implemented a simple program that finds all prime numbers between two integer inputs using Sieve of Eratosthenes, but online judge is still complaining that time limit is exceeding. Maybe I'm ...
2
votes
1answer
95 views

Simple and efficient code for finding factors and prime factorization?

I've modified this program many times, but I want to know if this is a good way to perform this task. My first algorithm for finding the factors of a number was pretty slow and horrible (started at ...
4
votes
5answers
247 views

How to optimise a for loop in a factor search to run faster?

I created a program to find the largest prime factor for a given number. The program worked but unfortunately it takes very long time to compute a huge number like 600851475143. How can I optimise ...
3
votes
2answers
203 views

Improvements for isPrime C++ function

There are many problems on the internet that require you to find prime numbers, so I decided to write a set of functions to find them. I used the Sieve of Eratosthenes for generating the primes as it ...
4
votes
6answers
716 views

Optimization - Prime Factorizations of Numbers <= 10^7

I am using Trail Division Method with a pre-calculated list of primes to calculate the prime factorization of all numbers less than M (M <= 10^7). I am using an array of vectors of pairs. The ...
11
votes
6answers
608 views

Is this C++ naive primality test done right?

Here's my C++ code: bool isPrime(double value) { if (value == 2) // ensure 2 returns true return true; else if (value <= 1) // eliminate 1 and all ...
2
votes
2answers
287 views

Prime Number calculation bottleneck help

Inspired by another question here: Sum of Prime numbers under 2 million, I decided to try and implement Eratosthenes' Sieve from scratch using the algorithm described in the article. unsigned int ...
8
votes
6answers
2k views

Sum of all primes under 2 million

I have this assignment to write the optimized space and time code for finding the sum of all primes under 2 million in c/c++. Im using the following function to check if each number is prime int ...
5
votes
4answers
450 views

Please comment on this code for generating prime numbers within a range

I was given a problem to find out all the prime numbers within a range. Just after I wrote the program I found numerous other codes to solve the problem. I am interested to know is my algorithm ...
2
votes
9answers
2k views

Prime number finder

There's another exercise from Thinking in C++. This time it asks this: Write a program that uses two nested for loops and the modulus operator (%) to detect and print prime numbers ...