Kaelygon

p-adic factors of base 10 prime

Aug 21st, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. typedef unsigned long long uint64_t;
  5.  
  6.  
  7. uint64_t find_b(uint64_t a, uint64_t target, uint64_t digits) {
  8.    
  9.     uint64_t guessDigit = 0;
  10.     uint64_t guessLen = 0;
  11.     uint64_t result = 0;
  12.  
  13.  
  14.     for(int j=0;j<digits;j++){
  15.         uint64_t p10=pow(10,1+guessLen); //base 10 shift by 10^n divison.
  16.         uint64_t p10d=p10/10;
  17.          
  18.         for(uint64_t b=0;b<10;b++){ //test every base 10 digit
  19.             uint64_t bg = b*p10d+result; //concatenate b-guess to lead of result, b=2 result=39 -> 2*100+39 -> 239
  20.             uint64_t product = a * bg;
  21.             if( (product%p10) == (target%p10) ){ //same trailing digits
  22.                 guessDigit=b;
  23.                 break;
  24.             }
  25.         }
  26.         result+=guessDigit*(p10/10); //concat
  27.         guessLen++;
  28.     }      
  29.  
  30.     return result;
  31. }
  32.  
  33. int main() {
  34.     for(uint a=1;a<32;a++){ //a is chosen
  35.         uint64_t target = 13238717; // Target 10-adic number
  36.         uint64_t leadingDigits = 6; //leading digits after minimum required precision
  37.         uint64_t digits = log10(target)+1+leadingDigits; //target digit count
  38.         uint64_t mod = pow(10, digits ); // ignore imprecise digits e.g. 3 * 33337746239 = 100013238717 -> ...00013238717
  39.  
  40.         uint64_t b = find_b(a, target, digits);
  41.  
  42.         std::string leadingZeros="";
  43.         for(uint64_t i=0; i<leadingDigits;i++){
  44.             leadingZeros+="0";
  45.         }
  46.  
  47.         uint64_t approx = (a * b) % mod;
  48.         if ( approx == target ) { //exclude impossible ones
  49.             std::cout << a << " * ..." << b << " = ..." << leadingZeros << approx << std::endl;
  50.         }
  51.     }
  52.  
  53.     return 0;
  54. }
  55.  
Tags: number Theory
Add Comment
Please, Sign In to add comment