17
\$\begingroup\$

The task is to find a non-trivial factor of a composite number.

Write code that finds a non-trivial factor of a composite number as quickly as possible subject to your code being no more than 140 bytes long. The output should just be the factor you have found.

Your code can take input and give output in any way that is convenient including for example as arguments to a function.

Test cases which list all the factors (you only need to output one)

187: 11 17
1679: 23 73
14369648346682547857: 1500450271 9576890767
34747575467581863011: 3628273133 9576890767
52634041113150420921061348357: 2860486313 5463458053 3367900313
82312263010898855308580978867: 264575131106459 311111111111113
205255454905325730631914319249: 2860486313 71755440315342536873 
1233457775854251160763811229216063007: 1110111110111 1000000000063 1111111999999
1751952685614616185916001760791655006749: 36413321723440003717 48112959837082048697

I won't score your answer on the following difficult test case which may be of interest for testing:

513231721363284898797712130584280850383: 40206835204840513073 12764787846358441471

Score

Your score is the combined time to factor all the test cases above with a penalty of 10 minutes for each failed factorization (all rounded to the nearest second). Your code should work for other integers too, that is shouldn't just hardcode these answers.

I will stop your code after 10 minutes.

If two people get the same score then the first answer wins.

Restrictions

Your code can't use any builtin or library function that performs integer factorization. You can assume the input takes less than 256 bits. All input numbers will be composite.

How will I time?

I will literally run time ./myprog on my Ubuntu system to do the timing so please also supply a complete program for me to run that includes any function you have defined.

A note for compiled languages

Compilation time must take no more than 1 minute on my machine.

Is it actually possible?

If you ignore the space constraints, then each one can be factored in less than 2 seconds on my computer using pure Python code + pypy.

So what is a non-trivial factoring algorithm?

Pollard's rho algorithm is fast and suitable for golfing. Of course there are plenty of other ways to factor an integer as well.

Even faster is the Quadratic sieve. It looks like a serious challenge to squeeze that into 140 bytes.

Leading scores

  • SEJPM, 10 minutes penalty for the last test case + 16 seconds in Haskell
\$\endgroup\$
  • \$\begingroup\$ So, we may be given a number like 2 ** 1024? \$\endgroup\$ – Conor O'Brien Jul 8 '17 at 19:23
  • \$\begingroup\$ @ConorO'Brien You won't be given anything with more digits than the test cases. \$\endgroup\$ – user9206 Jul 8 '17 at 19:35
  • \$\begingroup\$ So, in terms of precision, nothing more than 256 bits. \$\endgroup\$ – Conor O'Brien Jul 8 '17 at 19:37
  • \$\begingroup\$ For an input like 4, should the output be 2 or 2, 2? \$\endgroup\$ – Mr. Xcoder Jul 8 '17 at 20:37
  • 1
    \$\begingroup\$ @AndersKaseorg I updated the question following your suggestion. Thanks. \$\endgroup\$ – user9206 Jul 9 '17 at 6:05
9
\$\begingroup\$

Haskell, 100 97 91 89 87 72 67 Bytes

Try It Online!

-3 bytes thanks to @flawr
-6 bytes thanks to @flawr again
-2 bytes thanks to @flawr yet again
-2 bytes thanks to an optimized set of parameters
-1 byte thanks to @flawrs yet another time
-14 bytes thanks to requirement to only having to output one factor
-5 bytes thanks to @AndersKaseorg

f n|let s x=mod(x*x+7)n;a#b|d<-gcd(b-a)n,d>1=d|c<-s b=s a#s c=5#s 5

This works for the first 5 test cases in unnoticable time.
This will probably time-out on the largest test case.

In general this will usually return one non-trivial factor in time proportional to the square root of the smallest factor.
It will not work on every input because it doesn't vary the polynomial and detection of the exceptional case is hard to do in 140 bytes.
It will also not output the full factorization, but rather a non-trivial factor and the division of the input by this factor.
It will also not sort the factors by size.

The method used is Pollard-Rho-Factoring with the standard starting value of 2 (with the standard x^2+1 polynomial applied once) and the non-standard polynomial constant factor of 7 (because 1 didn't work with 1679) for all further evaluations.

Full program (factor.hs):

import System.Environment(getArgs)

f n|let s x=mod(x*x+7)n;a#b|d<-gcd(b-a)n,d>1=d|c<-s b=s a#s c=5#s 5

main= do
      args <- getArgs
      print$f (read $ head args :: Integer)

Compile as $ ghc factor.hs (needs ghc installed).
Run as $ ./factor <number>.

Example run:

$ ./factor 187
11

Ungolfed code:

f n=g 5 (s 5)
   where s x=mod(x*x+7)n
         g a b = if d>1 then d else g(s a)(s(s b))
               where d=gcd(b-a)n

Computes the non-trivial factor by calling g with initial values. The polynomial is pre-applied on 2 here and re-applied on the result (5) so that the input to g (in a "where" clause) can always be readily used for the gcd-test. g (golfed version uses infix #) then tries to compute a non-trivial factor d (in the where clause in the un-golfed version, in-line in the golfed one) as the difference between the two inputs to g, if it succeeds returns said factor, else retries. Here it may generate n as the output if a==b and thus returns only a trivial factor, the proper approach to handle this would be to either vary the starting values upon this event or change the polynomial.

\$\endgroup\$
  • \$\begingroup\$ |1<2=s a#(s$s b) could be replaced with |c<-s b=s a#s c I think :) (also: why don't you post a TIO link?) \$\endgroup\$ – flawr Jul 8 '17 at 23:14
  • \$\begingroup\$ I updated the question following the comment suggestions. Now you only need to output one factor and the numbers are guaranteed to be composite. \$\endgroup\$ – user9206 Jul 9 '17 at 5:47
  • 3
    \$\begingroup\$ PS: why are we golfing, this isn't even code-golf \$\endgroup\$ – flawr Jul 9 '17 at 9:05
  • 4
    \$\begingroup\$ You now have 53 bytes in which to implement an even more sophisticated factoring algorithm :) \$\endgroup\$ – user9206 Jul 9 '17 at 11:55
  • 1
    \$\begingroup\$ Also you can take out abs , since b is always nonnegative. (Perhaps you meant abs$b-a, but gcd accepts negative arguments and always produces a nonnegative result.) That brings this down to less than half a tweet! \$\endgroup\$ – Anders Kaseorg Jul 10 '17 at 5:33
5
+100
\$\begingroup\$

Pari/GP, 137 bytes, ~5 seconds

Using GP's built-in elliptic curve operations (and some underhanded parameter tuning):

ecm(n)=iferr(if(n%2==0,2,n%3==0,3,for(a=1,n,ellmul(ellinit(Mod([a,a^2-a-1],n)),[1,a],lcm([1..ceil(4^a^0.5)])))),e,gcd(n,lift(Vec(e)[3])))

ecm is a function that (ought to) return a factor. Try it online!

Test:

ecm(n)=iferr(if(n%2==0,2,n%3==0,3,for(a=1,n,ellmul(ellinit(Mod([a,a^2-a-1],n)),[1,a],lcm([1..ceil(4^a^0.5)])))),e,gcd(n,lift(Vec(e)[3])))

{
ns = [
  187,
  1679,
  14369648346682547857,
  34747575467581863011,
  52634041113150420921061348357,
  82312263010898855308580978867,
  205255454905325730631914319249,
  1233457775854251160763811229216063007,
  1751952685614616185916001760791655006749
  ]
}

test(n) = {
    d = ecm(n);
    if (!(1<d && d<n && n%d==0), error(d));
    print(n, ": ", d)
}

apply(test, ns)

quit

Ungolfed:

ecm(n) = {
  iferr(if(n%2 == 0, 2,
           n%3 == 0, 3,
           for(a = 1, n,
               /* x^3 + A*x + B = y^2 */
               E = ellinit(Mod([a, a^2-a-1], n)); /* [A, B] */
               x0 = [1, a]; /* [x, y] */
               B = ceil(4^a^0.5); /* ~ exp(sqrt(log(p))), p ~= exp(a) */
               print("a=", a, ", B=", B);
               ellmul(E, x0, lcm([1..B]))
              )
          ),
         ERR, gcd(n, lift(Vec(ERR)[3] /* = Mod(d, n) */)),
         errname(ERR)=="e_INV")
}

Sadly, handling the factors 2 and 3 uses many bytes. Bytes that could have been used to add a stage 2:

ecm(n)=iferr(for(a=1,n,Y=X=ellmul(E=ellinit(Mod([a,1],n)),[0,1],(B=ceil(4^a^0.5))!);for(z=0,9*B,Y=elladd(E,Y,X))),e,gcd(n,lift(Vec(e)[3])))
\$\endgroup\$
1
\$\begingroup\$

Axiom, 137 bytes 9 minutes

p(n:PI):PI==(j:=1;a:=3;s:=n^.2;repeat(b:=j:=nextPrime(j);repeat(b<s=>(b:=b*j);break);a:=powmod(a,b,n);d:=gcd(a-1,n);d>1 or j>n=>break);d)

above the function p() that would implement p-1 algo for factoring below what to copy in a file for test on p() function

-- one has to copy this below text in a file name for example file.input
-- in one window where there is Axiom one could write 
-- )read C:\absolutepathwherethereisthatfile\file
-- and call the function test()
-- test()
-- the first character of all function and array must be afther a new line "\n"
)cl all
)time on
vA:=[187,1679,14369648346682547857,34747575467581863011,52634041113150420921061348357,82312263010898855308580978867,205255454905325730631914319249,1233457775854251160763811229216063007, 1751952685614616185916001760791655006749]

p(n:PI):PI==(j:=1;a:=3;s:=n^.2;repeat(b:=j:=nextPrime(j);repeat(b<s=>(b:=b*j);break);a:=powmod(a,b,n);d:=gcd(a-1,n);d>1 or j>n=>break);d)

-- this would try to factor n with p-1 Pollard method
pm1(n:PI):PI==
   j:=1;a:=3;s:=n^.2
   repeat
      b:=j:=nextPrime(j)
      repeat(b<s=>(b:=b*j);break)
      a:=powmod(a,b,n)
      d:=gcd(a-1,n);d>1 or j>n=>break
   d

test()==(for i in 1..#vA repeat output [vA.i, p(vA.i)])

results here:

(5) -> test()
   [187,11]
   [1679,73]
   [14369648346682547857,9576890767]
   [34747575467581863011,9576890767]
   [52634041113150420921061348357,2860486313]
   [82312263010898855308580978867,311111111111113]
   [205255454905325730631914319249,2860486313]
   [1233457775854251160763811229216063007,1111111999999]
   [1751952685614616185916001760791655006749,36413321723440003717]
                                                               Type: Void
                              Time: 496.78 (EV) + 53.05 (GC) = 549.83 sec
\$\endgroup\$
  • \$\begingroup\$ Could you spell out exactly how to run this code from the command line in ubuntu please? I have installed axiom and made a file called foo.ax with your non-golfed code in it. \$\endgroup\$ – user9206 Aug 7 '17 at 17:34
  • \$\begingroup\$ @Lembik 1)rename fop.ax in foo.input 2) run Axiom in one terminal or xterm 3) write in that Axiom terminal the follow command ")read C:absolutepath\foo" 4) write in the terminal of Axiom the call to function test(). This is how to do in Windows, the clue it seems to me open one Axiom session and load the file with ")read" command \$\endgroup\$ – RosLuP Aug 7 '17 at 20:44
  • \$\begingroup\$ @Lembik if there are problem with files I think it would be ok too: 1) run Axiom 2) write )time on <return> in Axiom program 3) copy paste and press return in each "copy paste" in Axiom program: the array vA, the function p() and test() 4) in the Axiom program write test()<return> \$\endgroup\$ – RosLuP Aug 9 '17 at 4:31
  • \$\begingroup\$ @Lembik so what time it takes? \$\endgroup\$ – RosLuP Aug 29 '17 at 7:21
1
\$\begingroup\$

Axiom, 10 minutes + 31 seconds

A(a)==>a:=(a*a+7)rem n;z(n)==(p:=a:=b:=101;for i in 1..repeat(A(a);A(b);A(b);p:=mulmod(p,a-b,n);i rem 999<9=>(p:=gcd(p,n);p>1=>break));p)

z() is the function rho, one 137 bytes function; ungolfed z() and call it as rho(). It would suppose that gcd(0,n)=n so the loop stop and return for fail n.

)time on    
rho(n)==
  p:=a:=b:=101
  for i in 1..repeat
          A(a);A(b);A(b)
          p:=mulmod(p,a-b,n)
          i rem 999<9=>(p:=gcd(p,n);p>1=>break)
  p

va1:=[187,1679,14369648346682547857,34747575467581863011,52634041113150420921061348357,82312263010898855308580978867,205255454905325730631914319249,1233457775854251160763811229216063007, 1751952685614616185916001760791655006749]
p1()==(for i in 1..#va1-1 repeat output [va1.i,z(va1.i)]) 

results (z() is ok to all but the last number 1751952685614616185916001760791655006749 remain not factored (10 minutes) )

(6) -> p1()
   [187,17]
   [1679,23]
   [14369648346682547857,1500450271]
   [34747575467581863011,3628273133]
   [52634041113150420921061348357,2860486313]
   [82312263010898855308580978867,264575131106459]
   [205255454905325730631914319249,2860486313]
   [1233457775854251160763811229216063007,1111111999999]
                                                               Type: Void
                                 Time: 30.38 (EV) + 1.38 (GC) = 31.77 sec

(8) -> z(1679)
   (8)  23
                                                    Type: PositiveInteger
                                                              Time: 0 sec
\$\endgroup\$
0
\$\begingroup\$

Python 3, 100 99 bytes, 45 40 39 seconds + 10 minute penalty

import math
def f(n):
 x=y=2;d=1
 while d<2:y=y*y+1;x,y=1+x*x%n,y*y%n+1;d=math.gcd(x-y,n)
 return d

Try it online!

Uses Pollard-Rho with initial value 2 and polynomial x^2+1.

\$\endgroup\$
  • \$\begingroup\$ You could use pow (with the 3rd argument) to improve your execution speed. \$\endgroup\$ – mbomb007 May 30 '18 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy