[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">
> > </script>
> >     <startouttext />
> > Find the prime factorization: <p></p>
> >
> > <m>\$60 = \$</m>
> >     <endouttext />
> >
> >
> >
> >         <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);
> }
> <startouttext />Factorize \$number into primes.<endouttext />
> { return 'WANTED_NUMERIC'; }
> \$submission=~s/(\d+)\^(\d+)/&expanded(\$1,\$2)/ge;
> @submission=sort(split(/\s*\*\s*/,\$submission));
> if (\$#answer!=\$#submission) { return 'INCORRECT'; }
>     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;
>   </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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

------=_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 &lt;<a href="mailto:korte@lite.msu.edu">korte@lite.msu.edu</a>&gt; 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>
&gt;<br>
&gt; However, I do not know how to modify this problem to test whether<br>
&gt; an integer has been factored into primes. The example below will<br>
&gt; accept 60 as a correct answer. &nbsp;:(<br>
&gt;<br>
&gt; &lt;problem&gt;<br>
&gt; &lt;script type=&quot;loncapa/perl&quot;&gt;<br>
&gt; &lt;/script&gt;<br>
&gt; &nbsp; &nbsp; &lt;startouttext /&gt;<br>
&gt; Find the prime factorization: &lt;p&gt;&lt;/p&gt;<br>
&gt;<br>
&gt; &lt;m&gt;\$60 = \$&lt;/m&gt;<br>
&gt; &nbsp; &nbsp; &lt;endouttext /&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &lt;textline readonly=&quot;no&quot; size=&quot;40&quot; /&gt;<br>
&gt; &nbsp; &nbsp; &lt;/mathresponse&gt;<br>
&gt; &lt;/problem&gt;<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&#39;ll accept &quot;5*3*2^2&quot; or<br>
&quot;2*3*5*2&quot;, but not &quot;60&quot; or &quot;4*15&quot;:<br>
<br>
&lt;problem&gt;<br>
<div class="Ih2E3d">&lt;script type=&quot;loncapa/perl&quot;&gt;@primes=(2,3,5,7,11,13,17,19,23); # prime<br>
numbers<br>
\$nfactors=&amp;random(3,6,1); &nbsp;# number of factors<br>
\$number=1;<br>
@factors=();<br>
for (\$i=1;\$i&lt;=\$nfactors;\$i++) {<br>
&nbsp; &nbsp; &nbsp;\$newprime=\$primes[&amp;random(0,\$#primes,1)];<br>
&nbsp; &nbsp; &nbsp;\$number*=\$newprime;<br>
&nbsp; &nbsp; &nbsp;push(@factors,\$newprime);<br>
}<br>
</div>&lt;startouttext /&gt;Factorize \$number into primes.&lt;endouttext /&gt;<br>
{ return &#39;WANTED_NUMERIC&#39;; }<br>
\$submission=~s/(\d+)\^(\d+)/&amp;expanded(\$1,\$2)/ge;<br>
<div class="Ih2E3d">@submission=sort(split(/\s*\*\s*/,\$submission));<br>
if (\$#answer!=\$#submission) { return &#39;INCORRECT&#39;; }<br>
&nbsp; &nbsp; if (\$answer[\$i]!=\$submission[\$i]) { return &#39;INCORRECT&#39;; }<br>
}<br>
return &#39;EXACT_ANS&#39;;<br>
<br>
</div>sub expanded {<br>
&nbsp; &nbsp; my (\$n,\$e)=@_;<br>
&nbsp; &nbsp; my \$output=\$n;<br>
&nbsp; &nbsp; for (my \$i=2;\$i&lt;=\$e;\$i++) {<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\$output.=&#39;*&#39;.\$n;<br>
&nbsp; &nbsp; }<br>
&nbsp; &nbsp; return \$output;<br>