The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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.

633 questions
Filter by
Sorted by
Tagged with
30 views

Find parallel-base left-truncatable primes

Factorial base - representation of numbers not as sums of base_number^digit_number, but as sums of digit_number! (factorial). So 7 (decimal) is represented as 101_! in factorial base, because 7 = 6+1 =...
126 views

Segmented wheel Sieve of Eratosthenes in Python

Here is a new version of a segmented and wheel factorized Sieve of Eratosthenes. It currently uses mod 30 wheel factorization to eliminate multiples of 2, 3, and 5 in the sieve data structure to gain ...
128 views

Segmented and wheel factorized prime generator in Python

I recently discovered that it is much faster to generate primes using a Sieve of Eratosthenes (SoE) with a boolean array data structure than with a heap. Since then I have been looking for fast and ...
99 views

Finding the nth ugly number

I'm trying to optimize my solution to this LeetCode problem: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the ...
71 views

Optimizing the Sieve of Eratosthenes for prime generation

I've been on a Project Euler spree and I've been solving problems and as you might know, many of them require you to use prime numbers. I've been using the following code to generate and store primes ...
63 views

Java solution to CodeChef “Surgical Strikes” challenge

I've just recently completed this programming "challenge" called surgical strikes: $N$ Indian Air Force fighter planes are located in different bases across the country. Each airbase is described ...
183 views

Primality test in Python

Here is a working Python implementation of primality test. Is there something that I could change in code to achieve a better running time? For more information about algorithm see this post. ...
21 views

Solidity: verifying primality certificates, bounty for large primes

This is my first smart contract, and it isn't finished yet, but the remaining bits are complicated enough that I'd like to make sure I'm on the right track first. My concerns are: At line 117, is the ...
102 views

Calculate the number of primes in a range

I wrote some code to return how many prime numbers are in a specific range and what they are. It also tells you whether the number of primes is also a prime! I feel like this seems like a fairly long ...
66 views

Computing the first n primes: an example from Clean Code revised for C++

I'm trying to improve on examples from Clean Code* while re-implementing them in C++. This time it's the Sieve of Eratosthenes prime-computing example from pp. 71-74. Below is the original adapted ...
78 views

Project Euler Problem #50 Consecutive Prime Sums

Problem: Which prime, below one-million, can be written as the sum of the most consecutive primes? At the moment my code runs for over 200 seconds and was wondering how I can find a way to bring that ...
114 views

A simple implementation of the principle of RSA encryption

Inspired by a Numberphile video I made a little program that shows the principles of RSA encryption and decryption. To calculate the keys I used the explanation in this link: rsa public private key ...
4k views

Python π = 1 + (1/2) + (1/3) + (1/4) - (1/5) + (1/6) + (1/7) + (1/8) + (1/9) - (1/10) …1748 Euler

I wrote this code to show that my reddit post is correct. After the first two terms, the signs are determined as follows: If the denominator is a prime of the form 4m − 1, the sign is positive; if ...
54 views

...
61 views

Project Euler - Problem 3

The description of the problem is: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? Here is my solution: ...
73 views

Project Euler problem # 3 is this the most efficient approach in Python or can I do better?

The problem statement: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? This code takes 9 seconds!, is there a better way? ...
58 views

Prime number sieve using ranges::view::ints

I wrote a simple prime number generator in C++17. The function generate_primes(max) generates all prime numbers up to max. I ...
78 views

Optimizing primes generating program in C++

I am writing a program to generate primes in range 1 upto 10^9 but it seems to be timing out. It implements the Sieve of Atkin I have tried optimizing my code using bitsets and pragmas. Please are ...
121 views

Sum of primes up to 2 million using the Sieve of Eratosthenes

I've solved question 10 on Project Euler using the Sieve of Eratosthenes, what can I do to optimize my code? ...
66 views

Parallel Miller-Rabin Prime Test in Haskell

I implemented a prime number finder using the Miller-Rabin prime test in Haskell and it seems to be working. You enter a number and it finds the next prime following that number. I also tried to make ...
2k views

Prime sieve in Python

I was trying to write Sieve for the first time and I came up with this code: ...
831 views

Primes and SemiPrimes in Binary

I'm in the midst of re-learning python. My current effort is an implementation around prime factorization. I am attempting to reproduce something similar to the table found here (in binary): https://...
382 views

Find the nth term of a sequence that consists of Fibonacci and prime numbers interleaved

I have given a series problem 1,2,1,3,2,5,3,7,5.... in which the odd terms forms the Fibonacci series and even terms form the prime number series. I have to write a code in which I have to find the ...
1k views

Arranging numbers in a circle such that the sums of neighbors and sums of diametric opposites are prime

I need to create a circle with numbers from 1 to n , however it is necessary to respect some rules: All numbers from 1 to n are used in the circle only once. The sum of two consecutive numbers ...
58 views

Prime Sieve and brute force #2

Based on my previous attempt. Prime Sieve and brute force Trying to make the interface simpler I have moved all the code into a class. This way I can keep track of the ...
1k views

Prime Sieve and brute force

Got caught up with this morning's C++ question. So I thought I would have a go. SO here is my attempt at printing the first 1001 primes. ...
110 views

Fast prime generator

I wanted to test my performance optimizing skills, and so wanted to find how fast I could get the first $n$ prime numbers. I limited myself to only the standard library, as I'm sure ...
81 views

Simple prime factorization program in Haskell

I'm finally learning Haskell this summer and I have a few questions about conventions. I made a simple prime factorization program which is surprisingly fast, much faster than the ones I made in C++ (...
112 views

Decompose any number to its prime factors raised to their respective powers

A simple code which takes the given number as input and gives all of its prime factors raised to their respective powers...works well upto 106 after that it slows down greatly for some numbers..but ...
144 views

Project Euler #357: Prime generating integers

Project Euler Problem 357 asks: Consider the divisors of 30: 1,2,3,5,6,10,15,30. It can be seen that for every divisor d of 30, $$d+30/d$$ is prime. Find the sum of all positive integers n ...
66 views

Goldbach conjecture solution finder

The Goldbach conjecture says: any even number greater than two can be represented by the sum of two prime numbers. G(n) = { (p1, p2) | p1 + p2 = n, p1 and p2 are prime numbers with p1 < p2 } ...
157 views

Hackerrank: Computer Game (max-flow problem with integer factorization)

I am working on a coding challenge from the Hackerrank site. Given two equal-length arrays of integers, with values from 2 to 109, find the maximum number of times we can remove a pair (Ai, Bj) where ...
89 views

A faster way to compute largest prime factor (Again, but a lot better)

I came up with this code after implementing the feedback given by @200_success. The previous one (A faster way to compute the largest prime factor) tested my patience, but still failed to give the ...
2k views

A faster way to compute the largest prime factor

I am self-learning js and came across this problem(#3) from the Euler Project The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? ...
55 views

Segmented Sieve Spoj

I tried(trying...) to solve the SPOJ prime number problem PRIME1 so i learned and implemented Segmented Sieve correctly but this program works in time for 10^8 but getting Time limit exceeded (TLE) ...
209 views

Determining primality by removing select composits

Calculating many remainders for each prime and composite consumes more time. I use a factoring function to find factors of single values. When I use a factoring function to find lists of primes, I ...
196 views

Efficient Prime Number Generator in c++

I am trying to make an efficient prime number calculator. I've seen ones that can go up to close to 1 trillion in right around 4 hours, however I left mine on overnight and didn't get anywhere near ...
428 views

Prime factors and perfect square

I want to convert an integer to a perfect square by multiplying it by some number. That number is the product of all the prime factors of the number which not appear an even number of times. Example ...
74 views

Generate two random primes and find their totient

I am supposed to make a basic RSA encryption/decryption code, where I am only given e. I wrote this code to randomly generate two primes and then find their totient. But the totient is supposed to ...
226 views

1k views

The next prime number in Python

I have written a solution to a DM::OJ exercise to find the next Prime Number after a given number (input). I'm seeking to optimize it so that it will run for any number up to ...
221 views

Kattis prime reduction challenge

I am solving the Prime Reduction challenge on Kattis. Consider the following process, which we’ll call prime reduction. Given an input x: if x is prime, print x and stop factor x into its ...
150 views

Faster Sieve of Eratosthenes

This is an implementation of the Sieve of Eratosthenes : It takes advantages of the fact that all primes from 5 and above can be written as 6X-1 or ...
67 views

Euler Project #3 Largest prime factor timeout

I have been recently trying my hands on Euler's Project through HackerRank and got stuck with project #3 where you have to find the Largest Prime Factor. Question: The prime factors of 13195 are 5, 7,...
51 views

Find primes that are also primes when its digits are rotated

The program below is meant to find all of the fare prime numbers under a user specified value. A fare prime being a number that when its digits are rotated, each combination makes a prime number. An ...