Note, this is not my homework assignment! I'm just trying to understand Python (and math, sadly) at the same time. I know the ultimate goal of this program is to get a list of prime numbers in the range 1 to 20, however, once it gets to the "for x in range..." line, I'm getting lost and the tutorial doesn't explain it in detail.
Could you please explain in plain English step by step and clarify specifically
a) what is x in the line for x in range (2,n)
b) in the line for x in range (2,n), what is n? Is it the same "n" at the bottom?
c) what is this n, x, n // x saying exactly. Please clarify the //
thanks if you can help
def isprime(n):
if n == 1:
print("1 is special")
return False
for x in range(2, n):
if n % x == 0:
print("{} equals {} x {}".format(n, x, n // x))
return False
else:
print(n, "is a prime number")
return True
for n in range(1, 20):
isprime(n)
a)
for x in range (2,n)
is the same like
for (x = 2; x < n; x++)
in some other language: a loop where x gets integer values between 2 and n-1 included.
b)
for x in range (2,n):
this n comes from the first def isprime(n) and is whatever this function is later called with. In this case it is always the same n from the bottom.
c)
print("{} equals {} x {}".format(n, x, n // x))
this writes the following text: A equals B x C where A is n, B is x and C is n/x rounded to the nearest smaller integer. It is so called integer division (e.g. 9 // 2 = 4)
a) Try this at the prompt:
help(range)
It will tell you that range(a,b) returns a list atarting as a, ending at b-1, so
range(2,10)
is
[2, 3, 4, 5, 6, 7, 8, 9]
Play at the prompt, type range(2,2), range(2,-1), range(2,3) and see what comes out. You'll see that n==1 isn't the only special case.
Now, something like for x in y iterates over the elements of y, which in your case would be a list. You can also verify this at the prompt:
for x in range(2,10) :
print x
b) the block starting with def isprime(n) is a function, with argument n. You can call it for any n: isprime(100). At the bottom of the code, you are iterating over range(1,20) (if in doubt type it into the prompt) and calling isprime for each value, i.e. 1,2,3,4,...,19.
Note that in this example, there isn't a need to create and return a list with range, you can use xrange, which is a generator. Type help(xrange) in the prompt...
a) x takes on the values from 2 to n-1 (since range excludes the upper bound)
b) No, it's the same n as in the method definition (i.e. the method argument)
c) Integer division
Related
I am trying to write code which gives you numbers which are less than given or entered number , and their GCD equal to 1 . I wrote this code but I don't know if works or why not . For example I chose number 6. array will be like [1,2,3,4,5]. And my point is to filter numbers that GCD equals to 1. So it will be [1,5]. And their amount is two.
a is input number and b is list numbers that are less than entered one and not equal to zero . And then print it .
a = int(input("enter number \n"))
b = list(range(1,a))
print (b)
Then I convert list to array
for i in range(1, len(b)):
b[i] = int(b[i])
and then this
r = a % b[i]
q = int ( a / b[i])
while(r!=0):
a = b[i]
b[i] = r
q = int ( a / b[i])
r = a - (b[i] * q)
print ( a , b[i], r )
break
I am beginner .
A few comments about your code:
You should always encapsulate code like this in a function; write a function find_coprimes which takes an argument n and returns the list you want;
In order to test the correctness of your function, write a reference function find_coprimes_ref which does the same thing, but uses library functions to make sure there is no error; this will teach you to look for relevant library functions, and you can compare the results of the two functions;
The initial loop for i in range(1, len(b)): b[i] = int(b[i]) is wrong for two reasons; 1) It has no effect, as b is already a list of integers. 2) Lists are 0-indexed, so a correct iterations on every element of b would be for i in range(0, len(b)): or simply for i in range(len(b)):;
Your code has two nested loops: a while-loop executing repeatedly inside a for-loop; whenever there are nested loops like this, you must make sure that variables are reinitialised the way you intend them to at the beginning of the outer loop; in your case, variable a is modified inside the while-loop, and as a result, its value is wrong at the beginning of the next iteration of the for-loop.
The break statement at the end of the while-loop makes no sense; in general, break statements only make sense if they are encapsulated in an if conditional, and they act as a substitute for the loop condition; but it's always possible to write loops without using break at all and I recommend you forget about break entirely.
After performing the gcd calculation using q and r, your code is missing something to tell it whether or not to keep b[i] or not in the final result;
For integer division in python, it is better to use // rather than int(... / ...).
Code
import math
def find_coprimes_ref(n):
return [x for x in range(1,n) if math.gcd(x,n) == 1]
def find_coprimes(n):
result = []
for x in range(1, n):
a = n
b = x
r = a % b
q = a // b
while (r > 1):
a = b
b = r
q = a // b
r = a - b * q
if (r == 1):
result.append(x)
return result
# TESTING
for n in range(1, 31):
coprimes_ref = find_coprimes_ref(n)
coprimes = find_coprimes(n)
if coprimes_ref != coprimes:
print(n, coprimes_ref, coprimes)
Note how my code never modifies n or x in the loop; instead, I make copies called a and b and modify the copies.
Encapsulating even further
Note how function find_coprimes_ref is so much easier to read than function find_coprimes? This is not just because we used library function math.gcd. It's because library function math.gcd is a cleanly-encapsulated function with a name that explains clearly what it does. Your code contains a while loop inside a for loop and it's a bit hard to keep track of every variable and everything that is going on and not lost track of our sub-objective and overall objective.
To make your function both easier to read, easier to code and easier to debug, You should encapsulate the gcd calculation inside a function called gcd:
def gcd(a, b):
r = a % b
q = a // b
while (r > 1):
a = b
b = r
q = a // b
r = a - b * q
return r
def find_coprimes(n):
result = []
for x in range(1, n):
if gcd(a, b) == 1:
result.append(x)
return result
# TESTING GCD
for b in range(1, 31):
for a in range(b, 31):
r1 = math.gcd(a, b)
r2 = gcd(a, b)
if r1 != r2:
print(a, b, r1, r2)
# TESTING FIND_COPRIMES
for n in range(1, 31):
coprimes_ref = find_coprimes_ref(n)
coprimes = find_coprimes(n)
if coprimes_ref != coprimes:
print(n, coprimes_ref, coprimes)
There are two reasons why the code is easier to debug now:
The logic for gcd and for find_coprimes is cleanly separated, which means you can reason about gcd clearly without any risk of messing up the list and the other variables used in find_coprimes;
You can test separately your function gcd and your function find_coprimes; and if something doesn't work correctly, you'll know more precisely where to look for the issue rather than just thinking "well, something is wrong somewhere in the code but I have no idea where".
There are a few errors in your code like, break inside while loop. I have refactored your code and also added inbuilt math.gcd function to compare results.
import math
def math_inbuilt_gcd(a, b):
gcd_one = []
for x in b:
if math.gcd(x, a) == 1:
gcd_one.append(x)
print("gcd_one_math_fun:", gcd_one)
def gcd_my_fun(a, b):
gcd_arr = []
for i in range(len(b)):
x, y = a, b[i] # taking x, y just to make things clear
r = x % y # remainder
q = int(x / y) # quotient
while(r != 0):
x = y
y = r
q = int(x/y)
r = x % y
if y == 1:
gcd_arr.append(b[i])
print("gcd_one_my_fun:", gcd_arr)
a = int(input("Enter number: "))
b = list(range(1, a))
print("b:", b)
math_inbuilt_gcd(a, b)
gcd_my_fun(a, b)
Output:
Enter number: 10
b: [1, 2, 3, 4, 5, 6, 7, 8, 9]
gcd_one_math_fun: [1, 3, 7, 9]
gcd_one_my_fun: [1, 3, 7, 9]
import sys
def pythTrue(a,b,c):
(A,B,C) = (a*a,b*b,c*c)
if A + B == C or B + C == A or A + C == B:
return True
def smallestTrip(a,b,c):
if pythTrue(a,b,c) == True:
if (a+b+c)%12 == 0:
return True
else:
return False
def tuplePyth(n):
list_=[]
for x in range(1, n):
for y in range(1, n):
for z in range (1, n):
if x+y+z<=n:
if smallestTrip(x, y, z)==False:
list_.append([x,y,z])
print (list_)
tuplePyth(int(sys.argv[1]))
Pythagorean triplets are sets of 3 positive integers a, b, c
satisfying the relationship a2 + b2 =
c2. The smallest and best-known Pythagorean triple is
(a, b, c) = (3, 4, 5). Write a program that reads a command line
argument n and prints to the screen all Pythagorean triplets whose sum
is less than n (i.e., a+b+c < n) and that are not multiple of the (3,
4, 5) triplet. Your program will represent triplets as 3-tuples, and
should consist of three functions:
a function that takes in a tuple
and returns a boolean indicating whether the Pythagorean relationship holds or not.
a function that takes in a tuple and returns
a boolean indicating whether a triplet is a multiple of the smallest
triplet or not.
a function that takes in an integer n and generates
the Pythagorean triplets as specified above. The function should
return a list of tuples.
The main portion of your program pythagore.py will read in the command
line input, call the last function described above, and print the
results one triplet per line.
My problem is that I am getting the same combination in different
orders for example: (5,12,13),(13,12,5)...etc
You're short on logic in your main routine. There is nothing to enforce that the triple comes in only one order: your x and y are interchangeable, and you guarantee that you'll check both.
Instead, force x < y with your loop limits, and then make sure you stop when the value of y or z gets too large to be viable. Note that this gets rid of your check for the sum of the three.
def tuplePyth(n):
list_=[]
for x in range(1, n):
for y in range(1, n):
for z in range (1, n):
if x+y+z<=n:
if smallestTrip(x, y, z)==False:
list_.append([x,y,z])
print (list_)
Instead:
def tuplePyth(n):
list_=[]
for x in range(1, n):
for y in range(x + 1, (n - x) // 2):
for z in range (y + 1, n - x - y):
if smallestTrip(x, y, z)==False:
list_.append([x,y,z])
print (list_)
Output with n=100:
[[5, 12, 13], [7, 24, 25], [8, 15, 17], [9, 40, 41], [15, 36, 39], [16, 30, 34], [20, 21, 29]]
Note that you still have a problem with smallestTrip: your check is not logically equivalent to "smallest triple". Instead, check that the three numbers are relatively prime. Since Stack Overflow allows only one question per posting, and the problem is readily researched on line, I'll leave that as an exercise for the student. :-)
An easy solution would be to keep track of the ones aleady found and add checks to avoid repeating them. The following uses a set to store the ones already produced and sorts the the elements in each triple so that their order doesn't matter.
def tuplePyth(n):
list_=[]
seen = set()
for x in range(1, n):
for y in range(1, n):
for z in range (1, n):
if tuple(sorted((x,y,z))) not in seen:
if x+y+z <= n:
if smallestTrip(x, y, z) == False:
list_.append([x,y,z])
seen.add((x,y,z))
print (list_)
You can use itertools:
import itertools.combinations_with_replacement as cwr
list_ = [triple for triple in cwr(range(n),3) if sum(triple)<n and not smallestTrip(triple)]
You can also force the numbers to be in order with the limits. Also, you can simplify finding a,b, c by realizing that if we define a to be the smallest number, then it must be smaller than n/3 (b and c will both be at least as large as a, so if a were larger than n/3, then the sum of a, b, and c would be more than n). Similarly, b must be smaller than n/2. Once you've found all the combinations of a and b, you can find all the c that are larger than b and smaller than n-a-b.
list_=[]
for x in range(1, n//3):
for y in range(x+1, n//2):
for z in range (x+y+1, n-x-y):
if not smallestTrip(x, y, z):
list_.append([x,y,z])
Because the three numbers are never the same you can just change the second and the third range from (1,n) to (x+1,n) and (y+1,n) correspondingly.
I am trying to find the LCM of first 20 natural numbers (Project Euler question 5). For that, my algorithm is:
have numbers 1 to 20 in a list
Divide only those elements of the list that are divisible by i where i is in the range (2-20).
Whatever numbers are left in the list, multiply them and that will be the lcm.
This is the naivest algorithm which we actually used to calculate lcm in school for the first time.
Now, I donot know how to divide the elements of the list based on the condition.
I have tried:
a=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
for x in a:
if(x%2==0):
x=x/2
This does not seem to work.
I also tried:
a=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
a1=[if(x%2==0): x/2 for x in a]
the above with both with and without ":" after the if condition. This does not work. I have the following questions:
a. Why isn't the first loop working correctly?
b. Can someone tell me how I can do this?
c. Will my algorithm work correctly?
a. Why isn't the first loop working correctly?
For the same reason as:
Foreach in Python not working as expected
b. Can someone tell me how I can do this?
You can do either:
a=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
for i, x in enumerate(a):
if x%2==0:
a[i]=x/2
Or:
a=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
a1=[x/2 if x%2==0 else x for x in a]
c. Will my algorithm work correctly?
I don't think so. You'll end up dividing everyone by itself and the result will always be 1.
But there are other questions here in SO that have simple answers, like:
find least common multiple of numbers 1-20
a) Why is this loop not working correctly?
As #jose-ricardo-bustos-m indicates, the x is not a reference, is a local copy to each element of the array a, and cannot modify the array in the for loop. You can use, instead:
a=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
for i,x in enumerate(a): #used to provide a value, and an index
if(x%2==0):
a[i]=x/2
b) Can someone tell me how I can do this?
You can try to use the ternary if operator and list comprehension:
a = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
b = [x/2 if x%2==0 else x for x in a]
c) Will my algorithm work correctly
You have to keep track of the numbers you have already used, and you might need to divide by the same number more than once. But if you do that, and keep dividing by the same number _until the resulting list is equal to the previous one, and then move to the next, you can later multiply all numbers used, times the remainder of the list (but if you go to the max number in the list, the remaining list will contain just 1's).
def f(l,n): # divides items in a which are divisible by n, or leaves them
return [x/n if x%n==0 else x for x in l]
lcm = 1
a=[2,3,4,5,6,7]
# we go from the smallest to the largest number in your list
for i in range(2,max(a)+1):
repeat_next_time = True
while repeat_next_time:
b = f(a,i)
if a != b:
print('Using %s as a factor' % i)
a = b
lcm *= i
# print(a) # to get the status of the a list
else:
repeat_next_time = False
# finally, for numbers which might have not been divided yet,
# multiply the lcm by all of the remaining items
lcm *= reduce(lambda x,y: x*y, a)
It works even if there are common divisors, or repeated numbers in the list. Try, for instance, with a = [2,2,2], or a = [2,3,6], or a = [8,7,4,7].
a) the variable x takes the value of the list a , but not modified, it is not a reference of list, the following code does what you want:
a=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
for i in range(len(a)):
if(a[i]%2==0):
a[i]=a[i]/2
b) y C)
a=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
def f(x):
if(x%2==0):
return x/2
return x
a1=[f(x) for x in a]
Whatever numbers are left in the list, multiply them and that will be the lcm.
reduce(lambda x, y: x*y, a1)
I thought of solving this problem in the following way: start with two variables with value 999, multiplying one by another in a loop that decrements one or the other until a palindrome is found. The code is this:
def is_palindrome(n):
if str(n) == str(n)[::-1]:
return True
else:
return False
def largest_palindrome_product_of_3_digit():
x = 999
y = 999
for i in reversed(range(x + y + 1)):
if is_palindrome(x * y):
return x * y
if i % 2 == 0:
x -= 1
else:
y -= 1
The result of my method is 698896, while the correct result is 906609. Could you point me where my logic is incorrect?
Here are a couple of hints:
If n=y*x is any number in the range(600000, 700000) (for example) with y<=x, and x<1000, what's the smallest possible value of x?
If n is a palindromic number, both its first and last digit are 6, so what does that imply about the last digits of x & y?
Now generalize and figure out an efficient algorithm. :)
I've never done this problem before, but I just coded a reasonably fast algorithm that's around 2000 times faster than a brute-force search that uses
for x in xrange(2, 1000):
for y in xrange(2, x+1):
n = y*x
#etc
According to timeit.py, the brute-force algorithm takes around 1.29 seconds on my old machine, the algorithm I hinted at above takes around 747 microseconds.
Edit
I've improved my bounds (and modified my algorithm slightly) and brought the time down to 410 µsec. :)
To answer your questions in the comment:
Yes, we can start x at the square root of the beginning of the range, and we can stop y at x (just in case we find a palindromic square).
What I was getting at with my 2nd hint is that for x=10*I+i, y=10*J+j, we don't need to test all 81 combinations of i and j, we only need to test the ones where (i*j)%10 equals the digit we want. So if we know that our palindrome starts and ends with 9 then (i, j) must be in [(1, 9), (3, 3), (7, 7), (9, 1)].
I don't think I should post my actual code here; it's considered bad form on SO to post complete solutions to Project Euler problems. And perhaps some SO people don't even like it when people supply hints. Maybe that's why I got down-voted...
You're missing possible numbers.
You're considering O(x+y) numbers and you need to consider O(x * y) numbers. Your choices are, essentially, to either loop one of them from 999, down to 1, then decrement the other and...
Simple demonstration:
>>> want = set()
>>> for x in [1, 2, 3, 4, 5]:
... for y in [1, 2, 3, 4, 5]:
... want.add(x * y)
...
>>> got = set()
>>> x = 5
>>> y = 5
>>> for i in reversed(range(x + y + 1)):
... got.add(x * y)
... if i % 2:
... x -= 1
... else:
... y -= 1
...
>>> want == got
False
Alternatively, you do know the top of the range (999 * 999) and you can generate all palindromic numbers in that range, from the highest to the lowest. From there, doing a prime factorization and checking if there's a split of the factors that multiply to two numbers in the range [100,999] is trivial.
m = range(1, 2000000, 2)
sum1 = 2
for x in xrange(1, 2000000, 2):
for y in m:
if x != y:
if x%y == 0:
m.remove(x)
if all(x%y != 0):
sum1 += x
That's what I've written. It's about a problem, trying to add all the primes bellow two million. My problem is in the all() statement. What I want to happen is to check if x is a prime; that is true only if every x%y gives a remainder.
Also if I use a can I use a statement (break?) to stop the loop if y > x/3 like so:
m = range(1, 2000000, 2)
sum1 = 2
for x in xrange(1, 2000000, 2):
for y in m:
if y > x/3:
break
else:
if x != y:
if x%y == 0:
m.remove(x)
if all(x%y != 0):
sum1 += x
You have to pass a sequence or iterable to all -- it just tests whether or not all the items passed to it evaluate as true. Here's the right way to use all:
>>> all([True, True, True])
True
>>> all([True, False, True])
False
>>> all([x > 5 for x in range(10)])
False
>>> all([x > 5 for x in range(6, 10)])
True
>>> all(x > 5 for x in range(6, 10))
True
That last one is the best, because it takes advantage of short-circuiting.
However, your call to all in your code is pointless. The idea behind your code, it seems to me, is to go through all the values in m and remove those that are divisible by any number between 2 and 2000000. Once you've done that, m will contain only prime numbers.
Of course, your code still won't work if you remove all. That's because you're actually testing whether each number in m is divisible by the numbers [1, 3, 5, 7...1999999]. (That's the sequence signified by xrange(1, 2000000, 2). Because you start at 1, and everything is divisible by 1, your code will count nothing as prime. And then, once you remove 1 from that sequence, anything divisible by 2 will be counted as prime by your code! You should think more carefully about which numbers you actually have to test in your inner loop.
Finally, you should think about how many loops this code will complete. Even once you have this working, it will take a very long time to generate a result. You should test it on smaller numbers first; and then, you should think about how to reduce the number of loops. (And -- only after you've thought about it a bit -- read this.)
But once you have this working, all you have to do is call sum on your list of primes.
Your use of all is incorrect, if you look at the documentation for it, it takes an iterable.
What you may be trying to do is use a generator expression, something of the form:
sum(x**2 for x in range(10))
which is very similar to the list comprehension
[x**2 for x in range(10)]
However, use of all in this manner wouldn't suddenly stop the generator expression, if it found a divisor. Use of any and checking for x == 0 would stop sooner, but as the code is currently formatted, would check for many divisors before deeming something prime.
This would be more appropriate:
primes = []
MAX = 2000000
number = 2
while number < MAX:
for prime in primes:
if number % prime == 0:
number += 1
continue
primes.append(number)
number += 1
total = sum(primes)
all() takes an iterable for an argument. In your situation, you would use it like this:
all(x%y for y in m)
where x%y for y in m is a generator expression.
If I have an iterable
[item1, item2, item3, item4...]
all(iterable) is equivalent to:
item1 and item2 and item3 and item4...'