Questions tagged [primes]
Primes or prime numbers are integers greater than 1 which are divisible only by themselves and 1, i.e.: 2, 3, 5, 7, 11, ... .
3,196
questions
2
votes
2
answers
36
views
prime numbers in python same programs different outputs
I have this assignment where I have to write a code to determine if a number is a prime number or not and I couldn't come up with a solution so I searched the net and found the following code( the ...
0
votes
2
answers
48
views
Python program to find all prime numbers in the range 1 - n
Hi I know there are lots of solutions to this problem but I wanted some help finding out why my answer is wrong.
Here's my answer:
number = int(input("enter a number: "))
for n in range(2, ...
-1
votes
0
answers
40
views
How is "for (int div = num - 1; div > 1; div--)" working [closed]
I have this line of code, which I tested and works as intended every single time.
int count = 1;
int num = 1;
while (count < 100) {
num++;
for (int div = num - 1; div &...
-1
votes
0
answers
16
views
Why is my code returning all numbers as prime? [duplicate]
So I am currently doing a challenge, which is to print all prime numbers from 1-100. I'm not sure whether what part of my code is wrong, as for me the logic that I am implying seems to be right. But I ...
-4
votes
0
answers
47
views
Check if a number is a permutable prime [closed]
A permutable prime is a prime number that remains prime on every rearrangement (permutation) of the digits. For example 13 (because 31 is also a prime) or 113 (because 131 and 311 are also primes).
...
-1
votes
1
answer
67
views
Array of prime numbers in C# [closed]
I want to write code that populates a 15-element array with prime numbers from the smallest prime number. But I couldn't go beyond the code below. I need help debugging this code.
static void Main(...
-2
votes
1
answer
32
views
Clarifying "if all(...)" line: Project Euler #7 - Python [duplicate]
Thanks to this video I managed to further optimize the time for my code for Problem 7 on Project Euler. However, as someone who's getting more familiar with Python, I want to understand line 105 in ...
0
votes
2
answers
41
views
Find an element in a matrix M*M of numbers which have exactly 3 divisors in given time?
This was a task given on a coding competition a few months ago, and its still bugging me now. I did solve it, but my code did not give the result in required time for 3 test samples, likely due to the ...
0
votes
2
answers
65
views
Recursive Function - Is Prime
What's the logic behind if n < i*i: return True?
def isPrime(n, i = 2):
# Base cases
if (n <= 2):
return True if(n == 2) else False
if (n % i == 0):
return False
...
1
vote
1
answer
44
views
How can check if the digits of a number are prime and if so multiply them in C
The problem says that the user will input numbers till he inserts 0 (exit) then if there are any prime digits in that given number, the program shall multiply them.
For example:
input : 4657
output : ...
-1
votes
1
answer
33
views
My code is saying that number 1 is prime. How to fix that?
So,
I'm starting to learn how to code and now I have only one problem.
My code is functioning very well, until he says that the numbers 1 and 0 are prime.
I already tried to add them to exception, ...
2
votes
2
answers
99
views
Haskell list recursion - why does one work and not the other?
Anyone familiar with Haskell will have likely seen this implementation of the Fibonacci sequence.
fibs = 0:1:zipWith (+) fibs (tail fibs)
The way I understand this is as follows:
Haskell's lazy ...
-2
votes
2
answers
33
views
Nextprime program working fine for almost all values but glitching at 83, 84, 89 to 92 (so far)
n = int(input('Enter a value : '))
for p in range(n,n+999):
p = n+1
if p > n:
for i in range(2,p):
if p % i == 0:
p = p+1
else:
...
1
vote
1
answer
53
views
How can i multiply the prime numbers of a number from user input and display it in C language
So I started learning C language for Uni and got stuck with this exercise, I found a way to ge the prime numbers of a number but I don't know how to multiply the prime numbers and display them.
int ...
-1
votes
1
answer
78
views
Project Euler #10 [C++] sum of primes below 2000000
Prompt: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million.
Code:
#include <iostream>
#include <list>
/**
* @brief Searches for ...
-1
votes
0
answers
29
views
Fast way to find first low delta for big modulus
N=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
L= 0x5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72
maxX=0x14551231950b75fc4402da1722fc9baee
The main term is:
X *...
0
votes
0
answers
29
views
Why does this works as string when it should only work as number?
So I was putting this code from an exercise on node.js in a format to front-end javascript so I could upload like a webpage. It checks if the number is prime or not. The code is:
let num = 6
let ...
1
vote
1
answer
85
views
how to check among big prime numbers for condition
Out of prime numbers less than 201920190 how to find those that dont have digit '7' in their str representation? I need to count the number(count) of such prime numbers that have no '7' in them.
Std ...
1
vote
2
answers
46
views
Prime number function loop mistake [closed]
I have been told that this code for finding a prime number is wrong. Could anyone explain why? It works for at least the first 100 numbers.
if x<2:
return 'It is neither prime nor ...
0
votes
1
answer
33
views
Sieve of Eratosthenes with nested for loops
This is a very simple question about python. I was trying to get a list of prime numbers so I tried
primes = [2]
for i in primes:
for j in range(50):
if j%i != 0:
primes.append(j)
...
1
vote
2
answers
297
views
Python: How to get Prime Numbers from string using Permutations and Combinations
Complete the following function to return a list of strings lesser than 999 which form prime numbers from a given string of numbers.
For example if the given string is “23167”. All the prime numbers ...
0
votes
1
answer
20
views
I'm getting a weird error for printing n prime numbers
def prime(n):
if n==2 or n==3:
return True
if n==1:
return False
f=True
for i in range(2,int(n**0.5)+1):
if n%i==0:
f=False
break
...
0
votes
1
answer
56
views
What is this for loop doing? I don't understand the functionality of the for loop here
This is my code to print all the prime numbers between 1 to 100; I need to use arrays for this and this is important part of the challenge:
#include <stdio.h>
#include <stdlib.h>
#include &...
1
vote
0
answers
82
views
How to make an efficient prime generator in python using wheel factorisation
I've been trying to make an efficient prime generator that successfully utilises wheel factorisation to be faster than a basic sieve of Eratosthenes implementation but my code ends up being a lot ...
1
vote
0
answers
28
views
MIPS Prime number factors
I am trying to write a program in MIPS, where you enter a number and it tells you whether it is prime or not, and if it is prime it also gives all the prime numbers up to it. Ex: n = 11, and it will ...
0
votes
1
answer
42
views
Prolog - Display prime numbers
I am new to Prolog programming so still learning. I'm tryingto create a program that accepts a value X and returns a list of all prime numbers between 6 and X. I'll also be later adding an error ...
1
vote
2
answers
39
views
What can I do to add a list and sort out all the prime numbers in the list? Python 3
I am creating a program that
accepts an inputted list
finds all the prime numbers and only displays them.
I tried many different methods, many derived from existing prime filters, but they have ...
1
vote
1
answer
112
views
Prime Factorization in Haskell to return a list of tuples giving the number and the power
I have been trying to learn haskell by trying to do some simple problems.
The Problem
Currently, I am trying to implement a function primeFactorization :: Integer -> [(Integer, Integer)] such that ...
-2
votes
1
answer
57
views
Prime-checking every element of a 10^6 array
My goal is to figure out whether each element of an array is a prime or not.
Example:
Input: int A[5]={1,2,3,4,5}
Output: bool P[5]={0,1,1,0,1}
The problem is the array size is up to 10^6. I tried ...
0
votes
0
answers
56
views
Double Segmented Primes Sieve
In cases of low memory available I thought it was possible to implement a double-segment sieve to find prime numbers in a neighborhood of n. In practice, a segmented sieve has dimension dim_seg_1 for ...
0
votes
1
answer
35
views
Exception thrown: read access violation this. was nullptr [duplicate]
I tried to work with the points but nothing I do seem to work.
Essentially this will be run in main and a number n is inserted to find all the prime numbers from 0 to n. This is then written into a ...
-1
votes
1
answer
35
views
How can I fix "SyntaxError: invalid error" for function is_prime? [closed]
There is a syntax error in line 3. I'm unsure of how to correct the error.
Write a python function is_prime() which takes one integer as an argument and returns True or False (boolean values). If the ...
0
votes
1
answer
120
views
Calculate the number of Primes that are less than the given Number [closed]
I'm getting desired output, but I'm not able to submit the code on the Leetcode.
The link of question on the Leetcode
Problem statement:
Given an integer n, return the number of prime numbers that ...
1
vote
2
answers
54
views
Prime number check doesn't work as it should
I'll try to keep this short. So my task is to find the last prime number in an array, if there's any. But right now my program assigns any number it wants from the array to the lastPrimeNumber ...
0
votes
0
answers
16
views
How to entrust primality test to unreliable clients?
Let's think about a two player game where each player should say distinct prime numbers every turn. If you repeat a prime previously said or say a composite number, you lose. Let's call this Prime ...
0
votes
3
answers
168
views
Efficiency of a prime number finding program(c++)
I'm wondering if there is something inefficient in this code I've made, or if there is a faster way to find prime numbers.
#include <stdio.h>
int main(void)
{
int count;
for(int i=3; i&...
-4
votes
2
answers
75
views
check if number is prime using recursion with only one parameter in c language [closed]
I've got a task in my lesson to write a function that will check if a number is prime, it will return 1 if it is and 0 if it is not and all of that to do recursively. the function will look like that :...
0
votes
1
answer
68
views
multi threaded program - core dumped
I'm trying to make a multi-threaded prime numbers counter program. I've added the critical codes for the understanding (the parseargs function works great in another program and feels unnecessary and ...
2
votes
1
answer
57
views
Algorithm for calculating primes in an interval not working
I am trying to come up with an algorithm that can find all primes from a starting to an ending number, without starting at 2 and using the Sieve of Eratosthenes to calculate all primes to the ending ...
0
votes
1
answer
38
views
To generate laundau's prime numbers using python
I want to generate list of primes of the format x2+1 but I am not getting any output.
My code:
def LPrime(n):
for i in range(1,n):
x = i**2+1;
for j in range(2,x):
if ...
0
votes
1
answer
79
views
Does the size of the prime number in Shamir's Secret Sharing affect the security of the sharding?
I've been working on an implementation of Shamir's Secret Sharing, and was wondering if the prime number selected will impact on the security. This is mainly because I've seen some implementations on ...
0
votes
2
answers
113
views
How to display all prime numbers that are less than or equal to the number a user enters
#include <iostream>
#include <vector>
using namespace std;
int main() {
bool is_prime = true;
vector<int> num;
cout << "Hello please enter a number larger ...
0
votes
3
answers
103
views
Storing numbers larger than Big integer C#
I am having a really hard time finding a way to store massive prime numbers in c#. I tried everything but nothing worked out for me. For example. How can I store this number.
...
1
vote
2
answers
64
views
Checking whether space should be printed in the middle or at the end in for loop?
I am trying to give info to my program whether it should print space or not.
My code looks something like this, and its printing spaces at the end (which is not what I want)
void function(int ...
0
votes
0
answers
15
views
Why function is executed prior to printing of string in Python? [duplicate]
I'm executing this code:
#Printing a list of prime numbers till an input number:
def is_prime(input):
for counter1 in range(1,input):
if(counter1==2):
print("Prime number: ...
0
votes
1
answer
42
views
How to make a prime decomposition function faster?
I want to make a prime decomposition function faster, but it needs to use a precomputed list of primes. This is what I have so far:
def decompose(n):
factors = dict()
m = n
while m > 1:
...
2
votes
2
answers
92
views
Getting the prime numbers till 10000 in fortran?
Im trying to print prime numbers till 10000. (display the first five for testing)
This is my program
program primes
implicit none
integer :: array(1229)
integer :: i, ind
logical :...
0
votes
2
answers
28
views
Run recursion to divide number into largest possible parts until it reaches smallest whole value
I need to take a number as input, and continuously run recursion of my function until the number is the smallest prime number the original value can be divided by. I want to divide the value by the ...
0
votes
0
answers
46
views
How do I find the median of all prime number(s) within a range?
For example, I want to find the median of all prime numbers between 9,999 and 100,000. I've done things like find the highest or lowest prime number in arrays but I've never found the median prime ...
0
votes
0
answers
100
views
primality test function is_prime (n, k) based on Fermat Little Theorem,
I solved this question: Implement a primality test function is_prime (n, k) based on Fermat Little Theorem, where n is the number to be tested, and k is the number of bases to be used. The function ...