Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Recurring Cycle of Fractions (tiny)

by Skeeve (Vicar)
on Sep 09, 2007 at 07:56 UTC ( #637892=note: print w/ replies, xml ) Need Help??


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

Same as mine but much shorter! ++!


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


Comment on Re^2: Recurring Cycle of Fractions (tiny)
Select or Download Code
Replies are listed 'Best First'.
Re^3: Recurring Cycle of Fractions (same)
by tye (Cardinal) on Sep 09, 2007 at 18:37 UTC

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2015-07-31 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (275 votes), past polls