Finding the number of k-primes within a range - python

Write function count_Kprimes with given parameters k, start, nd, that returns a list of the k-primes between start (inclusive) and end (inclusive).
Here is my attempt:
def count_Kprimes(k, start, nd):
ls = []
for x in range(start, nd + 1):
y = x
l = []
for i in range(2, x + 1):
while y % i == 0:
l.append(i)
y /= i
if len(l) == k:
ls.append(x)
return ls
However, my code takes too much time to process and I want to simply my code. How can it be done? Thank you so much!
This task is taken from Codewar

Well, I had fun solving this anyway. Here is a solution based on array-logic
def count_Kprimes(k, start, nd):
x = np.arange(start, nd + 1, dtype=np.float)
# divs will contain all divisors (plus one extra column)
divs = np.ones((x.size, k + 1))
# we have to loop only nd / 2^(k-1) to get all divisors
for i in range(2, int(nd / 2 ** (k - 1)) + 1):
# but each possible divisor "i" may occur up to k times
# we loop until k+1 to catch also number that exceed our target,
# so we can discard them later
for j in range(1, k + 2):
# check for each row (dimension 0) if i is a divisor
# then set the first zero-value in dimension 1 to be this divisor
d = np.prod(divs, axis=1)
divs[[[np.rint(x/d/i)==x/d/i][0],np.argmin(divs[np.rint(x/d/i)==x/d/i], axis=1)]] = i
# The correct result we're looking for is each row that has exactly
# k values != 1 (which equals to exactly one "1" per row)
indices = np.apply_along_axis(lambda x: x[x==1].size == 1, 1, divs)
for val, d in zip(x[indices], divs[indices]):
print "{} = {}".format(int(val), " * ".join([str(int(_)) for _ in d[:-1]]))
count_Kprimes(3, 1, 100)
returns
8 = 2 * 2 * 2
12 = 2 * 2 * 3
18 = 2 * 3 * 3
20 = 2 * 2 * 5
27 = 3 * 3 * 3
28 = 2 * 2 * 7
30 = 2 * 3 * 5
42 = 2 * 3 * 7
44 = 2 * 2 * 11
45 = 3 * 3 * 5
50 = 2 * 5 * 5
52 = 2 * 2 * 13
63 = 3 * 3 * 7
66 = 2 * 3 * 11
68 = 2 * 2 * 17
70 = 2 * 5 * 7
75 = 3 * 5 * 5
76 = 2 * 2 * 19
78 = 2 * 3 * 13
92 = 2 * 2 * 23
98 = 2 * 7 * 7
99 = 3 * 3 * 11

Related

College problem and I have no idea what to do (Prime numbers)

Im currently in college and I have an assignment that involves multiplying the primes and non primes below 45, so that their product doesn't go over 45. The language is python and I have tried doing for loops and checking for prime and not prime with modulus but I don't know where to go from there. (can't utilize arrays or anything related only loops, while else, elif etc.)
for num in range (1, 45):
if num > 1:
for num2 in range(2, 45):
if (num%num2) == 0:
n = num2
print(n, 'is Prime')
if num * num2 < 45:
print('Number ', num, '*', num2, '=', num*num2)
What exactly im trying to achieve is to first find all the prime numbers, then multiply them by all the non primes. Those primes and non primes are in the range of 1 - 45. Then, I Have to multiply them and their product has to be less than 45 then print that.
UPDATE: So I tried Thanh Tùng Nguyễn's program which is very well built, but I Can't utilize functions since I haven't been taught that (aka would be weird), what I tried to do is simply take what the function is and tried to just implement it into the loops themselves setting n as a boolean value, but my print doesn't amount to anything and I can't catch what's happening. The terminal just stays completely empty. My guess is on the last 'if not' statement, where I have probably written it wrong because I can't understand it myself why it isn't working when im treating it the same as a function through the entire block.
Here's what I got:
from math import sqrt
for num1 in range (2, 46):
if num1 <= 1:
n = False
for i in range (2, int(sqrt(num1))):
if num1%i == 0:
n = False
n = True
if n == True:
for num2 in range (1, 46):
if num2 <= 1:
n = False
for j in range (2, int(sqrt(num2))):
if num2%j == 0:
n = False
n = True
if not n == True:
if num1 * num2 < 45:
print(num1, '*', num2, '=', num1*num2)
You could create a prime-number check function like this:
from math import sqrt
def checkPrime(num):
if num <= 1:
return False
for i in range(2, int(sqrt(num))+1):
if num % i == 0:
return False
return True
Note that you can just check from 2 to √n to optimize it. From there, just find all prime numbers below 45 and multiply it with a non-prime number:
from math import sqrt
def checkPrime(num):
if num <= 1:
return False
for i in range(2, int(sqrt(num))+1):
if num % i == 0:
return False
return True
for n in range(2, 45+1):
if checkPrime(n):
for j in range(1, 45+1):
if not checkPrime(j):
if n * j < 45:
print(n, " * ", j, " = ", n*j)
Output:
2 * 1 = 2
2 * 4 = 8
2 * 6 = 12
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
2 * 12 = 24
2 * 14 = 28
2 * 15 = 30
2 * 16 = 32
2 * 18 = 36
2 * 20 = 40
2 * 21 = 42
2 * 22 = 44
3 * 1 = 3
3 * 4 = 12
3 * 6 = 18
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
3 * 12 = 36
3 * 14 = 42
5 * 1 = 5
5 * 4 = 20
5 * 6 = 30
5 * 8 = 40
7 * 1 = 7
7 * 4 = 28
7 * 6 = 42
11 * 1 = 11
11 * 4 = 44
13 * 1 = 13
17 * 1 = 17
19 * 1 = 19
23 * 1 = 23
29 * 1 = 29
31 * 1 = 31
37 * 1 = 37
41 * 1 = 41
43 * 1 = 43
If I understand correctly, you need to multiply primes with non-primes. This means that the products will be formed of 3 or more primes.
To have 3 primes in the product, you can have 2 at most 5 times. You can have 3 at most 3 times, 5 at most twice, 7/11 no more than once.
So you only need to take into account 2, 3, 5, 7 and 11 as primes and combine them in sets of 3 or more without going over 45:
You could nest 5 for-loops for the number of 2s, 3s, 5s, 7s and 11s in the product.
For example:
for p2 in range(7):
for p3 in range(4):
for p5 in range(3):
for p7 in range(2):
for p11 in range(2):
prod = 2**p2 * 3**p3 * 5**p5 * 7**p7 * 11**p11
if prod > 45 : continue
if p2+p3+p5+p7+p11 < 3: continue
if p2: print(2,'x',prod//2,'=',prod)
if p3: print(3,'x',prod//3,'=',prod)
if p5: print(5,'x',prod//5,'=',prod)
if p7: print(7,'x',prod//7,'=',prod)
if p11: print(11,'x',prod//11,'=',prod)
Output:
3 x 15 = 45
5 x 9 = 45
3 x 9 = 27
2 x 21 = 42
3 x 14 = 42
7 x 6 = 42
2 x 15 = 30
3 x 10 = 30
5 x 6 = 30
2 x 9 = 18
3 x 6 = 18
2 x 22 = 44
11 x 4 = 44
2 x 14 = 28
7 x 4 = 28
2 x 10 = 20
5 x 4 = 20
2 x 6 = 12
3 x 4 = 12
2 x 18 = 36
3 x 12 = 36
2 x 4 = 8
2 x 20 = 40
5 x 8 = 40
2 x 12 = 24
3 x 8 = 24
2 x 8 = 16
2 x 16 = 32à
You can also do it the other way around, by counting the primes that form each number up to 45 and, if there are 3 or more primes, print the ones that are part of the number as you go:
for prod in range(8,46):
primeCount = 0
rest = prod
for prime in range(2,12):
while rest and rest % prime == 0:
primeCount += 1
rest = rest//prime
if primeCount<3: continue
if prod%2==0: print(prod,'=',2,'x',prod//2)
if prod%3==0: print(prod,'=',3,'x',prod//3)
if prod%5==0: print(prod,'=',5,'x',prod//5)
if prod%7==0: print(prod,'=',7,'x',prod//7)
if prod%11==0: print(prod,'=',11,'x',prod//11)
Note that, even though I'm dividing by non=primes in the counting loop, only primes will actually be counted because, as the rest is reduced, multiples of previous primes will no longer divide the rest evenly.
Output:
8 = 2 x 4
12 = 2 x 6
12 = 3 x 4
16 = 2 x 8
18 = 2 x 9
18 = 3 x 6
20 = 2 x 10
20 = 5 x 4
24 = 2 x 12
24 = 3 x 8
27 = 3 x 9
28 = 2 x 14
28 = 7 x 4
30 = 2 x 15
30 = 3 x 10
30 = 5 x 6
32 = 2 x 16
36 = 2 x 18
36 = 3 x 12
40 = 2 x 20
40 = 5 x 8
42 = 2 x 21
42 = 3 x 14
42 = 7 x 6
44 = 2 x 22
44 = 11 x 4
45 = 3 x 15
45 = 5 x 9

how to print for loop output on same line after each iteration

Code
a = int(input("enter a no"))
b = int(input("enter a range"))
for i in range(1, a+1):
print(i)
for j in range(1, b + 1):
c = i * j
print(i, "*", j, "=", c)
Desired output
1 2 3
1 * 1 = 1 2 * 1 = 2 3 * 1 = 3
1 * 2 = 2 2 * 2 = 4 3 * 2 = 6
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27
1 * 10 = 10 2 * 10 = 20 3 * 10 = 30
You can tell print to finish with something other than a newline by specifying the "end" argument:
a = int(input("enter a no "))
b = int(input("enter a range "))
for i in range(1, a+1):
print(i, end=" ")
print("")
for j in range(1, b + 1):
for i in range(1, a+1):
c = i * j
print(i, "*", j, "=", c, end=" ")
print("")
In this case I split the main loop into two separate loops, the first outputs the top line (1, 2, 3...) with some large spacing, whilst the second then does all of the others with slightly less spacing.
I also switched the order of the later loops since there should be b lines, each with a multiplications, so the b loop needs to be the outer (first) loop.
In both cases an empty print statement is used to output a newline when we need it.
This is what you are looking for:
a = int(input("enter a no"))
b = int(input("enter a range"))
for i in range(1, a + 1):
print(i, end="\t"*4)
print("")
for k in range(1, b + 1):
for j in range(1, a + 1):
print(j, "*", k, "=", j*k, end="\t"*2)
print("")
You have to think line-by-line, since you cannot go back in stdout.
The first for will fill the first line
I modified your nested loop because you were using the wrong variables
"\t" is meant to get decent formatting, but it will eventually break if numbers are too big: I believe there is a more refined approach that I am not aware of
print("") is just meant to go to the next line
Approach
Approach is use f-strings to left align all the prints into fields of width 20
Use fixed field widths since otherwise columns won't line up with single and multi-digit numbers
Use print parameter end = '' to avoid carriage returns on after printing a field
Code
a = int(input("enter a no "))
b = int(input("enter a range "))
width = 20
for i in range(1, a+1):
print(f'{i:<{width}}', end="") # Print all multipliers on a single row
print("")
for j in range(1, b + 1):
# Looping over multiplication row
for i in range(1, a+1): # Looping through the columns to multipl
s = f'{i} * {j} = {i*j}' # Expression for column field
print(f'{s:<{width}}', end = '') # Print field left aligned to width 20
print("") # New row
Test
enter a no 4
enter a range 10
1 2 3 4
1 * 1 = 1 2 * 1 = 2 3 * 1 = 3 4 * 1 = 4
1 * 2 = 2 2 * 2 = 4 3 * 2 = 6 4 * 2 = 8
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 4 * 3 = 12
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36
1 * 10 = 10 2 * 10 = 20 3 * 10 = 30 4 * 10 = 40
I corrected it like this. Thankyou guys.
just add the end="\t"*4 to fix the alignment issue.
a = int(input("enter a no "))
b = int(input("enter a range "))
for i in range(1, a+1):
print(i, end="\t"*4)
print("")
for j in range(1, b + 1):
for i in range(1, a+1):
c = i * j
print(i, "*", j, "=", c, end="\t"*2)
print("")

Why in Python 1%2>>>1 or 5%6>>>5

If % should return the remainder, why then
>>>1%2
1
>>>5%6
5
when
>>>1/2
0.5
>>>5/6
0.83(3)
?
This comes from the mathematical definition of modulo which is:
x= quotient(x//y) * y + modulo(x%y)
In your examples quotient is zero which results to:
5 = 0 * 6 + modulo(5%6) => modulo(5%6) = 5
1 = 0 * 2 + modulo(1%2) => modulo(1%2) = 1

Printing a Multiplication Table Using Python Nested For Loops

for i in range(3,33,3):
for j in range(1,11,1):
print("3 *", j, '=', i)
if j == 10:
break
This is the output that I am getting:
3 * 1 = 3
3 * 2 = 3
3 * 3 = 3
3 * 4 = 3
3 * 5 = 3
3 * 6 = 3
3 * 7 = 3
3 * 8 = 3
3 * 9 = 3
3 * 10 = 3
Could anyone please point out the error for me?
If you just want to print multiples of 3, you don't need two loops. Just one loop from 1 to 10, and then multiply that by 3.
for i in range(1, 11):
j = i * 3
print('3 *', i, '=', j)
I believe you want the product as well as the multiplier, you can use enumerate for this. The code will look something like this:
for i,j in enumerate(range(3,33,3)):
print("3 *", i, '=', j)
Change the i to i * j:
for i in range(3,33,3):
for j in range(1,11,1):
print("3 *", j, '=', i * j)
if j == 10:
break
Here is a simplified version:
for i in range(1, 11):
print(f"3 * {i} = {3 * i}")
Output:
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
You're doing nested loops*, but you meant to loop in parallel. You can do that with zip():
for i, j in zip(range(3, 33, 3), range(1, 11)):
print("3 *", j, '=', i)
Output:
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
Canonical question: How to iterate through two lists in parallel?
* This creates a Cartesian product, but it's cut short by the break.
However, in this case it's simpler to just do the math, like in Barmar's answer.

Solve equations with combinations and for loops

I have equations with multiple unknowns, and a number range:
eq1 = (x + 5 + y) #
ans = 15
no_range = [1..5]
I know that I can solve the equation by checking all possible combinations:
solved = False
for i in range(1, 5+1) # for x
for j in range(1, 5+1) # for y
if i + 5 + j == ans:
solved = True
So, the problem is that I want a function to deal with unknown_count amount of unknowns. So that both the following equations, or any, can be solved in the same manner above:
eq1 = (x + 5 + y)
ans = 15
eq2 = (x + 5 + y + z * a + 5 * b / c)
ans = 20
I just cannot think of a way, since for each unknown you need a for loop.
You could use itertools.product to generate the Cartesian product for an
arbitrary number of variables:
In [4]: import itertools
In [5]: list(itertools.product(range(1, 5+1), repeat=2))
Out[5]:
[(1, 1),
(1, 2),
(1, 3),
...
(5, 3),
(5, 4),
(5, 5)]
So you could modify your code like this:
import itertools as IT
unknown_count = 6
ans = 20
solved = False
def func(*args):
x, y, z, a, b, c = args
return x + 5 + y + z * a + 5 * b / c
for args in IT.product(range(1, 5+1), repeat=unknown_count):
if func(*args) == ans:
solved = True
print('{} + 5 + {} + {} * {} + 5 * {} / {} = {}'.format(*(args+(ans,))))
which yields a lot of solutions, such as
1 + 5 + 1 + 1 * 3 + 5 * 2 / 1 = 20
1 + 5 + 1 + 1 * 3 + 5 * 4 / 2 = 20
1 + 5 + 1 + 2 * 4 + 5 * 1 / 1 = 20
...
5 + 5 + 5 + 2 * 2 + 5 * 1 / 5 = 20
5 + 5 + 5 + 3 * 1 + 5 * 2 / 5 = 20
5 + 5 + 5 + 4 * 1 + 5 * 1 / 5 = 20
The * unpacking operator was used
to create a function, func, which accepts an arbitrary number of arguments (i.e. def func(*args)), and also to
pass an arbitrary number of arguments to func (i.e. func(*args)).

Categories

Resources