devices without division
Hi there,
How to write a device funciton without using a '/' operator
sub device_now($a, $b){
my ($a, $b)= [at] ;
<don't use $result=$a/$b; >
return $result;
}
&device_now(6,3);
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: devices without division
> How to write a device funciton without using a '/' operator
>
> sub device_now($a, $b){
> my ($a, $b)= [at] ;
>
> <don't use $result=$a/$b; >
>
> return $result;
>
> }
> &device_now(6,3);
That's "divide", not "device".
Not sure it's the solution that the professor had in mind, but you
can use exponentiation (in Perl written as **,
5**2=25,5**3=125,...).
It's enough to remember that
x ** -1 = 1 / x
and the rest is trivial.
Another option would be to use a numerical solution. Guess the
result, see whether it's too big or too small, change the guess
accordingly, check again and continue like this until the error is
small enough.
Jenda
===== Jenda [at] Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: devices without division
Here is the program
use strict;
use warnings;
my ($div,$reminder) =3D ÷_now(20,4);
print "Dividend: $div\nReminder: $reminder\n";
sub divide_now {
my ($a,$b) =3D [at] _;
my ($s,$n);
for($n=3D1;;$n++) {
$d+=3D$b;
$s =3D $a - ($n*$b);
return($n,$s) if ($s<$b);
}
}
*** If you still want to use without '*' ********
use strict;
use warnings;
my ($div,$reminder) =3D ÷_now(20,4);
print "Dividend: $div\nReminder: $reminder\n";
sub divide_now {
my ($a,$b) =3D [at] _;
my ($s,$n,$d);
for($n=3D1;;$n++) {
$d+=3D$b;
$s =3D $a-$d;
return($n,$s) if ($s<$b);
}
}
On Apr 20, 4:20=A0am, aichuab... [at] gmail.com (ai nguyen) wrote:
> Hi there,
>
> How to write a device funciton without using a '/' operator
>
> sub device_now($a, $b){
> =A0 my ($a, $b)=3D [at] ;
>
> =A0 =A0<don't use $result=3D$a/$b; >
>
> =A0 return $result;
>
> }
>
> &device_now(6,3);
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: devices without division
--000325558c36c454a204a15ec517
Content-Type: text/plain; charset=ISO-8859-1
On Wed, Apr 20, 2011 at 7:58 AM, Saran <mail2saravanan [at] gmail.com> wrote:
> Here is the program
>
> use strict;
> use warnings;
>
> my ($div,$reminder) = ÷_now(20,4);
Please don't call subs with &; It'll bite you in the arse eventually. You
can read why in perldoc perlsub[0], or well, this mailing list.
Brian.
[0] http://perldoc.perl.org/perlsub.html
--000325558c36c454a204a15ec517--
Re: devices without division
On Wed, Apr 20, 2011 at 03:58, Saran <mail2saravanan [at] gmail.com> wrote:
> Here is the program
>
> use strict;
> use warnings;
>
> my ($div,$reminder) =3D ÷_now(20,4);
> print "Dividend: $div\nReminder: $reminder\n";
>
> sub divide_now {
> =A0 =A0 =A0 =A0my ($a,$b) =3D [at] _;
>
> =A0 =A0 =A0 =A0my ($s,$n);
> =A0 =A0 =A0 =A0for($n=3D1;;$n++) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$d+=3D$b;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$s =3D $a - ($n*$b);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return($n,$s) if ($s<$b);
>
> =A0 =A0 =A0 =A0}
> }
>
>
> *** If you still want to use without '*' ********
>
> use strict;
> use warnings;
>
> my ($div,$reminder) =3D ÷_now(20,4);
> print "Dividend: $div\nReminder: $reminder\n";
>
> sub divide_now {
> =A0 =A0 =A0 =A0my ($a,$b) =3D [at] _;
>
> =A0 =A0 =A0 =A0my ($s,$n,$d);
> =A0 =A0 =A0 =A0for($n=3D1;;$n++) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$d+=3D$b;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$s =3D $a-$d;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return($n,$s) if ($s<$b);
> =A0 =A0 =A0 =A0}
> }
>
>
>
>
>
> On Apr 20, 4:20=A0am, aichuab... [at] gmail.com (ai nguyen) wrote:
>> Hi there,
>>
>> How to write a device funciton without using a '/' operator
>>
>> sub device_now($a, $b){
>> =A0 my ($a, $b)=3D [at] ;
>>
>> =A0 =A0<don't use $result=3D$a/$b; >
>>
>> =A0 return $result;
>>
>> }
>>
>> &device_now(6,3);
I think that the variables "a" and "b" should be left reserved for Perl sor=
ting.
http://perldoc.perl.org/functions/sort.html
Ken Wolcott
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/