[Tutor] Re: Creatively solving math problems -----help
Andrei
project5 at redrival.net
Wed Sep 10 03:53:06 EDT 2003
Conrad Koziol wrote:
Hi Conrad,
> for x in range(20000):
> if x/2 == int(x/2) and x/3 == int(x/3) and x/4 == int(x/4) and \
> x/5 == int(x/5) and x/6 == int(x/6) and x/7-1 == int(x/7-1):
>
> print x
> else:
> pass
Your indentation is all screwed up. Either use Tabs, or Spaces. Spaces are the
preferred solution, with 4 spaces per indentation level. If your current editor
does not support this, try Idle, SciTE, PythonWin or any other editor with
proper Python support. These editors will also automatically insert 4 spaces
when you press the Tab key. Remember, in Python indentation defines the code
structure.
Depending on what else you're doing, x/2 might return an integer or a floating
point (integer division: 5/2 = 2), so you shouldn't use it. If you expected to
get a float (and you do get floats if you use "from __future__ import
division"), you shouldn't compare a float to an integer, because floats aren't
100% accurate, they're approximations of numbers (very accurate, but not enough
to be regarded equal to an integer). This is documented in the Python FAQ.
> The question is what whole numbers can be divided by 2-6 and have a
> remainder of 1 but divided by 7 and have no remainder. I already have
You can test reminder with the % operator:
>>> 5%2
1
>>> 4%2
0
Obviously, this is a much better solution than testing equality.
> the answer;
> 301 and then you add 420 each time. The problem is the program gives me
> every number between 1 and 19999. Also i was wondering how many line
Given the indentation errors, I have no idea what that snippet is actually doing.
> breaks can you use? I believe it gives me 1-19999 because im using ==
You can break up lines as much as you like, as long as you use the backslash to
indicate the code continues on the next line. In certain cases, the backslash is
not required (e.g. inside triple-quoted strings or parentheses).
> but if i use only 1 = it gives me a syntax error. I believe this has
> something to do with the int(). Also int() returns it rounded down, is
> there any subsitute so it rounds to the nearest whole number.
Your approach for testing the remainder is incorrect, as I explained above.
> Another way i though i could solve this was by a script that looks like
> this:
>
>
> for x in range(2000):
>
> if x/2 = int(x/2):
> continue
> else:
> ??????????
>
> the script would continue like this all the way to 7. The problem is i
> dont know a command that if if proves true skips that iteration and goes
> on to the next one.Any help??
What's wrong with continue? It breaks the current iteration and goes to the next
one. This won't help you a lot because it's too difficult to understand, but
it's fun to see anyway *wink*; a one-line solution:
>>> [ x for x in range(20000) if [x%i for i in range(2,8)]==[1]*5+[0] ]
[301, 721, 1141, 1561, 1981, 2401, 2821, 3241, 3661, 4081, 4501, 4921, 5341,
5761, 6181, 6601, 7021, 7441, 7861, 8281, 8701, 9121, 9541, 9961, 10381, 10801,
11221, 11641, 12061, 12481, 12901, 13321, 13741, 14161, 14581, 15001, 15421,
15841, 16261, 16681, 17101, 17521, 17941, 18361, 18781, 19201, 19621]
--
Yours,
Andrei
=====
Mail address in header catches spam. Real contact info (decode with rot13):
cebwrpg5 at bcrenznvy.pbz. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq gur
yvfg, fb gurer'f ab arrq gb PP.
More information about the Tutor
mailing list