P is for Practical PerlMonks

### Comment on

 Need Help??
```#! /usr/bin/perl -w
use strict;
while (1) {
print "Please enter a number or expression: ";
my \$num = eval(scalar <STDIN>);
print "How many iterations: ";
chomp(my \$count = <STDIN>);
print "Doing \$count iterations of approximations to \$num.\n";
my \$f = ret_frac_iter(\$num);
for (1..\$count) {
my (\$n, \$m) = \$f->();
my \$approx = \$n/\$m;
print "\$n/\$m  =  \$approx\n";
}
print "\n";
}

# Takes a number, returns the best integer approximation and  (in list
# context) the error.
sub best_int {
my \$x = shift;
my \$approx = sprintf '%.0f', \$x;
if (wantarray) {
return (\$approx, \$x - \$approx);
}
else {
return \$approx;
}
}

# Takes a numerator and denominator, in scalar context returns
# the best fraction describing them, in list the numerator and
# denominator
sub frac_standard {
my \$n = best_int(shift);
my \$m = best_int(shift);
my \$k = gcd(\$n, \$m);
\$n /= \$k;
\$m /= \$k;
if (\$m < 0) {
\$n *= -1;
\$m *= -1;
}
if (wantarray) {
return (\$n, \$m);
}
else {
return "\$n/\$m";
}
}

# Euclidean algorithm for calculating a GCD.
# Takes two integers, returns the greatest common divisor.
sub gcd {
my (\$n, \$m) = @_;
while (\$m) {
my \$k = \$n % \$m;
(\$n, \$m) = (\$m, \$k);
}
return \$n;
}

# Takes a list of terms in a continued fraction, and converts it
# into a fraction.
sub ints_to_frac {
my (\$n, \$m) = (0, 1); # Start with 0
while (@_) {
my \$k = pop;
if (\$n) {
# Want frac for \$k + 1/(\$n/\$m)
(\$n, \$m) = frac_standard(\$k*\$n + \$m, \$n);
}
else {
# Want \$k
(\$n, \$m) = frac_standard(\$k, 1);
}
}
return frac_standard(\$n, \$m);
}

# Takes a number, returns an anon sub which iterates through a set of
# fractional approximations that converges very quickly to the number.
sub ret_frac_iter {
my \$x = shift;
my \$term_iter = ret_next_term_iter(\$x);
my @ints;
return sub {
push @ints, \$term_iter->();
return ints_to_frac(@ints);
}
}

# terms of a continued fraction converging on that number.
sub ret_next_term_iter {
my \$x = shift;
return sub {
(my \$n, \$x) = best_int(\$x);
if (0 != \$x) {
\$x = 1/\$x;
}
return \$n;
};
}

In reply to Continued Fractions by tilly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Please read these before you post! —
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.

Create A New User
Chatterbox?
and the leaves swirl about...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-07-19 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

Results (419 votes). Check out past polls.

Notices?