Do you know where your variables are? PerlMonks

### Re: Recurring Cycle of Fractions (tiny)

by tye (Sage)
 on Sep 09, 2007 at 07:43 UTC ( #637891=note: print w/replies, xml ) Need Help??

in reply to Recurring Cycle of Fractions

Such boring limitations, as previously noted. But monstrously large floating points and regexes? Bah. It is simple enough to compute directly using boringly small integers:

```#!/usr/bin/perl -w
use strict;
my( \$num, \$den )= ( 1, 7, @ARGV )[2&@ARGV,-1];
my \$rem= \$num % \$den;
my %seen;
my \$rep= '';
while(  1  ) {
\$rem *= 10;
last
if  exists \$seen{\$rem};
\$seen{\$rem}= length( \$rep );
\$rep .= int( \$rem / \$den );
\$rem %= \$den;
}
substr( \$rep, 0, \$seen{\$rem} )= '';
print "\$num / \$den = ...\$rep\n";

Sample runs:

``` \$ perl repDig.pl 97
1 / 97 = ...010309278350515463917525773195876288659793
814432989690721649484536082474226804123711340206185567

\$ perl repDig.pl 2 97
2 / 97 = ...020618556701030927835051546391752577319587
628865979381443298969072164948453608247422680412371134

- tye

Replies are listed 'Best First'.
Re^2: Recurring Cycle of Fractions (tiny)
by Skeeve (Vicar) on Sep 09, 2007 at 07:56 UTC
Same as mine but much shorter! ++!

s\$\$([},&%#}/&/]+}%&{})*;#\$&&s&&\$^X.(\$'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`\$''`"e

Oh wow, so it is. Sorry, I didn't look at your code since you didn't mention that you had gone outside the parameters provided so I didn't think that you had done what I was doing.

In penance, here is a script that shows the repeat length for 1/\$n only when that length isn't "boring". If \$n is prime a repeat length of \$n-1 is "boring". If \$n is composite, the boring length is the max repeat length of its prime factors.

```#!/usr/bin/perl -w
use strict;

Main();
exit();

sub repDig
{
my( \$den, \$num )= @_;
\$num ||= 1;
my \$rem= \$num % \$den;
my %seen;
my \$rep= '';
while(  1  ) {
\$rem *= 10;
last
if  exists \$seen{\$rem};
\$seen{\$rem}= length( \$rep );
\$rep .= int( \$rem / \$den );
\$rem %= \$den;
}
substr( \$rep, 0, \$seen{\$rem} )= '';
return \$rep;
}

sub factor
{
my( \$r )= @_;
my \$f= '';
my @f;
my \$p= 2;
while( 1 < \$r ) {
my \$q= int( \$r / \$p );
last
if  \$q < \$p;
my \$e= 0;
while(  \$r == \$q*\$p  &&  \$q  ) {
\$e++;
\$r= \$q;
\$q= int( \$r / \$p );
}
if(  \$e  ) {
push @f, \$p;
\$f .= "*\$p";
\$f .= "^\$e"
if  1 < \$e;
}
\$p += 2==\$p ? 1 : 2;
}
if(  1 < \$r  ||  ! @f  ) {
push @f, \$r;
\$f .= "*\$r";
}
substr( \$f, 0, 1 )= '';
return \$f, @f;
}

sub Main
{
my \$dem= 1;
my %r;
while( 1 ) {
my( \$f, @f )= factor( ++\$dem );
my \$r= repDig( \$dem );
\$r= \$r ? length(\$r) : 0;
if(  \$f eq \$dem  ) {
\$r{\$dem}= \$r;
next
if  \$r == \$dem-1;
} else {
my \$max= 0;
for(  @f  ) {
\$max= \$r{\$_}
if  \$max < \$r{\$_};
}
next
if  \$r == \$max;
}
printf "%8d: 1/%s\n", \$r, \$f;
}
}

and the first few lines of output:

My favotite is: 42: 1/7^2

Now demonstrate your understanding by correctly predicting a number with a repeat length of 11 or 25.

- tye

Create A New User
Node Status?
node history
Node Type: note [id://637891]
help
Chatterbox?
 [Dumu]: Hello @jedikaiti & other Monks! [Dumu]: Can't rememeber how to correctly tag other monks in chat [Dumu]: hello jedikaiti

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-11-20 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (290 votes). Check out past polls.

Notices?