Primes or prime numbers are numbers which are divisible only by themselves and one: 2, 3, 5, 7, 11, ...

learn more… | top users | synonyms

5
votes
5answers
401 views

Project Euler #3 - largest prime factor

I was going through the Project Euler problem #3 and made a program to solve it. The problem is as follows: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of ...
9
votes
3answers
680 views
12
votes
4answers
907 views

Prime Number Generator - Are the conventions proper?

I am slowly learning C and C++, and decided after a few lessons to dive in myself without any help. I developed this Prime Number generator. It seems fast, but I'm wondering if I'm following the best ...
3
votes
2answers
104 views

Prime generator program SPOJ time limit exceed

Problem Statement Input The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= ...
10
votes
4answers
625 views

Sieve of Eratosthenes in C++

I'm looking for some feedback on my implementation of the algorithm. How can I improve it? I ran into problems when calculating the larger prime numbers > 46349 due to integer overflow, but fixed that ...
16
votes
5answers
793 views

Count distinct primes, discarding palindromes, in under 2 seconds

Problem Statement Generate as many distinct primes P such that reverse (P) is also prime and is not equal to P. Output: Print per line one integer( ≤ 1015 ). Don't print more than ...
11
votes
2answers
767 views

First prime number larger than given integer

How can I test this C program for "efficiency"? The most interesting usage is that it returns negative output for large enough input, otherwise the behavior is about expected. Will you suggest how to ...
7
votes
2answers
215 views

Prime generator from one to n

My code calculates primes from one to n. I have verified that the code always produces all the primes in that range correctly. Are there any optimizations that I can make? Are there any bad ...
10
votes
2answers
323 views

First attempt at generating prime numbers

I had to look up some other solutions online because I could not figure it out on my own, which kinda sucks. I really wish I came up with it completely on my own but that didn't happen. Nevertheless, ...
7
votes
5answers
464 views

Ten thousand and first prime checker takes a very long time

I am attempting problem 7 on Project Euler. I have come up with this solution which works fine for finding smaller nth prime numbers, but really lags when it comes to higher nth prime numbers. I am ...
4
votes
3answers
84 views

Prime number util class

A prime number util, which provides 3 APIs - find the nth prime, find the next prime, and find all primes up to n. I must admit this code has been influenced a lot by discussions here. I'm ...
4
votes
6answers
286 views

Speeding up my implementation of Project Euler #3

Project Euler problem 3 asks for the largest prime factor of 600851475143. I have gone from 96 lines to 17 lines taking hits at this one. This is what I am left with after all of that effort: ...
13
votes
10answers
1k views

Prime number generator exercise

I have been trying to create a class that would list prime numbers forever. I know it has already been done but I think I learned some valuable principles during my trials. This project helped me ...
0
votes
2answers
33 views

Prime conjecture [closed]

I am trying to solve this SPOJ question. ...
3
votes
2answers
162 views

BigInteger prime testing

...
3
votes
1answer
131 views

Sum of two squares

I'm trying to do a SPOJ problem called twosquares where you check whether the current number can be obtained by adding two squares together. However, I'm getting a "time limit exceeded" message. I'm ...
11
votes
1answer
91 views

Python implementation of prime modular square root

I did an implementation of the Tonelli-Shanks algorithm as defined on Wikipedia. I put it here for review and sharing purpose. Legendre Symbol implementation: ...
11
votes
4answers
269 views

Factorizing integers

I'm working on this method in Java, which is giving me the factors of a number. I'll be using this method a lot, and I was wondering if there isn't a better way of doing it. Like, with just one loop. ...
10
votes
2answers
129 views

Thread-safe prime number source

I am working on some multi-threaded code that requires a prime-number source. The following code keeps an array of primes in memory, extending it as needed. It works in the 'int' domain, being ...
10
votes
3answers
118 views

Project Euler “Largest prime factor” (#3) in Java

The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? I wrote the following code with help of some Java 8, I'll explain the equivalent ...
5
votes
4answers
500 views

Sieve of Eratosthenes - Python

So I've been doing a lot of Project Euler lately and just wanted to make sure my implementation was as good as it could be. Does anyone have any suggestions to speed this up? ...
7
votes
1answer
101 views

Strange performance difference

For exercise, I wrote this code: ...
7
votes
3answers
210 views

Sieve of Erathostenes optimization

I am making a program to find primes between the given range of numbers. Unfortunately, the algorithm I created is too slow. Do you have any ideas of how to optimize the code? ...
4
votes
1answer
133 views

Why does this python code take so long?

I don't understand what is taking the program so long: ...
5
votes
1answer
43 views

Project Euler #41 - pandigital prime

I've written a solution to Euler 41: We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is ...
7
votes
2answers
605 views

Finding prime factors of a number?

Okay, so just having a little fun I thought I'd see if I could write an algorithm to find all of the prime factors of a number. The solution I came up with is as follows: ...
6
votes
1answer
91 views

Number of prime numbers between 1 and n in javascript

I came up with following solution to find the number of prime numbers from 1 to n. Wondering if there is a more optimal way. ...
3
votes
4answers
197 views

Relative prime numbers

Two integers a and b are relatively prime if and only if there are no integers: x > 1, y > 0, z > 0 such that a = xy and b = xz. I wrote a program that determines how many positive integers less ...
3
votes
3answers
344 views

Smallest prime factor of a large number (using BigInteger)

I'm working on a Project Euler problem and am trying to get the smallest prime factor of a BigInteger: ...
7
votes
2answers
349 views

Checking for circular primes - Project Euler Problem 35

I have been trying to solve Project Euler problem number 35. The problem is to find: How many circular primes are there below one million? A circular prime is a prime where all the rotations of ...
2
votes
2answers
248 views

Find next consecutive prime and find nth prime

Code review for best practices, optimizations, bug detection etc. Also please verify my complexity as mentioned within the function comments. ...
11
votes
4answers
353 views

Suggestions for improvement on Project Euler #7

Here is a solution for Project Euler Problem #7. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10,001st prime number? I've ...
2
votes
3answers
156 views

Project Euler #12 in C++ - highly divisible triangular number

Project Euler Problem 12 asks (paraphrased): The nth triangular number Tn = 1 + 2 + … + n. T7 = 28 has 6 divisors (1, 2, 4, 7, 14, 28). What is the first Tn to have over 500 divisors? After ...
5
votes
3answers
452 views

Summation of primes takes forever

Project Euler Problem 10 asks: "What is the sum of all prime numbers below 2 million?" How do I speed up my code? It's taking forever and I have never gotten the answer. However, I am sure that my ...
0
votes
1answer
26 views

Caml light and finding the prime factors

I wrote the following Caml light code to find prime factors of a number (part of an early Project Euler question). Largest prime factor The prime factors of 13195 are 5, 7, 13 and 29. ...
3
votes
3answers
376 views

Determining whether or not a number is prime

I wrote this code in Java without using any methods, just by using simple for loop and if statements. When we input a number, ...
3
votes
3answers
208 views

How Pythonic is my Project Euler #5? (LCM of [1, 2, …, 20])

I have implemented a solution to Project Euler problem 5: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive ...
0
votes
0answers
87 views

Optimizing my Quadratic Sieve, with advice on style?

I'm still new to Haskell, and I'd like others' opinions on optimizing my basic quadratic sieve, in addition to feedback on the code's clarity and functional style. The ...
2
votes
5answers
141 views

Is there a shorter way to print out prime numbers from an array given a max number in ruby

Is there a shorter or maybe a cleaner way of doing: Q: Using your is_prime? method, write a new method, primes that takes a ...
0
votes
1answer
63 views

Review my prime factorization function. Is it complete?

This is a function designed to return all the prime factors of a given number, in sorted order from least to greatest, in a list. It relies on another function that checks if a given number is prime. ...
3
votes
1answer
90 views

Is a Recursive-Iterative Method Better than a Purely Iterative Method to find out if a number is prime?

I made this program in C that tests if a number is prime. I'm as yet unfamiliar with Algorithm complexity and all that Big O stuff, so I'm unsure if my approach, which is a combination of iteration ...
2
votes
1answer
50 views

Any problems with this prime checker script?

I have just made what seems to me to be the first reliable prime-checking program that works. However, I am not sure if it is completely reliable, or if there are problems. It relies on list-making ...
2
votes
3answers
574 views

Project Euler Problem 7: Find 10001st prime number

I was able to complete the problem, but I would like to improve my code and make it more idiomatic. Here is the challenge description: Problem 7 - 10001st prime By listing the first six ...
3
votes
1answer
123 views

Prime number dictionary

I attempted to write a read-only dictionary, containing primes: ...
3
votes
2answers
92 views

Attempting to efficiently compute the largest prime factor

As per the help, here's what I'm looking for from this post: best practice and practical advice about (what I think is describable as) buffering the output of a function. So I'm working on the third ...
6
votes
2answers
345 views

Improving remake of factoring/prime factorization calculator

Background info on factoring and prime factorization. I've remade this program from my first attempt, and I'm sure it can still be improved. My questions: These calculations should only be done ...
0
votes
2answers
1k views

Problem in calculating prime numbers in python [closed]

I'm a noob in programming. I was asked to code a program that can calculate prime numbers. So i coded as provided below. But it prints all the Odd Numbers instead of Prime Numbers. I think the Value ...
6
votes
2answers
193 views

Sieve of Erastosthenes in Java

Here you can find my implementation of sieve of eratosthenes in Java. ...
4
votes
1answer
91 views

Questions about opinions on prime number generator

What do you consider the readability of this program? Do you think it's easy to understand what the variables and functions do just by reading their names? Would you prefer more, less, or this amount ...
1
vote
1answer
160 views

Clojure code for finding prime numbers

This is my Clojure code for finding prime numbers. Note: this is an advanced version which starts eliminating from i*i with step ...