Re: Midterm, question 13

0 views
Skip to first unread message

William Stein

unread,
Feb 17, 2012, 7:43:06 AM2/17/12
to Andrey Sarantsev, uw-sag...@googlegroups.com
On Thu, Feb 16, 2012 at 10:10 PM, Andrey Sarantsev <ansa...@gmail.com> wrote:
> So we just ignore comparison mod 0 with the first entry and do comparison
> mod 1 (which, BTW, always holds true),
> by 2 etc. ?

NO. The problem asks for n such that n = i (mod v[i]). As long as no
v[i] is 0, you will *never* have to reduce anything mod 0 ever.

> also, is it necessary to write efficient code or just brute force is OK?

You will want some level of efficiency. There is a builtin function
called CRT_list in Sage, which you may find useful.

> Best,
> Andrey.
>
>
> 2012/2/16 William Stein <wst...@gmail.com>
>>
>>
>> On Feb 16, 2012 7:26 PM, "Andrey Sarantsev" <ansa...@gmail.com> wrote:
>> >
>> > Dear Prof. Stein,
>> >
>> > what's wrong with this:
>> >
>> > def crt(v):
>> >         n = len(v)
>> >         for k in range(n):
>> >             if (v[k] == 0):
>> >                 return None
>> >         p = prod(v)
>> >         if p < 0:
>> >             p = -p
>> >         def if_crt(v, a):
>> >             bulean = True
>> >             n = len(v)
>> >             for k in range(n)
>> >                 if (a % k != v[k] % k):
>>
>> k=0 so v[k]%k will never work --- mod 0 is not defined.
>>
>> >                     bulean = False
>> >             return bulean
>> >         k = 0
>> >         while ((k < p) or if_crt(v, k)):
>> >             if (not(if_crt(v, k))):
>> >                 k = k + 1
>> >         if (k == p):
>> >             return None
>> >         else:
>> >             return k
>> >
>> >
>> > crt([3, 4, 5])
>> >
>> > Thanks,
>> > Andrey.
>
>

--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

William Stein

unread,
Feb 17, 2012, 3:12:15 PM2/17/12
to Andrey Sarantsev, uw-sag...@googlegroups.com
On Fri, Feb 17, 2012 at 12:09 PM, Andrey Sarantsev <ansa...@gmail.com> wrote:
> Dear Professor,
>
> the midterm.pdf reads 'all_composite' = True for the test example [1]*10.
> But the number 1 is neither prime nor composite!...

For the purposes of the midterm, please just consider 1 to be
composite (so you don't have to raise an error). For
the midterm, you may define composite to mean "not prime".

But yes, I completely agree -- the number 1 is neither prime nor composite.

Thanks,

-- William

>
> Andrey.
>
>
> 2012/2/17 William Stein <wst...@gmail.com>
>>
>> It works perfectly for me.  I just copied and pasted your code:
>>
>>
>> {{{id=9|
>> def div(a, b):
>>    try:
>>        c =float(a/b)
>>    except(ZeroDivisionError):
>>        return None
>>    return c
>>
>>
>> ///
>> }}}
>>
>> {{{id=8|
>> div(2, 0) is None
>> ///
>> True
>> }}}
>>
>> On Fri, Feb 17, 2012 at 10:28 AM, Andrey Sarantsev <ansa...@gmail.com>
>> wrote:
>> > Sir,
>> >
>> > I am trying to model raising an exception:
>> >
>> > def div(a, b):
>> >     try:
>> >         c =float(a/b)
>> >     except(ZeroDivisionError):
>> >         return None
>> >     return c
>> >
>> >
>> > div(2, 0)
>> >
>> > it does not work!
>> > could you help me?
>> >
>> > Andrey.
>> >
>> > 2012/2/17 William Stein <wst...@gmail.com>

Reply all
Reply to author
Forward
0 new messages