Ruud H.G. van Tol on Sun, 21 Jan 2024 13:54:25 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

oeis_digits( x, {D=105} )



oeis_digits( x, D=105) = {
  localprec(D);
  if("t_STR" == type(x), x=eval(x));
  my(v=Vec(strprintf("%.*f", D, x)));
  [[ eval(s) |s<-v[1+(x<1)..D+1+(x<1)], s!="."], if(x<1, 0, 1+logint(x, 10))];
}


? oeis_digits("exp(1)") \\ A001113
% [[2, 7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9, 0, 4, 5, 2, 3, 5, 3, 6, 0, 2, 8, 7, 4, 7, 1, 3, 5, 2, 6, 6, 2, 4, 9, 7, 7, 5, 7, 2, 4, 7, 0, 9, 3, 6, 9, 9, 9, 5, 9, 5, 7, 4, 9, 6, 6, 9, 6, 7, 6, 2, 7, 7, 2, 4, 0, 7, 6, 6, 3, 0, 3, 5, 3, 5, 4, 7, 5, 9, 4, 5, 7, 1, 3, 8, 2, 1, 7, 8, 5, 2, 5, 1, 6, 6, 4, 2, 7, 4, 2, 7, 4, 6], 1]

? oeis_digits("Catalan") \\ A006752
% [[9, 1, 5, 9, 6, 5, 5, 9, 4, 1, 7, 7, 2, 1, 9, 0, 1, 5, 0, 5, 4, 6, 0, 3, 5, 1, 4, 9, 3, 2, 3, 8, 4, 1, 1, 0, 7, 7, 4, 1, 4, 9, 3, 7, 4, 2, 8, 1, 6, 7, 2, 1, 3, 4, 2, 6, 6, 4, 9, 8, 1, 1, 9, 6, 2, 1, 7, 6, 3, 0, 1, 9, 7, 7, 6, 2, 5, 4, 7, 6, 9, 4, 7, 9, 3, 5, 6, 5, 1, 2, 9, 2, 6, 1, 1, 5, 1, 0, 6, 2, 4, 8, 5, 7, 4], 0]

? oeis_digits("1/113", 112) \\ A021117
% [[0, 0, 8, 8, 4, 9, 5, 5, 7, 5, 2, 2, 1, 2, 3, 8, 9, 3, 8, 0, 5, 3, 0, 9, 7, 3, 4, 5, 1, 3, 2, 7, 4, 3, 3, 6, 2, 8, 3, 1, 8, 5, 8, 4, 0, 7, 0, 7, 9, 6, 4, 6, 0, 1, 7, 6, 9, 9, 1, 1, 5, 0, 4, 4, 2, 4, 7, 7, 8, 7, 6, 1, 0, 6, 1, 9, 4, 6, 9, 0, 2, 6, 5, 4, 8, 6, 7, 2, 5, 6, 6, 3, 7, 1, 6, 8, 1, 4, 1, 5, 9, 2, 9, 2, 0, 3, 5, 3, 9, 8, 2, 3], 0]

? oeis_digits(1/113, 112) \\ A021117
% [[0, 0, 8, 8, 4, 9, 5, 5, 7, 5, 2, 2, 1, 2, 3, 8, 9, 3, 8, 0, 5, 3, 0, 9, 7, 3, 4, 5, 1, 3, 2, 7, 4, 3, 3, 6, 2, 8, 3, 1, 8, 5, 8, 4, 0, 7, 0, 7, 9, 6, 4, 6, 0, 1, 7, 6, 9, 9, 1, 1, 5, 0, 4, 4, 2, 4, 7, 7, 8, 7, 6, 1, 0, 6, 1, 9, 4, 6, 9, 0, 2, 6, 5, 4, 8, 6, 7, 2, 5, 6, 6, 3, 7, 1, 6, 8, 1, 4, 1, 5, 9, 2, 9, 2, 0, 3, 5, 3, 9, 8, 2, 3], 0]

? oeis_digits(12345.6789, 20)
% [[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 5]



Notice that I did not add a guard against this usage:

? oeis_digits(0.5, 78)
% [[4, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 4, 6, 7, 6, 2, 2, 7, 4, 3, 8, 1, 7, 7, 9, 0, 0, 6, 8, 8, 3, 5, 0, 5, 4, 6, 2, 9, 1, 4, 4, 5, 6, 7, 4, 8, 0, 3, 2], 0]

Also be aware that the used printf-format rounds to the nearest, so without proper care the final digits are easily off.

-- Ruud