Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Recurring Cycle of Fractions (tiny)

by tye (Cardinal)
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        


Comment on Re: Recurring Cycle of Fractions (tiny)
Select or Download Code
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        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://637891]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2014-09-30 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (375 votes), past polls