Skip to main content

All Questions

Tagged with
9 votes
6 answers
4k views

Goldbach’s Conjecture in Python

Input Format Input starts with an integer n (1 ≤ n ≤ 100) indicating the number of cases. The following n lines each contain a test case of a single even number x (4 ≤ x ≤ 32000). Output Format For ...
4 votes
1 answer
564 views

Prime factorization algorithm by using the multiprocessing module of Python

I may introduce a Python code of prime factorization which is from my personal project that I'm working on. ...
6 votes
1 answer
921 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. It works well up to 10⁶; after that it slows down greatly for some numbers. ...
2 votes
2 answers
348 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 ...
8 votes
1 answer
3k views

Project Euler 407: Is there any more optimal way to solve this idempotent equation (modulo n ring)?

Project Euler problem 407: If we calculate a2 mod 6 for 0 ≤ a ≤ 5 we get: 0, 1, 4, 3, 4, 1. The largest value of a such that a2 mod 6 = a is 4. Let's call M(n) the largest value of a < n ...
4 votes
4 answers
221 views

My prime number generation program in Python

So I did my own take (I wrote my own algorithm) for generating prime numbers from 1 to 1000. ...
1 vote
1 answer
54 views

Python Prime Search Slower Optimization

I have these 2 version of code. Both are looking for all primes under a certain ceiling value. The first is testing against the set of all odd numbers. The second in testing against the set of all 6k-...
6 votes
2 answers
104 views

Finding consecutive primes matching p, p + 4, p + 6, p + 10, p + 12, p + 16

Someone asked about this question on the main StackOverflow. The full question is: Given a value N, find p such that all of ...
12 votes
3 answers
5k 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 ...
5 votes
2 answers
1k 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 ...
3 votes
4 answers
3k views

Sieve of Eratosthenes: making it quicker

I was thinking about doing problem 23 of Project Euler. It includes the difficulty where you have to factorize primes. I had already done that in problems I solved earlier, but it was only necessary ...
14 votes
3 answers
8k views

Tonelli-Shanks algorithm 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: ...
14 votes
4 answers
2k views

Codechef: Prime Number Generator

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number t of test cases ...
5 votes
1 answer
1k views

Sieve of Atkin in Python

I recently implemented the Sieve of Atkin prime generating algorithm in Python. Though I know the term "pythonic" isn't exactly set in stone, I can tell that my program doesn't quite take advantage of ...
7 votes
4 answers
1k views

Python Prime Testing

This code seems surprisingly fast at checking if a number is prime. ...

15 30 50 per page