3,375 questions
2
votes
2
answers
139
views
Looking for proportion of prime numbers in a list, error in code
I'm trying to solve this Project Euler task which is to find the point at which the proportion of primes in a list drops below a certain percentage (the diagonals in this spiral of numbers below).
I ...
-2
votes
1
answer
38
views
Maximize Score based Prime Score Algorithm
Consider the following LeetCode problem:
You are given an array nums of n positive integers and an integer k. Initially, you start with a score of 1. You have to maximize your score by applying the ...
0
votes
3
answers
379
views
Most-time-efficient algorithm for generating primes contiuously until halted?
What is the most efficient (time complexity not space) algorithm to generate all continuous prime numbers until the algorithm is halted? There are infinite prime numbers, the algorithm will be halted ...
3
votes
0
answers
180
views
How to efficiently compute the exact value of prime counting function for n? [closed]
I want to know the exact count of prime numbers no greater than n, without finding all these prime numbers first.
I know of a very efficient method to find all primes no greater than n, using Sieve of ...
2
votes
0
answers
152
views
Simplify a prime factorization-like expression with optimal performance [closed]
I am working with a situation in which I need to turn an arithmetic string expression (e.g. (3/2) * (3**4 / (20 * 4))**(-1/4)) into the following prime factorization form: $\prod_{p:\text{prime}}p_i^{...
0
votes
1
answer
109
views
How to make Excel do a different operation in A2 depending on what type of number A1 is?
These operations take priority:
If A1 Prime: subtract previous prime (if 1 treat as odd number)
If A1 Perfect Square: square root
While these are the conditions if those aren't true:
If A1 Even: ...
-2
votes
3
answers
285
views
From 1 to n, count how many integers there are with the sum of divisors being a prime number? [closed]
Let there be integer n.
Count how many numbers there are from 1 to n that has the sum of divisors being a prime number.
Example:
Input: 10
Output: 3
Explanation: There are 3 numbers with the sum of ...
2
votes
1
answer
185
views
How to create possible sets of n numbers from m-sized prime number list?
Input: a list of m prime numbers (with possible repetition), and integers n and t.
Output: all sets of n numbers, where each set is formed by partitioning the input into n parts, and taking the ...
2
votes
1
answer
211
views
Optimizing The Exact Prime Number Theorem
For example, given this sequence of the first 499 primes, can you predict the next prime?
2,3,5,7,...,3541,3547,3557,3559
The 500th prime is 3571.
Prime Number Theorem
The Prime Number Theorem (PNT)...
0
votes
3
answers
86
views
Function to find prime factors overwrites parts of its results
I'm trying to make an algorithm that shows what primes factors a number is made of. I have made the following code (not optimized) which divides a number into the 2 numbers it's made of. Then I do ...
3
votes
0
answers
108
views
How to use list comprehension in the Sieve of Atkin algorithm? [closed]
I am writing a Python program to find a list of prime numbers.
I wrote it using the Sieve of Eratosthenes:
def Eratosthenes(n:int) -> list[int]:
if n <= 2:
return []
l = list(range(3,...
1
vote
0
answers
64
views
Caching the Sieve of Erathosthenes
I have the following code which generates all the primes numbers below n
def sieve(n):
# sieve of Erathosthenes to generate prime numbers
sieve = [True]*(n//2)
for i in range(3, int(sqrt(n)...
2
votes
1
answer
140
views
Prime-k solution doesn’t go above certain number in c++
Im currently trying to solve the prime-k factor problem, where I have to find the sum of all primes factorial 1-5 ! and then mod(%) to itself. An example would be the prime 7.
(7-1)!+(7-2)!....(7-5)!=....
-2
votes
1
answer
125
views
Finding next prime number lands in an execution error (time limit exceeded) [closed]
Does anyone have any idea on why the program may crash? The input is prime numbers, and you have to search the next one. If you input a non-prime, it stops. Thanks.
#include <iostream>
using ...
0
votes
0
answers
65
views
Can anyone explain why this RSA calculation not working
I made a RSA encrypting and decrypting program.
Everything works well as it is suppose to be. However when I decrypt the message, it doesn't give me the message.
Can somebody check which part is wrong ...
-1
votes
2
answers
104
views
Sieve of eratosthenes python script outputs non prime numbers
I am making a python (3.10) program that counts all prime numbers and adds them to a list (already containing 2)
and somehow This program outputs 16 and 50 in the list (both divisible by 2 so not ...
4
votes
9
answers
1k
views
Wrote a program to print prime numbers, How do I make it better?
I started learning C the previous day and I wrote a program to print within a given range. Minimum and Maximum of range is given by user.
#include <stdio.h>
int check(int number)
{
if (...
-1
votes
1
answer
94
views
How can I improve my Python code for checking if a number is prime؟ [duplicate]
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i =...
-2
votes
2
answers
148
views
Why does my script stop execution after 33178210th iteration? [closed]
I am writing a python program to find and return a list of Twin primes between two numbers.
This is my code:
#prime selector
def is_prime(num):
if num < 2 or num % 2 == 0:
return False
...
-1
votes
1
answer
213
views
write a programme in c to skip 2 elements after occurence of a non prime
I write the code as my knowledge but i didnt get the correct ouput.i provide my code below
#include <stdio.h>
int main() {
int i, j, isPrime, n, a[1000], k;
// Input array limit
...
3
votes
1
answer
143
views
Primes using setjmp
I wrote a simple Haskell function, that gives a list of primes.
primes' :: [Int] -> [Int]
primes' (p : xs) = p : primes' (filter (\x -> x `rem` p /= 0) xs)
primes :: [Int]
primes = primes' [2 .....
-1
votes
7
answers
375
views
What is the error in my Prime number code?
import java.util.*;
class PrimeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int d = 2;
while(d < n) {
if(...
1
vote
1
answer
88
views
Iteration of the output in simple prime number
I'm trying to find out whether a given number is prime or not. For example, when I enter the input as 5, it should output "5 is a prime".
However, I get multiple, conflicting outputs.
This ...
4
votes
1
answer
147
views
Algorithm to compare multiplications of powers of primes
Is there an algorithm to compare two numeric expressions that follow these rules:
The expressions are multiplications of powers.
Every power is a power of a prime number.
The two expressions do not ...
0
votes
0
answers
46
views
How to find a prime that is larger than some constant value and what it's time complexity
I ask this in the context of hashing where a prime greater than some constant is required
for example in universal hashing:
h(k) = [(a.k+b) mod p] mod m
a and b are random numbers
m is the size of ...
3
votes
1
answer
130
views
Why this idea to generate the primes in Haskell doesn't seem to work?
I'm trying to use the Sieve of Eratosthenes' algorithm in Haskell to generate a stream of primes but the code doesn't seems to work. This is the main idea.
The main idea is derived from our functional ...
1
vote
3
answers
126
views
the judgement of the Prime number
#task:input a number ,then determine if it is a prime number.
1.question:how can I optimize my code ,and when I run this code ,I find that some prime numbers get the answer "No".
#include &...
0
votes
0
answers
56
views
Optimizing the code for find primes upto n using recursion
I had written a code for finding primes using recursion and this code needs some refining, I need somebody to help me in this case
#include<stdio.h>
int is_prime(int,int,int);
void ...
1
vote
0
answers
77
views
Optimization of Large "Primes in Range" solver?
I'm generating a large range of primes starting from a non-zero value.
e.g., Primes from 2^64 to 2^64+1,000,000. Due to not needing all primes from 0 - instead starting at 2^64 - a sieve is both ...
1
vote
1
answer
75
views
isPrime in ASM is always giving the same output
I've been trying to learn ASM and this is one of the problems I've been trying to solve.
The goal of the algorithm is to check if a number is prime. From what I could follow in my head this should ...
-1
votes
1
answer
139
views
Error finding and rectification of my C code for finding prime numbers
The error that I'm struggling, with some examples
I have written a code for finding prime numbers using sieve of Eratosthenes algorithm, but the problem is my code works as it tends to be, but it ...
1
vote
1
answer
111
views
How to make my pyspark code for sieve of eratosthenes scalable for large range of numbers?
I am trying to write a pyspark script to generate all the prime numbers <= a given number. For example, generate all the primes upto 1 billion.
I wrote the following code. It does well for small ...
3
votes
1
answer
137
views
Why is my Mersenne prime code faster with a larger exponent?
I am writing a program in Rust to check if large numbers are Mersenne primes for fun. For some reason, when I test the program with an exponent of 1_000_000_000 it takes around 5 seconds, but when I ...
1
vote
0
answers
64
views
Can someone explain why this is throwing a stackoverflow error? (Scala, lazylist, nth prime number)
The Scala snippet below finds the nth prime number up to the 6500th (indexed from 0) on my machine. Calling it with primes(6500) returns 65071. After this (say, primes(6501)), it throws a ...
0
votes
0
answers
50
views
Benchmark for an implementation of the Miller-Rabin-Primetest
I have an implementation of the Miller-Rabin-Primetest, coded in python3.
Unfortunately I have no idea how "fast" this test should be on my machine.
With my code I can test the number $n = 2^...
0
votes
1
answer
103
views
Optimising Sieve of Erastosthenes (Finding Primes over a million w/o memory error)
My code can only handle a range of about a million, whereas I need it to handle about 100 million to a billion.
I keep getting a memory error when I put in a range of 1 to 1 billion.
Is there any way ...
0
votes
2
answers
63
views
Enhancing Performance of Sorted Linked List for Truncatable Prime Numbers
I am currently engaged in solving Question 37 ("Truncatable Primes") on Project Euler. In essence, the task involves identifying 11 prime numbers with the unique property that, when any ...
-1
votes
1
answer
182
views
lock-free queue to count prime numbers in c performance
I try to use multi-threads with lock-free queues to do the best performance to count prime numbers without editing the naive prime check function and in a maximum of only 1.8MB of RAM space.
On the ...
0
votes
1
answer
162
views
Primes with exponent series
I was interested in finding out if I could compact a number using prime exponents. During my research into this, I came across several solutions one being the creation of a sequential series of primes ...
-4
votes
1
answer
327
views
Prime generating Python code using C.P. Willans formula
I wrote a prime generating code in Python, and it produces correct results. However when i want to print 8th prime I get an OverflowError:
sum += floor(pow(cos(pi * (factorial(j - 1) + 1) / j), 2))
...
0
votes
0
answers
130
views
How does the Radix-2 Montgomery multiplication algorithm R2MM work on bit level?
The following algorithm is for modular multiplication modulo a prime number p, xi represents the bit number "i" of the X input, Y is the second input.
i know how to implement this algorithm ...
0
votes
1
answer
120
views
Best way to find divisors of a number [duplicate]
I have a function that finds the prime divisors of a number and returns a list of its prime factors:
def prime_factors(n, listFact=[]):
end = floor(sqrt(n))
if n == 1:
return listFact
...
0
votes
1
answer
127
views
But does the Fermat Factorization algorithm work?
I was testing the basic version of the Fermat Factorization method, so I'm not using any improvement whatsoever. The code I wrote in Python works as fast as expected for those numbers obtained by ...
1
vote
3
answers
7k
views
Why does 2147483647 is the only Int that the code I wrote does'nt get the right feedback?
I wrote a code that finds if a number is prime or not.
for every prime number it prints the number + is prime
for every number that isn't prime it prints the number=the lowest divider*the highest ...
3
votes
2
answers
107
views
Alignment issue when printing formatted prime numbers in J language
I am a relatively new programmer in J-Lang and recently discovered its efficacy in Code Golfing, where it excels in scoring. I am currently working on a coding problem that involves printing all prime ...
3
votes
1
answer
159
views
Python nested lists search optimization [closed]
I have a search and test problem :
in the list of prime numbers from 2 to 100k, we're searching the first set of 5 with the following criteria :
p1 < p2 < p3 < p4 < p5
any combination of ...
0
votes
1
answer
397
views
SAP Abap code not working | Field "CHECK_PRIME" is unknown. | The method "CHECK_PRIME" is not declared or inherited in class "ZCL_NTH_PRIME". |
REPORT Z_PRIMENUMBER.
CLASS ZCL_NTH_PRIME DEFINITION FINAL CREATE PUBLIC.
PUBLIC SECTION.
METHODS prime
IMPORTING
input TYPE i
RETURNING
VALUE(result) TYPE i
...
3
votes
1
answer
159
views
Sieve of Eratosthenes: speeding up the "cross off multiples" step
I have implemented a function to list prime numbers using the Sieve of Eratosthenes algorithm as follows:
func ListPrimes(n int) []int {
primeList := make([]int, 0)
primeBooleans := ...
-2
votes
3
answers
278
views
In a sorted array of prime numbers arr find index i of the smallest number in arr such that arr[i] divides given number
Consider the following data:
arr = [2,3,5,7,11,13,17,19]
n = 100
Output of the given should be 0 since n % 2 = 0 and arr[0] = 2.
Since array is sorted I want to use that to my advantage and design an ...
0
votes
0
answers
44
views
Python: Trouble testing primality of a number using Sieve of Eratosthenes
I am trying to calculate the nth prime using the Sieve of Eratosthenes. For large values of n though, it would speed up the process to generate some lower bound, and then only sieve the values above ...