primes

How do I find a prime number using recursion in Python

寵の児 提交于 2020-06-27 16:30:10
问题 I have to find out whether number(N) is a prime or not using recursion, no loops are allowed. I've tried converting the usual code that uses a for loop to a recursive one, but it's not behaving the same. This function is included in another function, which is part of another function. only parameters a and N should be used and passed Here is my function. a=2 def is_prime(a,N): prime = True if N <=1: return else: if a >= N: return else: if N == 2: prime = True print(N) return elif (N % a) == 0

Finding all Coprime subset upto a number N

旧巷老猫 提交于 2020-05-08 19:48:25
问题 Suppose I have numbers 1 to N and I want to divide them into subsets based on following criteria: Each number can be present in only 1 subset. The elements of the subsets must be mutually coprime. Minimizing the total number of subsets. My approach to it is by finding all primes up to N using Sieve of Eratosthenes and then dividing them accordingly in subsets. For example for N=5, I can have two subsets at minimum {1,2,3,5} and {4}. But I am unsure how to distribute the elements in subsets so

Why does my sieve not perform well for finding primes?

故事扮演 提交于 2020-03-22 09:05:15
问题 I wrote two prime finder functions and the sieve only performs about 10% better. I'm using two optimizations for the simple version. Don't check even numbers Only check up to the square root or j * j <= i . ( equivalent ) and one optimization for the sieve version Only check up to the square root or i * i <= n . ( equivalent ) What optimizations can I add to the sieve? My sieve is pretty slow. I don't want to do a bitwise implementation yet, I want to understand if this implementation offers

threads in Java and computation

懵懂的女人 提交于 2020-02-05 07:20:52
问题 I am new to java, and I am trying to write a program that takes two parameters: the number until which we have to sum prime numbers the number of threads in which we have to do this So I use a method named Eratosthene that stores an array of boolean and if a number is prime, we mark it true and after that we mark all the multiples of this number false. I try to devide my array into sub arrays for each thread and do the operation in each sub arrays, and at the end sum all the results of sub

Prime factorization of a big number

时光毁灭记忆、已成空白 提交于 2020-02-05 06:06:04
问题 I'm trying to create a program for prime factorization of a number and this is the code I came up with. def primeFactors(n): l=[] ss=0 for i in range(2,n,1): #checking for prime t=0 for j in range(2,i): if(i==2): continue if(i%j==0): t=t+1 if(t>0): continue else: if(n==0): break else: print(i) if(n%i==0): n=n//i ss=ss+1 i=i-1 if(n%i!=0 and ss>0): l.append(i) l.append(ss) ss=0 else: continue q="" for i in range(0,len(l),2): q=q+"("+str(l[i])+"**"+str(l[i+1])+")" return q The working of the

finding nth prime in python

一个人想着一个人 提交于 2020-01-30 13:28:26
问题 I wrote the following segment of code in Python to find the nth number. I don't understand why it doesn't work. Can you please only give me a hint or point out exactly which bit is messing it up rather than a complete solution. term = int(input("What prime do you want to find? ")) prime_list=[2] def prime_search(term): x=3 while len(prime_list) <= term: if all(x % y != 0 for y in range(2,x)): prime_list.append(x) x += 1 return prime_list[term-1] prime_search(term) 回答1: Your dont print

finding nth prime in python

馋奶兔 提交于 2020-01-30 13:27:34
问题 I wrote the following segment of code in Python to find the nth number. I don't understand why it doesn't work. Can you please only give me a hint or point out exactly which bit is messing it up rather than a complete solution. term = int(input("What prime do you want to find? ")) prime_list=[2] def prime_search(term): x=3 while len(prime_list) <= term: if all(x % y != 0 for y in range(2,x)): prime_list.append(x) x += 1 return prime_list[term-1] prime_search(term) 回答1: Your dont print

Finding the nth number of primes

倾然丶 夕夏残阳落幕 提交于 2020-01-25 07:39:25
问题 I can not figure out why this won't work. Please help me from math import sqrt pN = 0 numPrimes = 0 num = 1 def checkPrime(x): '''Check\'s whether a number is a prime or not''' prime = True if(x==2): prime = True elif(x%2==0): prime=False else: root=int(sqrt(x)) for i in range(3,root,2): if(x%i==0): prime=False break return prime n = int(input("Find n number of primes. N being:")) while( numPrimes != n ): if( checkPrime( num ) == True ): numPrimes += 1 pN = num print("{0}: {1}".format

Use of BigInteger.isProbablePrime() to generate cryptographically secure primes

纵然是瞬间 提交于 2020-01-24 20:41:06
问题 Can you use BigInteger.isProbablePrime() to generate cryptographically secure primes? What certainty is necessary for them to be "secure"? 回答1: I do not hold a degree in crypto, so take this with a grain of salt. You have two major areas of concern here: Your primes need to be unpredictably random. This means that you need to use a source such as SecureRandom to generate your primes. No matter how sure of your primality, if they are predictable, the entire cryptosystem fails to meet its goal.

Forcing variable to reassign (Prolog)

你说的曾经没有我的故事 提交于 2020-01-24 12:54:26
问题 The homework is to take in two variables, a number between 0 and 10,000 and a number of how many circular primes there are between 1 and that number. I am having trouble passing the variable back up through the recursion (backtracing is what it is called, I think.) I get the right number and I am pretty sure I have the concept down, the problem I am having is that it throws an error when I try to reassign a variable (?!) Here is the code: circPrimeCompare(Below, NumCirc):- RealNum is 0,