Prime numbers that are factors of a received integer - python

I've been trying to complete this assignment but I couldn't get what is asked, which is: in Python 3, Ask a user to enter an integer (1, 1000). Out of the list of the first prime numbers 2,3,5,7, print those prime numbers that are factors of the received integer.
I hope could help me to get this.

def get_primes(n):
out = list()
sieve = [True] * (n+1)
for p in range(2, n+1):
if (sieve[p]):
out.append(p)
for i in range(p, n+1, p):
sieve[i] = False
return out
def get_factors(n):
output = list()
for i in range(1, n + 1):
if n % i == 0:
output.append(i)
return output
# input_number = input('Enter a number')
# input_number = int(input_number)
input_number = 30
primes = get_primes(input_number+1) # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
factors = get_factors(input_number) # [1, 2, 3, 5, 6, 10, 15, 30]
prime_factors = list()
for i in factors:
if i in primes:
prime_factors.append(i)
print(prime_factors)
Output:
[2, 3, 5]
Code for getting prime numbers:
Print series of prime numbers in python

Related

fixing Fibonacci numbers from a range of 1 to 50 Python

I have a set of numbers from 1 to 50 and from these numbers I have to find which are Fibonacci number, after that I have to find which of these numbers contain the number 1 and 2.
For example in my code my Fibonacci numbers are 0, 1, 2, 3, 5, 8, 13, 21, 34 I have written a code, but I can't add the last part of the question, so it finds out which of these numbers contain a number 1 or 2 in them.
here is my code
def isPerfectSquare(x):
s = int(math.sqrt(x))
return s * s == x
def isFibonacci(n):
return isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4)
for i in range(0, 51):
if isFibonacci(i) == True:
print(i, "is a Fibonacci Number")
else:
print(i, "is a not Fibonacci Number ")
for q in range(1, 51):
if isFibonacci(i) == True and '1' in i and '2' in i:
print(q)
else:
print('Error')
the result is something like this is gives me all of these numbers 0, 1, 2, 3, 5, 8, 13, 21, 34 as Fibonacci and the rest are not which is perfect but then it keeps giving me error, what i wanted to do is it to write a loop that contains all Fibonacci numbers like these 0, 1, 2, 3, 5, 8, 13, 21, 34 and that contain a 1 and 2 in the number to be printed out but it just prints everything as Error.
for q in range(1, 51):
if isFibonacci(q) == True and '1' in str(q) and '2' in str(q):
print(q)
else:
print('Error')
This loop's variable is q, not i.
You have to convert the number to string using str(...).

How to make a function that returns all integers that are multiples of either parameters n1 or n2 within the list n

The 3 parameters: list of integers (n), integer number (n1), and another integer number (n2), not including 0
I have:
def hw(n, n1, n2):
multiples = []
for i in n:
if i % n1 == 0 and i % n2 == 0:
return multiples
which is wrong and not even returning anything. I'm not sure where I went wrong, though?
the test script:
sol= hw(np.arange(20), 3, 4)
assert sol==[3, 4, 6, 8, 9, 12, 15, 16, 18]
With return you just return from the function in the very first iteration, you need to append to the list and return the list outside of the loop.
def hw(n, n1, n2):
multiples = []
for i in n:
if i % n1 == 0 or i % n2 == 0:
multiples.append(i)
return multiples
Also, use or instead of and if you need multiples of 3 or 4.
This is how you can do it using list comprehension:
def fun(n, n1, n2):
multiples = [i for i in n if i % n1 == 0 and i % n2 == 0]
return multiples
res = fun([18, 23, 21, 42, 3], 3, 7)
print(res)

Iterate through list and find prime numbers and add to another list

I want to iterate through a list in python, detect prime numbers and then add them to another list.
primes = []
nprimes = []
for j in range(0, len(arr)):
num = arr[j] #my list with numbers to check
if num > 1:
for k in range(2, num):
if (num % k) == 0:
nprimes.append(num)
break
else:
primes.append(num)
else:
print(num, " can't be checked, because its smaller than 1")
I have the problem that numbers are always added which are not prime numbers. Also in general the code does not seem to work properly.
If num % k == 0 is false you can't say it's prime directly you have to wait the whole loop, so move the else with the for loop, it'll be executed of no break has been encountered which means it's prime
you may iterate directly on the values for num in arr
you can stop your loop at sqrt(num) you won't find a new divisor after that
for num in arr:
if num > 1:
for k in range(2, int(num ** 0.5) + 1):
if num % k == 0:
nprimes.append(num)
break
else:
primes.append(num)
else:
print(num, " can't be checked, because its smaller than 1")
For learning purposes, let's play with a different approach. First, I recommend you move your trial division code into its own predicate function is_prime(), that returns True or False, so it can be optimized independently of the rest of your code.
Then, I'm going to have itertools.groupby divide the list into prime and non-prime sequences that we splice onto the appropriate lists:
def is_prime(number):
if number < 2:
return False
if number % 2 == 0:
return number == 2
for divisor in range(3, int(number ** 0.5) + 1, 2):
if number % divisor == 0:
return False
return True
if __name__ == "__main__":
from random import sample
from itertools import groupby
array = sample(range(1, 100), 15)
primes = []
composites = []
for are_prime, numbers in groupby(array, is_prime):
if are_prime:
primes.extend(numbers)
else:
composites.extend(numbers)
print("Numbers:", array)
print("Primes:", primes)
print("Composites:", composites)
OUTPUT
% python3 test.py
Numbers: [91, 87, 10, 2, 11, 24, 21, 12, 46, 61, 15, 32, 57, 22, 5]
Primes: [2, 11, 61, 5]
Composites: [91, 87, 10, 24, 21, 12, 46, 15, 32, 57, 22]
%
There are lots of ways to go about this problem, many of them educational!

How to make a program which prints prime numbers in given range

I want to make a program as an exercise which prints all prime numbers in range of given limit(parameter). Thanks.
#prime numbers
prime = list()
for x in range(1, 100):
for a in range(2, x):
if x % a == 0:
break
else:
prime.append(x)
print(prime)
You can use this list comprehension to find prime numbers in given range;
def prime_generator(number):
return [x for x in range(2, number) if not any(x % y == 0 for y in range(2, int(x/2)+1))]
In: prime_generator(30)
Out: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Pyschools Prime factorization

I'm doing the pyschools practices and i have a problem at Topic 5 question 12 - Prime Factorization, i have to do this:
Given a positive integer, write a function that computes the prime factors that can be multplied together to get back the same integer.
Examples
>>> primeFactorization(60)
[2, 2, 3, 5]
>>> primeFactorization(1050)
[2, 3, 5, 5, 7]
>>> primeFactorization(1)
[]
This is my code:
import operator
import functools as fun
def primeFactorization(num):
num = num
primes = []
result = []
if num > 1:
[primes.append(x) for x in range(2, num) if all(x % y != 0 for y in range(2, x))]
multy = fun.reduce(operator.mul, result, 1)
for number in primes:
if num % number == 0 and multy != num:
result.append(number)
return result
Which returns me this:
Test Cases Expected Result Returned Result
primeFactorization(33) [3, 11] [3, 11]
primeFactorization(84) [2, 2, 3, 7] [2, 3, 7]
primeFactorization(1) [] []
I've tryed this and i'm getting Private Test Case failed:
import operator
import functools as fun
def primeFactorization(num):
num = num
primes = []
result = []
if num > 1:
[primes.append(x) for x in range(2, num) if all(x % y != 0 for y in range(2, x))]
multy = fun.reduce(operator.mul, result, 1)
for number in primes:
if num % number == 0:
result.append(number)
multy = fun.reduce(operator.mul, result, 1)
y = num/multy
if y != 1 and y in primes:
result.insert(0, int(y))
return result
Test Cases Expected Result Returned Result
primeFactorization(33) [3, 11] [3, 11]
primeFactorization(84) [2, 2, 3, 7] [2, 2, 3, 7]
Private Test Cases Passed Failed
primeFactorization(1) [] []
What can i do to pass?
Why make it so complicated?
The problem of finding all prime factors of a given number X is the same as to find the set of the smallest numbers (that are larger than 1) that we can divide X into.
To solve the problem we can start by finding the smallest number n that divides X.
This is the first prime factor of X. We can then find the rest of the prime factors by finding the prime factors of X/n
def primeFactorization(X):
possible = [2] + range(3,int(ceil(sqrt(X)))+1,2)
for p in possible:
if X % p == 0:
return [p] + primeFactorization(X/p)
return [X]
primeFactorization(3*3*7*5*11*13*31)
> [3,3,5,7,11,13,31]

Categories

Resources