OFFSET
0,3
COMMENTS
This sequence is not ultimately periodic. This can be deduced from the fact that the sequence can be obtained as a fixed point of a morphism. - Jean-Paul Allouche, Jul 25 2001
The decimal number 0.1126422428... formed from these digits is a transcendental number; see the article by G. Dresden. The Mathematica code uses Dresden's formula for the last nonzero digit of n!; this is more efficient than simply calculating n! and then taking its least-significant digit. - Greg Dresden, Feb 21 2006
From Robert G. Wilson v, Feb 16 2011: (Start)
(mod 10) == 2 4 6 8
10^
1 4 2 1 1
2 28 23 22 25
3 248 247 260 243
4 2509 2486 2494 2509
5 25026 24999 24972 25001
6 249993 250012 250040 249953
7 2500003 2499972 2499945 2500078
8 25000078 24999872 25000045 25000003
9 249999807 250000018 250000466 249999707 (End)
REFERENCES
J.-P. Allouche and J. Shallit, Automatic Sequences, Cambridge Univ. Press, 2003, p. 202.
Gardner, M. "Factorial Oddities." Ch. 4 in Mathematical Magic Show: More Puzzles, Games, Diversions, Illusions and Other Mathematical Sleight-of-Mind from Scientific American. New York: Vintage, pp. 50-65, 1978
S. Kakutani, Ergodic theory of shift transformations, in Proc. 5th Berkeley Symp. Math. Stat. Prob., Univ. Calif. Press, vol. II, 1967, 405-414.
Popular Computing (Calabasas, CA), Problem 120, Factorials, Vol. 4 (No. 36, Mar 1976), page PC36-3.
LINKS
Robert G. Wilson v, Table of n, a(n) for n = 0..10000
K. S. Brown, The least significant nonzero digit of n!
F. M. Dekking, Regularity and irregularity of sequences generated by automata Sém. Théor. Nombres, Bordeaux, Exposé 9, 1979-1980, pages 9-01 to 9-10.
Jean-Marc Deshouillers, A footnote to the least non zero digit of n! in base 12, Uniform Distribution Theory 7:1 (2012), pp. 71-73.
Jean-Marc Deshouillers, Yet Another Footnote to the Least Non Zero Digit of n! in Base 12. Unif. Distrib. Theory 11 (2016), no. 2, 163-167.
Gregory P. Dresden, Three transcendental numbers from the last non-zero digits of n^n, F_n and n!, Mathematics Magazine, pp. 96-105, vol. 81, 2008.
Gregory P. Dresden, Two Irrational Numbers from the Last Nonzero Digits of n! and n^n, Mathematics Magazine, Vol. 74, No. 4 (2001), 316-320.
Gregory P. Dresden, Research Papers.
Fritz Jacob (fritzjacob(AT)gmail.com), A way to compute a(n)
MathPages, Least Significant Non-Zero Digit of n!
J. C. Martin, The structure of generalized Morse minimal sets on m symbols, Trans. Amer. Math. Soc. 232 (1977), 343-355.
T. Sillke, What are the next entries for the following sequences? Puzzle U asks for the next number after 2642242888682886824484644846.
Eric Weisstein's World of Mathematics, Factorial
David W. Wilson, Minimal state machine for this sequence
David W. Wilson, Another method for computing this sequence
FORMULA
The generating function for n>1 is as follows: for n = a_0 + 5*a_1 + 5^2*a_2 + ... + 5^N*a_N (the expansion of n in base-5), then the last nonzero digit of n!, for n>1, is 6*Product_{i=0..N} (a_i)! (2^(i a_i)) mod 10. - Greg Dresden, Feb 21 2006
a(n) = f(n,1,0) with f(n,x,e) = if n < 2 then A010879(x*A000079(e)) else f(n-1,A010879(x*A132740(n),e+A007814(n)-A112765(n)). - Reinhard Zumkeller, Aug 16 2008
From Washington Bomfim, Jan 09 2011: (Start)
a(0) = 1, a(1) = 1, if n >= 2, with
n represented in base 5 as (a_h, ..., a_1, a_0)_5,
t = Sum_{i = h, h-1, ... , 0} (a_i even),
x = Sum_{i=h, h-1, ... , 1} (Sum_{k=h, h-1, ..., i}(a_i)),
z = (x + t/2) mod 4, and y = 2^z,
a(n) = 6*(y mod 2) + y*(1-(y mod 2)).
For n >= 5, and n mod 5 = 0,
i) a(n) = a(n+1) = a(n+3),
ii) a(n+2) = 2*a(n) mod 10, and
iii) a(n+4) = 4*a(n) mod 10.
For k not equal to 1, a(10^k) = a(2^k). See second Dresden link, and second Bomfim link.
(End)
EXAMPLE
6! = 720, so a(6) = 2.
MATHEMATICA
f[n_]:=Module[{m=n!}, While[Mod[m, 10]==0, m=m/10]; Mod[m, 10]]
Table[f[i], {i, 0, 100}]
f[n_] := Mod[6Times @@ (Rest[FoldList[{ 1 + #1[[1]], #2!2^(#1[[1]]#2)} &, {0, 0}, Reverse[IntegerDigits[n, 5]]]]), 10][[2]]; Join[{1, 1}, Table[f[n], {n, 2, 100}]] (* program contributed by Jacob A. Siehler, Greg Dresden, Feb 21 2006 *)
zOF[n_Integer?Positive] := Module[{maxpow=0}, While[5^maxpow<=n, maxpow++]; Plus@@Table[Quotient[n, 5^i], {i, maxpow-1}]]; Flatten[Table[ Take[ IntegerDigits[ n!], {-zOF[n]-1}], {n, 100}]] (* Harvey P. Dale, Dec 16 2010 *)
f[n_]:=Block[{id=IntegerDigits[n!, 10]}, While[id[[-1]]==0, id=Most@id]; id[[-1]]]; Table[f@n, {n, 0, 100}] (* Vincenzo Librandi, Sep 07 2017 *)
PROG
(Python)
def a(n):
if n <= 1: return 1
return 6*[1, 1, 2, 6, 4, 4, 4, 8, 4, 6][n%10]*3**(n/5%4)*a(n/5)%10
# Maciej Ireneusz Wilczynski, Aug 23 2010
(Python)
from functools import reduce
from sympy.ntheory.factor_ import digits
def A008904(n): return reduce(lambda x, y:x*y%10, (((6, 2, 4, 8, 6, 2, 4, 8, 2, 4, 8, 6, 6, 2, 4, 8, 4, 8, 6, 2)[(a<<2)|(i*a&3)] if i*a else (1, 1, 2, 6, 4)[a]) for i, a in enumerate(digits(n, 5)[-1:0:-1])), 6) if n>1 else 1 # Chai Wah Wu, Dec 07 2023
(PARI) a(n) = r=1; while(n>0, r *= Mod(4, 10)^((n\10)%2) * [1, 2, 6, 4, 2, 2, 4, 2, 8][max(n%10, 1)]; n\=5); lift(r) \\ Charles R Greathouse IV, Nov 05 2010; cleaned up by Max Alekseyev, Jan 28 2012
(Sage)
def A008904(n):
# algorithm from David Wilson, http://oeis.org/A008904/a008904b.txt
if n == 0 or n == 1: return 1
dd = n.digits(base=5)
x = sum(i*d for i, d in enumerate(dd))
y = sum(d for d in dd if d % 2 == 0)/2
z = 2**((x+y) % 4)
if z == 1: z = 6
return z # D. S. McNeil, Dec 09 2010
(Haskell)
a008904 n = a008904_list !! n
a008904_list = 1 : 1 : f 2 1 where
f n x = x' `mod` 10 : f (n+1) x' where
x' = g (n * x) where
g m | m `mod` 5 > 0 = m
| otherwise = g (m `div` 10)
-- Reinhard Zumkeller, Apr 08 2011
CROSSREFS
KEYWORD
nonn,base,nice
AUTHOR
EXTENSIONS
More terms from Greg Dresden, Feb 21 2006
STATUS
approved