Newbie - Trying to Help a Friend

Duncan Booth duncan.booth at invalid.invalid
Wed Nov 20 06:38:14 EST 2013


Denis McMahon <denismfmcmahon at gmail.com> wrote:

> 1) Find all the numbers less than n that are not divisible by a, b, or c.
> 
> ask the user for x;
> assign the value 0 to some other variable i;
> while i is not greater than than x do the following [
> if i is not divisible by a and i is not divisible by b and i is not 
> divisible by c then display i to the user;
> add 1 to i;
> ]
> 
The question didn't ask to find all the numbers, it asked to count how many 
there are. Also even if you change this to count instead of print, it could 
be very inefficient for large values of x.

If x is greater than a*b*c, find how many numbers up to a*b*c are not 
divisible by a, b, or c. (Depending on your interpretation of the English 
language for 2, 3, 5 this is either 8 or 1, you could check whether the 
system is set to Australian English to determine the correct action here.) 
You may then store these numbers in a list for easy reference.

Now the answer you want is the length of that list times the integer part 
of x divided by a*b*c plus the number of values in the list that are less 
than the remainder you get when dividing x by a*b*c.

If x is less than a*b*c then just find how many numbers up to x are not 
divisible by a, b, or c, which would be a case of re-using some of the 
above code.

For extra credit, calculate and use the least common multiple of a,b and c 
instead of just using their product.

-- 
Duncan Booth 



More information about the Python-list mailing list