<problem>
<script type="loncapa/perl"># Construct an Egyptian Fraction that can be represented by three terms with denominators between 3 and 12
@denominators=(&random_permutation(&random(1,1000,1),(3..12)))[0..2];
$egyptian='1/'.(join('+1/',sort{$a<=>$b}(@denominators)));
$possible="A possible solution is $egyptian";
# Let the CAS figure out the value
$solution=&cas('maxima',$egyptian);
# Subroutine that checks if the provided term is indeed an Egyptian Fraction
sub analyze {
my ($expression)=@_;
$expression=~s/\s//gs;
$expression=~s/\+?1\//\,/gs;
if ($expression=~/^(\,[0-9]+)+$/) {
# Format is indeed 1/n+1/m+...
$last=-1;
foreach $number (sort { $a<=>$b } split(/\,/,$expression)) {
# Is a number used twice?
if ($last==$number) { return(0,1); }
$last=$number;
}
return(0,0);
}
return(1,0);
}
</script>
<startouttext />
Write $solution as an Egyptian Fraction<br />
<endouttext />
<customresponse answerdisplay="$possible" id="11">
<answer type="loncapa/perl"># Analyze the format
($formaterror,$doubleerror)=&analyze($submission);
if ($formaterror || $doubleerror) { return 'BAD_FORMULA'; }
# It is an Egyptian Fraction, is the value correct?
if (&cas('maxima',$submission.'-('.$egyptian.')') eq '0') {
return 'EXACT_ANS';
}
return 'INCORRECT';</answer>
<textline readonly="no" size="40" />
<customhint name="format" id="12">
<answer type="loncapa/perl">if ((&analyze($submission))[0]) { return 'EXACT_ANS'; }
return 'INCORRECT';</answer>
</customhint>
<hintpart on="format">
<startouttext />
<p>
Egyptian Fractions have a format of 1/number+1/number+..., for example 1/3+1/17+1/52.
</p>
<endouttext />
</hintpart>
<customhint name="double" id="13">
<answer type="loncapa/perl">if ((&analyze($submission))[1]) { return 'EXACT_ANS'; }
return 'INCORRECT';</answer>
</customhint>
<hintpart on="double">
<startouttext />
<p>
Egyptian Fractions cannot have the same denominator more than once. For example, 1/3+1/17+1/4+/17 is not an Egyptian Fraction, as it has 17 twice as denominator.
</p>
<endouttext />
</hintpart>
</customresponse>
</problem>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>