DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Pollard Rho Factoring Algorithm

Snippets Manager user avatar by
Snippets Manager
·
Sep. 14, 08 · · Code Snippet
Like (0)
Save
Tweet
812 Views

Join the DZone community and get the full member experience.

Join For Free
This is a ruby port of Patrick C. Konsor's Java implementation of Richard Brent's variant of Pollard's Rho algorithm.  This is probably the fastest factoring algorithm to date.

http://www.patrickkonsor.com/
http://www.arestaenterprise.com/


def gcd(b,m)
  while true do
    if m==0
      return b
    end
    r=b%m
    b=m
    m=r
  end
end

def f(x,n)
  ((x**2) + 1) % n
end
  
def factor(n)  
  y = (rand*100).to_i * n / 100
  m = (rand*100).to_i * n / 100
  r = q = 1
  begin
    x = y
    1.upto(r) do |i|
      y = f(y,n)
    end
    k = 0
    begin
      ys = y
      1.upto([r-k, m].min) do |j|
        y = f(y, n)
        q = (q * (x-y).abs) % n
      end
      g = gcd(q,n)
      k += m
    end while k < r and g <= 1
    r *= 2
  end while g <= 1

  if g == n
    begin
      ys = f(ys, n)
      g = gcd((x-ys).abs, n)
    end while g <= 1
  end
  g
end
Algorithm

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Generate Fake Test Data
  • Java Hashtable, HashMap, ConcurrentHashMap: Performance Impact
  • Your Old Laptop Is Your New Database Server
  • DZone's Article Submission Guidelines

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo