Perl-Sensitive Sunglasses PerlMonks

### Re^3: Recurring Cycle of Fractions (same)

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

in reply to Re^2: Recurring Cycle of Fractions (tiny)
in thread Recurring Cycle of Fractions

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:

```       0: 1/2
1: 1/3
0: 1/5
2: 1/11
6: 1/13
3: 1/3^3
15: 1/31
3: 1/37
5: 1/41
21: 1/43
42: 1/7^2
13: 1/53
3: 1/2*3^3
33: 1/67
35: 1/71
8: 1/73
13: 1/79
9: 1/3^4
41: 1/83
44: 1/89
42: 1/2*7^2
4: 1/101
34: 1/103
53: 1/107
3: 1/2^2*3^3
48: 1/7*17
22: 1/11^2
42: 1/127
3: 1/3^3*5
8: 1/137
46: 1/139
42: 1/3*7^2
75: 1/151
78: 1/157
66: 1/7*23
9: 1/2*3^4
81: 1/163
78: 1/13^2
43: 1/173
95: 1/191
42: 1/2^2*7^2
98: 1/197
99: 1/199
84: 1/7*29
30: 1/211
3: 1/2^3*3^3
30: 1/7*31
48: 1/13*17
113: 1/227
48: 1/2*7*17
7: 1/239
30: 1/241
22: 1/2*11^2
27: 1/3^5
42: 1/5*7^2
50: 1/251
3: 1/2*3^3*5
5: 1/271
69: 1/277
28: 1/281
141: 1/283
30: 1/7*41
272: 1/17^2
146: 1/293
42: 1/2*3*7^2
6: 1/3^3*11
66: 1/13*23
42: 1/7*43
153: 1/307
155: 1/311
79: 1/317
66: 1/2*7*23
144: 1/17*19
9: 1/2^2*3^4
138: 1/7*47
110: 1/331
78: 1/2*13^2
30: 1/11*31
294: 1/7^3
173: 1/347
116: 1/349
32: 1/353
48: 1/3*7*17
179: 1/359
342: 1/19^2
22: 1/3*11^2
78: 1/7*53
186: 1/373
84: 1/13*29
176: 1/17*23
42: 1/2^3*7^2
99: 1/397
200: 1/401
30: 1/13*31
9: 1/3^4*5
84: 1/2*7*29
6: 1/11*37
204: 1/409
174: 1/7*59
140: 1/421
215: 1/431
3: 1/2^4*3^3
30: 1/2*7*31
198: 1/19*23
219: 1/439
42: 1/3^2*7^2
48: 1/2*13*17
221: 1/443
32: 1/449
10: 1/11*41
152: 1/457
48: 1/3^3*17
154: 1/463
233: 1/467
66: 1/7*67
42: 1/11*43
48: 1/2^2*7*17
239: 1/479
66: 1/3*7*23
22: 1/2^2*11^2
27: 1/2*3^5
42: 1/2*5*7^2
112: 1/17*29
210: 1/7*71
78: 1/3*13^2
24: 1/7*73
52: 1/521
261: 1/523
240: 1/17*31

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://637924]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2016-07-01 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What is your favorite alternate name for a (specific) keyboard key?

Results (7 votes). Check out past polls.