Created
June 30, 2021 00:58
-
-
Save Hermann-SW/0f5e58ddc1591a885d1d238a6dfbdde0 to your computer and use it in GitHub Desktop.
Determines for "x->(x^2+1)%n" number of steps needed by Pollard-Rho factorization algorithm, with s on cycle of start value 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc -Wall -Wextra -pedantic search_cycle.c -o search_cycle | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
typedef unsigned long unsig; | |
unsig gcd(unsig a, unsig b) | |
{ | |
if (a<b) | |
{ | |
if (a==0) return b; else b%=a; | |
} | |
while (b!=0) | |
{ | |
a%=b; | |
if (a==0) return b; | |
b%=a; | |
} | |
return a; | |
} | |
unsig dif(unsig a, unsig b) | |
{ | |
return (a<b) ? b-a : a-b; | |
} | |
unsig f(unsig x, unsig a, unsig n) | |
{ | |
return (x*x + a) %n; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
unsig n=atoi(argv[1]); | |
unsig s=atoi(argv[2]); | |
unsig x,y,c; | |
assert(argc == 3); | |
x=s; | |
do | |
{ | |
for(y=f(x,1,n), c=1; gcd(dif(x, y), n)==1; y=f(y,1,n), ++c) {} | |
printf("%lu: %lu [%lu]\n",x,c, gcd(dif(x,y), n)); | |
x = f(x,1,n); | |
} | |
while (x!=s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works for n<2^32 on machines with "sizeof(unsigned long)==8" (because of square step in "f()").
Allows to be used for n=71^5 (for https://oeis.org/A248218):
Find values on start value 0 cycle using analyze.c:
https://gist.github.com/Hermann-SW/83deac0d8977f33b56b548641c26c590
Take any of the values on cycle identified, here 6:
Period of cycle:
For all values on cycle, Pollard-Rho factorization algorithm stops after 11 steps, with divisor 71 found: