Difference between revisions of "Playing with Primes"

From PeformIQ Upgrade
Jump to navigation Jump to search
(New page: =Product of Adjacent Primes which are Palindromes= <pre> 2 * 3 = 6 7 * 11 = 77 17 * 19 = 323 191 * 193 = 36863 1051 * 1061 = 111...)
 
 
Line 12: Line 12:


<pre>
<pre>
#!/usr/bin/env python


import sys
primes = []
MAX    = 100000
#-------------------------------------------------------------------------------
def primes_to(N):
  for i in range(N):
      factored = False
      no = i + 2
      # print "Number -> %s" % no
      for divisor in primes:
        # print "Divisor -> %s" % divisor
        if divisor:
          if no % divisor == 0:
              factored = True
          if factored:
              break
      if not factored:
        # print "Prime -> %s" % no
        primes.append(no)
#-------------------------------------------------------------------------------
def reverse(n):
  s = "%s" % n
  l = len(s)
  r = []
  if l > 1:
      for i in range(l):
        r.append(s[l-i-1])
      rev = ''.join(r)
  else:
      rev = s
  # print "s -> %s" % s
  # print "r -> %s" % rev
  return int(rev)
#-------------------------------------------------------------------------------
primes_to(MAX)
for i in range(len(primes)):
  try:
      product =  primes[i] * primes[i+1]
  except:
      continue
  rev = reverse(product)
  if rev == product:
      sys.stderr.write("%5s * %5s = %10s\n" % (primes[i], primes[i+1], product))
      sys.stderr.write("^^^^^^^^^^^^^^^^^^^^^^^^^^^\n")
      sys.stderr.flush()
#-------------------------------------------------------------------------------
</pre>
</pre>


[[Category:Python]]
[[Category:Python]]

Latest revision as of 08:51, 30 March 2009

Product of Adjacent Primes which are Palindromes

    2 *     3 =          6
    7 *    11 =         77
   17 *    19 =        323
  191 *   193 =      36863
 1051 *  1061 =    1115111

The Code

#!/usr/bin/env python

import sys

primes = []

MAX    = 100000

#-------------------------------------------------------------------------------

def primes_to(N):
   for i in range(N):
      factored = False
      no = i + 2
      # print "Number -> %s" % no
      for divisor in primes:
        # print "Divisor -> %s" % divisor
        if divisor:
           if no % divisor == 0:
              factored = True
           if factored:
              break
      if not factored:
         # print "Prime -> %s" % no
         primes.append(no)

#-------------------------------------------------------------------------------

def reverse(n):
   s = "%s" % n
   l = len(s)
   r = []

   if l > 1:
      for i in range(l):
         r.append(s[l-i-1])
      rev = ''.join(r)
   else:
      rev = s

   # print "s -> %s" % s
   # print "r -> %s" % rev
 
   return int(rev)

#-------------------------------------------------------------------------------

primes_to(MAX)

for i in range(len(primes)):
   try:
      product =  primes[i] * primes[i+1]
   except:
      continue

   rev = reverse(product)

   if rev == product:
      sys.stderr.write("%5s * %5s = %10s\n" % (primes[i], primes[i+1], product))
      sys.stderr.write("^^^^^^^^^^^^^^^^^^^^^^^^^^^\n")
      sys.stderr.flush()

#-------------------------------------------------------------------------------