Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

Questions tagged [primes]

Primes or prime numbers are numbers which are divisible only by themselves and one, starting with 2, 3, 5, 7, 11.... They are commonly used in encryption and hashing algorithms.

Filter by
Sorted by
Tagged with
26
votes
2answers
2k views

Euler's Totient Function in x86 assembly (MASM)

My task was to implement Euler's totient function, and I'm looking for any and all criticism. ...
6
votes
5answers
601 views

Python prime number checker function

I was bored, and I was watching a sci-fi show where people were starting out during a long systems check asking each other whether numbers were prime or not, sort of passing the time as they do their ...
2
votes
1answer
102 views

Is It In Its Prime?

Just because I'm bored, I wrote yet another prime number generator. I think it's pretty clean, but I won't be surprised if someone finds something to comment on. ...
14
votes
5answers
4k views

Nearly Illegible Prime Number Calculator

This is my first reasonably large C# program. Considering my code looks like it was written in an esolang to me, I'm guessing I did something wrong at some point. My main priority is readability, ...
6
votes
2answers
246 views

Returning the prime factors of a number

I am trying to create a function prime-factors that returns the prime factors of a number. To do so, I created is-prime function,...
3
votes
1answer
165 views

Counting primes in given interval

So, here is a Hackerearth question, regarding (more or less) counting the number of primes between a given pair of numbers (up to 106). Then there were 3 major options available to me: Trial ...
-3
votes
1answer
83 views

Closest Prime Number via Letters [closed]

I'd like this code to be optimized and factorized, along with, perhaps, re-making it so it's more readable via introducing better functions and minimizing the lines needed for it to be human-readable (...
5
votes
1answer
470 views

Goldbach's Conjecture

I am doing an exercise on finding the nearest pair of prime numbers p₁ and p₂ such that: p₁ ≤ p₂ p₁ + p₂ = n (for 4 ≤ n ≤ 10⁷) p₁ and p₂ are primes p₂ - p₁ is minimal I think it should work ...
3
votes
0answers
625 views

Miller-Rabin Primality Test Using C++ and GMP

I'm currently writing a Miller-Rabin primality test using C++ and the GNU MP Bignum Library (GMP). As I've only started learning C++ about a day ago, I'm sure this code is riddled with errors and ...
2
votes
1answer
125 views

Mocking an F# primality tester

I would like to ask 2 questions about the modularization of an F# file. The code is just a working example of prime factorization, but that's not the main topic. I have a main file (PrimeFactors.fs), ...
2
votes
4answers
86 views

Generating N Prime Numbers

I'm trying to generate N prime numbers where my N = 200,000. The method that I think I wrote is - Start with first prime of 2 Increment possible prime number by 1 See if possible prime is ...
4
votes
1answer
406 views

Optimizing Miller-Rabin Primality Test in Python

I'm writing a Miller-Rabin primality test in Python. While I've briefly looked at how others approached the problem, I decided to try solving the problem on my own. Aside from my choice of language, ...
5
votes
1answer
2k views

C++ RSA Implementation

A while ago I wrote an implementation of RSA with C++, that takes in a simple string,encrypts and then decrypts it. It's pretty much the only substantial thing I have written in C++ and while it works ...
7
votes
2answers
569 views

Project Euler 357 prime number generator in Python 3

I'm brute forcing the Project Euler 357 since no better algorithm comes to my mind. The challenge asks: Find the sum of all positive integers n not exceeding 108 such that for every divisor d of n, ...
9
votes
4answers
4k views

Project Euler Problem #7 in Python (10001st prime number)

I have managed to solve the 7th Project Euler problem, however I think it can be improved by a lot, I am by no means a professional programmer or even consider myself really good at it. Any ...
1
vote
1answer
52 views

Prime reversion algorithm - JavaScript

I solved the following problem: Consider the range 0 to 10. The primes in this range are: 2, 3, 5, 7, and thus the prime pairs are: (2,2), (2,3), (2,5), (2,7), (3,3), (3,5), (3,7),(5,5), (5,7), (7,7)....
2
votes
1answer
143 views

Find the probability of picking a prime number in a range

The task is: From a sub-sequence of integers between 2 inputted numbers A and B, we need to find the probability of picking a prime number. I have written something to accomplish the task- ...
3
votes
0answers
104 views

Kotlin sieve using sequences

While learning about functional programming the simple sieve example was brought up in Haskell. I wanted to try a Kotlin implementation using sequences. Are there any other quick wins without ...
1
vote
1answer
2k views

code to check if number is prime number

I have written a code to check if the number is prime number are not. I pass the number from command line argument, and check if the number is prime. I have written a function for it which returns ...
4
votes
2answers
270 views

Project Euler #196: Prime triplets

I did the HackerRank version of Project Euler #196 using Python 3. My code ran successfully for 6 test cases out of 20. For test 14, it showed a timeout error which is not a functionality bug but ...
2
votes
2answers
284 views

Primality predicate in Haskell

I wrote a primality test for UniHaskell however, it seems complicated in terms of conditionals. Here it is, rewritten to not use Unicode and other functions in the code: ...
3
votes
1answer
285 views

Find primes faster (Java)

I'm new here, and relatively new to coding. I wrote a method that returns an array of the n first primes. Even though my intended goal was just to find the nth prime, I ended up using an array (int[] ...
0
votes
1answer
345 views

Finding the first 10 million primes

I'm trying to find the first 100 million primes and am trying to find the most efficient way to do it. Can I get some advice on how I could improve this? ...
5
votes
1answer
156 views

Cached infinite prime generator

For a project I am working on, I need to use a lot of primes, frequently. To do this, I added a cache to Will Ness's prime sieve, that stores already generated primes so getting them is quick. How can ...
6
votes
1answer
126 views

Python3 - Prime Sieve

I've implemented two prime sieves in python 3 to find all prime numbers up to an upper limit n - n exlusive. The first one is ...
7
votes
1answer
526 views

Maximum performance for Pollard's P-1 function

I am trying to find mersenne primes using python, and following the advice from https://www.mersenne.org/various/math.php, I have an intermediate step where I am using Pollard's P-1 factoring ...
3
votes
3answers
2k views

Checking whether a number is a Smith number

I have created a Java program to check if the number entered by the user is a Smith number (a number whose sum of digits equals the sum of the digits of its prime factorization). The program works ...
3
votes
1answer
122 views

Third project Euler solution

The things I'm interested the most from the review are: The performance of the code Overall review of the code structure, styling rules and naming conventions. What is the largest prime factor of ...
5
votes
3answers
403 views

Finding as many perfect numbers as possible

I have tried to make a perfect number finder as efficient as possible in python. If anyone has any things I can add to make it more efficient or any problems with my code please say. ...
2
votes
2answers
85 views

Pythonic Prime Generation

I am using a sieve to return all primes under N: ...
1
vote
0answers
605 views

Mersenne Prime Finder in Python

This program generates prime numbers: ...
17
votes
5answers
3k views

Sieve of Eratosthenes in C

I have attempted to implement the Sieve of Eratosthenes in C. It works rather quickly with values tested up to ten million: time ./soe.out 10000000 returns ...
2
votes
3answers
218 views

isPrime functions in Python and Java

This Python code looks messy to me. Am I doing any wrong style, or is this how Python looks like? ...
5
votes
1answer
541 views

A Public and Private Key generator for RSA

I made an RSA encryption public and private key generator and I would like some feedback. In particular, as this is cryptography related I want to know if I have done anything that might be considered ...
-2
votes
2answers
81 views

Testing primality

This method accepts an int parameter and verifies if the parameter is a prime number by trial division. It returns a Boolean value of true if it is prime or ...
7
votes
4answers
554 views

Eratosthenes' and Sundaram's prime sieves in Python

I recently started playing with Python. What can I do better? ...
1
vote
0answers
214 views

Printing all prime numbers up to N

I have an algorithm to print all prime numbers in interval [2..N]: ...
13
votes
2answers
2k views

Prime generator in Python and C

I have written the Rabin-Miller test in Python and ported it to C. But somehow, the C version is ~20 times slower on my system, and I am not sure how. Anyway, here is my code. Python ...
3
votes
1answer
578 views

Sieve of Eratosthenes using Java 8 streams

I am learning streams and lambdas in Java 8. I wanted to implement the classic Sieve of Eratosthenes using lambdas and streams. This is my implementation. ...
1
vote
2answers
222 views

Project Euler problem 3: Largest prime factor in JavaScript

I have created a soloution for Project Euler: Problem 3 - Largest Prime Factor in JavaScript: ...
7
votes
1answer
224 views

A type for prime numbers

I have recently discovered that in type theory there is a concept of a "predicate type" which is a type \$A\$ formed out of all members of the underlying type \$U\$ that satisfy a given predicate ...
-1
votes
1answer
23 views

prime function in a for loop using Math operations [closed]

guys I saw a function(the function is just below) that check wether a number is prime and return a value but I don't understand the operations made in the code. Can someone explain me mathematically?...
1
vote
1answer
532 views

Rust nth prime implementation

I have implemented an nth prime algorithm in Rust that uses value lookup for n < 6, trial division for n < 1000, and a sieve function for n > 999. How can it be better optimized? ...
12
votes
4answers
409 views

Deletable primes

I came across this problem and I was wondering if you could help me to improve my solution. Problem: Count the number of ways you can reduce a deletable prime to a one digit number. A "...
2
votes
2answers
638 views

Space and time efficient Algorithm for generating prime numbers

I have developed an algorithm for generating prime numbers (which turns out to be an optimized version of the Sieve of Eratosthenes) by reducing the space by a factor of 64. ...
5
votes
1answer
1k views

Find and print all prime numbers (less than 10000)

I want to improve my JS skills by doing challenges. My current challenge is: Write a script which allow user to enter number and then print all prime numbers from 0 to that number. ...
1
vote
1answer
505 views

Find first N primes

Here is code that returns the first N prime numbers: ...
5
votes
2answers
565 views

Prime Number Generator (6n + 1 or 6n - 1)

This generator is like most where it brute forces an integer: it see whether the integer is divisible by any of the primes; if so, then it's not a prime and vice versa. This though only compares ...
4
votes
1answer
280 views

Prime number sieve using difference of two squares

Here is a working pari/gp implementation of prime number sieve. Is there something that I could change in code to achieve a better running time? ...
5
votes
1answer
123 views

Summing all primes below 2,000,000 - Project Euler #10 in Assembly

I am currently learning assembly for university, and I'd like to hear some feedback concerning what I've written. Currently I have implemented Problems 1, 2, 3 and 10 from ProjectEuler. The goal is to ...