Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

RSA Factoring challenge

by one4k4 (Hermit)
on Jul 25, 2001 at 23:21 UTC ( [id://99779]=perlmeditation: print w/replies, xml ) Need Help??

Located Here. I wasnt sure if this was the correct place to place this, but I figured I would give it a go.
I'm curious about this, and maybe it's worthy of a little game of golf. Of course, given the specs by RSA, the amount of power required to do this challenge, is well, beyond me. Nonetheless, its still intreguing to the crypographic mind, and I just find it overall interesting. It applies to Perl, in my mind, because I -know- there's gotta be a way to do it with Perl. :)

_14k4 - perlmonks@poorheart.com (www.poorheart.com)

Replies are listed 'Best First'.
(MeowChow) Re: RSA Factoring challenge
by MeowChow (Vicar) on Jul 26, 2001 at 00:25 UTC
    Algorithmic efficiency and golf are not good friends. 33 chars, assuming you only want two prime factors:
      
    sub factor { $n=$p=pop;1while$n%--$p;$n/$p,$p; }
    Update: Thanks to CheeseLord for pointing out that the trailing semicolon on this golf is entirely unnecessary, without merit, sans raison d'etre, serving absolutely no purpose, whatsoever, amen.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      Not to be overly nitpicky, but this does not produce prime factors with any certainty. The first factor it provides is prime, the second only rarely is prime. So once you get the first prime factor, you have to run the second factor through this again until the second return value is 1.

      Update: I somehow missed the part of the RSA challenge where they stated that the numbers they pose have only two prime factors, and for that, there indeed would be no need to calculate past one iteration.

      And just so I'm not whining without contributing, here's a look at obtaining all the factors with the modulus routine. ;)
      #!/usr/bin/perl -w use strict; #note that this code is brutally slow my $number = shift; #assume user knows to input a number my @all_factors = factor( $number ); my $check_factor = 0; while ($check_factor != 1 ) { $check_factor = pop @all_factors; push( @all_factors, factor( @check_factor ) ); } if( @all_factors + 0 == 1 ) { print "$number is prime\n"; } else { print "$number: " .join( ',', @all_factors ). "\n"; } exit; sub factor { #one slight change to prevent an illegal modulus operation my $n; my $p; $n=$p=pop; return if( $n ==1 ); 1 while $n%--$p; $n/$p,$p; }
        What I meant, which I suppose could have been stated more clearly, is that the golf works for any number which has exactly two primes as its factors, as is certainly* the case for the numbers given in the RSA challenge.

        * For sufficiently high and practical values of certainty

        Update: here's a recursive version that does what you're looking for at 52 chars:
          
        sub f { my$n=my$p=pop;1while$n%--$p;$p>1?(f($n/$p),f($p)):$n }

           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
Re: RSA Factoring challenge
by Nitsuj (Hermit) on Jul 26, 2001 at 15:44 UTC
    Well, you only need one number since the RSA numbers are factors of 2 primes.
    Oh Yeah, and you only need every other number, since you know that it isn't prime if it's even.
    Also, you don't need to count for 2, because you can check to see if the last number is even, and then the challenge wouldn't be very hard.

    $p=<the number>;$r=3;while((p%r)!=0)r+=2; <==Unreadably

    **READABLY**
    $p=<the number>;
    $r=3;
    while((p%r)!=0)r+=2;
    **READABLY**


    Not such a bad solution character count wise.

    Just Another Perl Backpacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://99779]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2024-04-23 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found