ponderThis201004.frink

Download or view ponderThis201004.frink in plain text format

// IBM "Ponder This" challenge, 2010-04
//
// http://domino.research.ibm.com/Comm/wwwr_ponder.nsf/challenges/April2010.html
//
// The prime number 4535653, when translated to base 16, gives the hexadecimal
// number 0x453565, which has the same digits as the original number, omitting
// the last digit.

// Find another example of a prime number that, translated to hexadecimal,
// yields the same digits, omitting the last 21 digits.

//a = 1.09649*10^124
a = 10.^124
digits = 1
testNum[1e124, 1e135, digits]
//setPrecision[180]

testNum[lower, upper, digits] :=
{
   wasPos = false
   a = lower
   last = lower
   nextLast = lower
   lastDiff = a
//   setPrecision[digits+5]
   stepsize = 1.0 + 10^-(digits+3)
   println["Searching from\n$lower\n$upper"]
   println["Stepsize is $stepsize, digits are $digits"]
   do
   {
      fa = "$a"
      a = floor[a]
      astr = "$a"
      n = hex[a]

      atstr = left[astr, length[astr]-21]

      if length[n] == length[atstr] and left[n,digits] == left[atstr,digits]
      {
         diff = (parseInt[atstr,16] - a)
         println[]
         isPos = diff > 0
         println["\n$fa"]
         println[n]
         println[atstr]
         println[length[atstr] + " digits, diff = " + (1.0 diff)]
         if (!isPos)
            if wasPos
            {
               println["====================="]
               if (last != undef)
                  testNum[nextLast, a+100000000, digits+1]
               println["=======FAIL at $digits========="]
//               println["digits are $digits"]
//               setPrecision[digits + 5]
            }

         wasPos = isPos
      }
      nextLast = last
      last = a
      a = a * stepsize
   } while a<=upper
}


Download or view ponderThis201004.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 20143 days, 11 hours, 46 minutes ago.