[LON-CAPA-users] prime factorization
Justin Gray
lon-capa-users@mail.lon-capa.org
Thu, 20 Mar 2008 10:49:36 -0700
------=_Part_26249_24530115.1206035376999
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Excellent! Thanks, Gerd!
On Thu, Mar 20, 2008 at 9:50 AM, Gerd Kortemeyer <korte@lite.msu.edu> wrote:
> Hi,
>
> On Mar 19, 2008, at 6:39 PM, Justin Gray wrote:
> >
> > However, I do not know how to modify this problem to test whether
> > an integer has been factored into primes. The example below will
> > accept 60 as a correct answer. :(
> >
> > <problem>
> > <script type="loncapa/perl">
> > $answer = "2^2*3*5";
> > </script>
> > <startouttext />
> > Find the prime factorization: <p></p>
> >
> > <m>$60 = $</m>
> > <endouttext />
> >
> > <mathresponse cas="maxima" answerdisplay="$answer" args="$answer">
> > <answer>is(RESPONSE[1] = LONCAPALIST[1]);</answer>
> >
> >
> > <textline readonly="no" size="40" />
> > </mathresponse>
> > </problem>
>
> I should have read this in order. The example below is probably more
> what you want than what I sent yesterday - it'll accept "5*3*2^2" or
> "2*3*5*2", but not "60" or "4*15":
>
> <problem>
> <script type="loncapa/perl">@primes=(2,3,5,7,11,13,17,19,23); # prime
> numbers
> $nfactors=&random(3,6,1); # number of factors
> $number=1;
> @factors=();
> for ($i=1;$i<=$nfactors;$i++) {
> $newprime=$primes[&random(0,$#primes,1)];
> $number*=$newprime;
> push(@factors,$newprime);
> }
> $answer=join('*',sort{$a<=>$b}(@factors));</script>
> <startouttext />Factorize $number into primes.<endouttext />
> <customresponse answerdisplay="$answer">
> <answer type="loncapa/perl">if ($submission!~/^[\d\s\*\^]+$/)
> { return 'WANTED_NUMERIC'; }
> $submission=~s/(\d+)\^(\d+)/&expanded($1,$2)/ge;
> @submission=sort(split(/\s*\*\s*/,$submission));
> @answer=sort(@factors);
> if ($#answer!=$#submission) { return 'INCORRECT'; }
> for ($i=0;$i<=$#answer;$i++) {
> if ($answer[$i]!=$submission[$i]) { return 'INCORRECT'; }
> }
> return 'EXACT_ANS';
>
> sub expanded {
> my ($n,$e)=@_;
> my $output=$n;
> for (my $i=2;$i<=$e;$i++) {
> $output.='*'.$n;
> }
> return $output;
> }</answer>
> <textline readonly="no" />
> </customresponse>
> </problem>
> _______________________________________________
> LON-CAPA-users mailing list
> LON-CAPA-users@mail.lon-capa.org
> http://mail.lon-capa.org/mailman/listinfo/lon-capa-users
>
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Justin Gray Tel. 778-782-4237
Dept. of Mathematics Fax. 778-782-4947
Simon Fraser University
8888 University Drive
Burnaby, B.C. V5A 1S6
Canada
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
------=_Part_26249_24530115.1206035376999
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Excellent! Thanks, Gerd!<br><br><div class="gmail_quote">On Thu, Mar 20, 2008 at 9:50 AM, Gerd Kortemeyer <<a href="mailto:korte@lite.msu.edu">korte@lite.msu.edu</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
<div class="Ih2E3d"><br>
On Mar 19, 2008, at 6:39 PM, Justin Gray wrote:<br>
><br>
> However, I do not know how to modify this problem to test whether<br>
> an integer has been factored into primes. The example below will<br>
> accept 60 as a correct answer. :(<br>
><br>
> <problem><br>
> <script type="loncapa/perl"><br>
> $answer = "2^2*3*5";<br>
> </script><br>
> <startouttext /><br>
> Find the prime factorization: <p></p><br>
><br>
> <m>$60 = $</m><br>
> <endouttext /><br>
><br>
> <mathresponse cas="maxima" answerdisplay="$answer" args="$answer"><br>
> <answer>is(RESPONSE[1] = LONCAPALIST[1]);</answer><br>
><br>
><br>
> <textline readonly="no" size="40" /><br>
> </mathresponse><br>
> </problem><br>
<br>
</div>I should have read this in order. The example below is probably more<br>
what you want than what I sent yesterday - it'll accept "5*3*2^2" or<br>
"2*3*5*2", but not "60" or "4*15":<br>
<br>
<problem><br>
<div class="Ih2E3d"><script type="loncapa/perl">@primes=(2,3,5,7,11,13,17,19,23); # prime<br>
numbers<br>
$nfactors=&random(3,6,1); # number of factors<br>
$number=1;<br>
@factors=();<br>
for ($i=1;$i<=$nfactors;$i++) {<br>
$newprime=$primes[&random(0,$#primes,1)];<br>
$number*=$newprime;<br>
push(@factors,$newprime);<br>
}<br>
$answer=join('*',sort{$a<=>$b}(@factors));</script><br>
</div><startouttext />Factorize $number into primes.<endouttext /><br>
<customresponse answerdisplay="$answer"><br>
<answer type="loncapa/perl">if ($submission!~/^[\d\s\*\^]+$/)<br>
{ return 'WANTED_NUMERIC'; }<br>
$submission=~s/(\d+)\^(\d+)/&expanded($1,$2)/ge;<br>
<div class="Ih2E3d">@submission=sort(split(/\s*\*\s*/,$submission));<br>
@answer=sort(@factors);<br>
if ($#answer!=$#submission) { return 'INCORRECT'; }<br>
for ($i=0;$i<=$#answer;$i++) {<br>
if ($answer[$i]!=$submission[$i]) { return 'INCORRECT'; }<br>
}<br>
return 'EXACT_ANS';<br>
<br>
</div>sub expanded {<br>
my ($n,$e)=@_;<br>
my $output=$n;<br>
for (my $i=2;$i<=$e;$i++) {<br>
$output.='*'.$n;<br>
}<br>
return $output;<br>
<div class="Ih2E3d">}</answer><br>
<textline readonly="no" /><br>
</customresponse><br>
</problem><br>
</div><div><div></div><div class="Wj3C7c">_______________________________________________<br>
LON-CAPA-users mailing list<br>
<a href="mailto:LON-CAPA-users@mail.lon-capa.org">LON-CAPA-users@mail.lon-capa.org</a><br>
<a href="http://mail.lon-capa.org/mailman/listinfo/lon-capa-users" target="_blank">http://mail.lon-capa.org/mailman/listinfo/lon-capa-users</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>Justin Gray Tel. 778-782-4237<br>Dept. of Mathematics Fax. 778-782-4947<br>Simon Fraser University<br>
8888 University Drive<br>Burnaby, B.C. V5A 1S6<br>Canada<br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
------=_Part_26249_24530115.1206035376999--