Kustaz 178 Report post Posted June 11, 2006 Hi, I've been trying to learn Python, and I have two questions. First off, how does the follwoing this line work: a, b = b, a+b My brain tells me that it should be identical to: a = b b = a + b But apparently its not ;). Next, is this bit of code: c = 0 d = 0 while c < 100: a = range(1, c / 2) for x in a: if (c / x) == 0: d = 1 if d == 0: print c, ' is prime', d = 0 c = c+1 It's supposed to calculate all primes between 1 and 100, but it doesn't work. I think the problem iss in the use of the range() function, but once again I'm not too sure. Thanks for any help! 0 Share this post Link to post Share on other sites
SiCrane 11839 Report post Posted June 11, 2006 If you have a, b = b, a + b it evaluates the right hand side before doing the assignment. So, for example, if a is 4 and b is 6, internally it does the moral equivalent ofa, b = b, a + b turns intoa, b = 6, 4 + 6 turns into a, b = 6, 10 turns intoa = 6b = 10For the second part, it looks like you want a = range(2, c / 2) 0 Share this post Link to post Share on other sites
Guest Anonymous Poster Report post Posted June 11, 2006 1) a, b = b, a + b:It's assigning in parallel; that is, it is NOT implicitly sequencing the operations. The values on the right are calculated with the original values on the left. That's precisely WHY it is convenient. It saves you writingtemp = aa = bb = temp + b2) I don't know where to begin.You're doing integer division:>>> 1 / 30So you're not getting what you expect. You'll have to tell python in some way that you want REAL division, for example:>>> float (1) / 30.33333333333333331Your algorithm is, how should I say, weird, in that it says:print b, ' is prime',where b is a variable that hasn't been initialized in your code yet... 0 Share this post Link to post Share on other sites
Guest Anonymous Poster Report post Posted June 11, 2006 The second part should say a = range(2, c / 2),and if (c % x) == 0: 0 Share this post Link to post Share on other sites
Guest Anonymous Poster Report post Posted June 11, 2006 Your prime-finding algorithm looks so wrong as to be unintelligible, so instead of trying to fix it I'll just write one from scratch in simple python.First, we realize this isn't the fifties and python supports procedural abstraction. So it might be a good idea to split our program into procedures. Splitting a problem into small manageable bits is generally a good idea.A convenient function to have is one which takes a number and a list of numbers and checks if the number is divisible by any number in that list:def divisible(anum, num_list): for p in num_list: if anum % p == 0: return True return False Note I used the modulo operator % instead of checking for remainders by doing floating point division. This says what I MEAN: 'check to see this INTEGER is divisible by that other INTEGER.'I do NOT mean 'convert these integers into floating point values, divide them and check to see if I get the floating-point 0 back'. So I do not write what I do not mean.Next, I think about numbers a bit, and notice that any number n that is not prime MUST have a divisor that is equal than or less than the square root of n. So I only have to check for divisors smaller than or equal to the square root of n. For that we need the int() function, which returns the first integer smaller than its floating point argument, and the sqrt() function, which returns the square root of a number. sqrt() is found in the math module, so we'll import it.We can easily express the requirement of being prime in terms of the divisible() function: a prime is a number that is not divisible by the integers less than or equal to its square root except for 1:from math import *def is_prime(n): root_n = int(sqrt(n)) return not divisible(n, range(2, root_n + 1))Now, we wanted a list of primes up to a given number. Given the auxiliary functions we defined, this is easy to do:def list_primes(upto): for i in range(2, upto + 1): if is_prime(i): print iLet's try it:>>> list_primes(29)2357111317192329Seems to work. 0 Share this post Link to post Share on other sites