[Tutor] Posting solutions to "homeworky" problems
Oscar Benjamin
oscar.j.benjamin at gmail.com
Wed Aug 14 14:40:11 CEST 2013
On 13 August 2013 20:27, Prasad, Ramit
<ramit.prasad at jpmorgan.com.dmarc.invalid> wrote:
> Peter Otten wrote:
>> Oscar Benjamin wrote:
>>
>> > I would post a snippet but isn't it generally a bit of a faux-pas to give
>> > solutions to these problems (I mean this as a real question)?
>>
>> In principle I agree, but sometimes I lack the discipline to hold back.
I agree that it's difficult (see below).
>> I think the code I gave is peculiar enough to make it easy to find for a
>> suspicious teacher hunting for cut-and-paste artists ;)
A teacher should know not to set this as a weighted assignment except
perhaps under exam conditions. I think I'd get into a lot of trouble
if I made my students do this in an exam: the current consensus is
that I'm not allowed to actually *require* original lateral thinking
from them unless it's coursework (so that they can cheat).
> Not to mention that since this is a Euler question (stated in the original
> post), optimized answers are easily found on the Internet. The tricky bit
> is in figuring the appropriate algorithm and not actually in the coding.
Well if the solutions are already on the internet I guess it doesn't
matter. More importantly if Peter gets to do it and Tim gets to do it
then it's just *so* unfair if I can't do it too!
So here's my own deliberately cryptic solution :)
def lcm_1_to_N(N):
sup = 1
logpn = 0
while sup < N:
sup *= 2
logpn += 1
lcm = 1
p1 = 1
nums = []
while p1 < N:
p1 += 1
flag = True
for p2 in nums:
if not p1 % p2:
flag = False
break
elif p2 ** 2 > p1:
break
if flag:
nums.append(p1)
inf = p1 ** logpn
while inf > N:
inf /= p1
logpn -= 1
lcm *= inf
return lcm
Oscar
More information about the Tutor
mailing list