Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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! ++!


Replies are listed 'Best First'.
Re^3: Recurring Cycle of Fractions (same)
by tye (Sage) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://637892]
[robby_dobby]: This anonymonk's post reads weird to me
[robby_dobby]: Other than the content, that Chrome would not allow submitting code sounds like something funky going on there :P
[erix]: lazyness-driven inventio :)
[erix]: "the dog ate my homework"
[Eily]: looks more like "all my plates are broken and I don't understand why. Also I can't retrieve the broken parts because the elephant in the room is on the way"
[ambrus]: Eily: also webpages aren't displayed on my monitor no matter what I click with the mouse, and there's a blackout right now, could that be a problem?

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2017-04-25 13:07 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (453 votes). Check out past polls.