Advertisement
Paul_Pedant

Ramanujan Description

May 20th, 2020
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. Some notes on my Ramujan code.
  2.  
  3. It is a bash shell, almost all of it containing an awk script. But it just does arithmetic and loops, so it should translate directly into C in an obvious way. awk does not use data types -- it makes them up as it goes along. If you want numbers up to a billion, 32-bit ints would do throughout. Above that, 64-bit ints. I don't know your architecture, so that might be long int or long long int. C99 compilers (like gcc) have explicit int32_t and int64_t.
  4.  
  5. Notation: we are looking for values that satisfy n = (a * a * a) + (b * b * b);
  6. We output these like n a,b, as in 35 2,3 (35 is 8 + 27).
  7. a is the smaller number: we regard 35 3,2 as a duplicate.
  8. There may be more than one solution for n: 13832 2,24; 18,20
  9. For these, a will be increasing, and due to the + total, b will decrease.
  10. We refer to additional solutions for the same n like n c,d.
  11.  
  12. For (e.g.) n = 1,000,000, b cannot be above 100, because even for a = 1, 1000000 < (1 * 1 * 1) + (100 * 100 * 100).
  13.  
  14. Likewise, a cannot be above 79, because 1000000 < (80 * 80 * 80) + (80 * 80 * 80).
  15.  
  16. These limits really cut down on the iterations in (Loop 2) and (Loop 3).
  17.  
  18. The code runs three main nested loops:
  19.  
  20. (Loop 1) For n, which is the sequence of numbers from 1 up to your desired maximum.
  21.  
  22. (Loop 2) For a, which the sequence from 1 up to the limit. If there are no solutions for that n, we may also meet b as it decreases. (Less than 1% of values have even one solution.)
  23.  
  24. (Loop 3) For b, which we run downwards until we get an exact solution, or a too-small value.
  25.  
  26. In those rare cases where we get the first solution, we run secondary searches inside the a,b values using c,d. So these are the 2-way (Ramanujan) numbers.
  27.  
  28. We store the formatted n a,b result in tx. We append any secondary n c,d results in ty. We check ty is non-blank, because we only want multiple solutions.
  29.  
  30. The variable names are cryptic but follow a pattern:
  31.  
  32. n The number we are trying to analyse.
  33. nb The number that n begins at. It happens the algorithm can run between any values, it does not need to start at 1.
  34. ne The number that n ends at.
  35.  
  36. a, ab, ae Current value of a, and its begin and end value in the current loop.
  37. af The initial highest possible value for a, calculated from n.
  38.  
  39. b, be, bf Similar to those for a. We don't need bb because a is the low limit for b.
  40.  
  41. c, d, de Secondary search values, equivalent to a and b. The limits for these are set from the previous a, ae and be values.
  42.  
  43. The function bCbrt() finds the cube root of ne and bf as needed. Specifically, it counts up to find the largest integer whose cube is less than ne (etc). So it iterates 100 times to find cube_root(1,000,000). I can live with that: there is no math function for that in awk, and using exponent and log returns a float, and is awkward to round.
  44.  
  45. The script runs like: ./Ramanujan 10000 to start at 1, or ./Ramanujan 1700 1800 to process a range.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement