login
a(n) is the final nonzero digit of n!.
32

%I #165 Dec 07 2023 16:28:49

%S 1,1,2,6,4,2,2,4,2,8,8,8,6,8,2,8,8,6,8,2,4,4,8,4,6,4,4,8,4,6,8,8,6,8,

%T 2,2,2,4,2,8,2,2,4,2,8,6,6,2,6,4,2,2,4,2,8,4,4,8,4,6,6,6,2,6,4,6,6,2,

%U 6,4,8,8,6,8,2,4,4,8,4,6,8,8,6,8,2,2,2,4,2,8,2,2,4,2,8,6,6,2,6

%N a(n) is the final nonzero digit of n!.

%C 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

%C 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

%C From _Robert G. Wilson v_, Feb 16 2011: (Start)

%C (mod 10) == 2 4 6 8

%C 10^

%C 1 4 2 1 1

%C 2 28 23 22 25

%C 3 248 247 260 243

%C 4 2509 2486 2494 2509

%C 5 25026 24999 24972 25001

%C 6 249993 250012 250040 249953

%C 7 2500003 2499972 2499945 2500078

%C 8 25000078 24999872 25000045 25000003

%C 9 249999807 250000018 250000466 249999707 (End)

%D J.-P. Allouche and J. Shallit, Automatic Sequences, Cambridge Univ. Press, 2003, p. 202.

%D 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

%D S. Kakutani, Ergodic theory of shift transformations, in Proc. 5th Berkeley Symp. Math. Stat. Prob., Univ. Calif. Press, vol. II, 1967, 405-414.

%D Popular Computing (Calabasas, CA), Problem 120, Factorials, Vol. 4 (No. 36, Mar 1976), page PC36-3.

%H Robert G. Wilson v, <a href="/A008904/b008904.txt">Table of n, a(n) for n = 0..10000</a>

%H W. Bomfim, <a href="http://oeis.org/w/images/4/48/AlgLastFinal1.txt">An algorithm to find the last nonzero digit of n!</a>

%H W. Bomfim, <a href="http://oeis.org/w/images/0/0e/Property2.txt">A property of the last non-zero digit of factorials</a>

%H K. S. Brown, <a href="http://www.mathematik.uni-bielefeld.de/~sillke/SEQUENCES/series020">The least significant nonzero digit of n!</a>

%H F. M. Dekking, <a href="http://gdz.sub.uni-goettingen.de/dms/load/img/?PPN=GDZPPN002544067&amp;IDDOC=72426">Regularity and irregularity of sequences generated by automata</a> Sém. Théor. Nombres, Bordeaux, Exposé 9, 1979-1980, pages 9-01 to 9-10.

%H Jean-Marc Deshouillers, <a href="http://www.boku.ac.at/math/udt/vol07/no1/03Desh30-11.pdf">A footnote to the least non zero digit of n! in base 12</a>, Uniform Distribution Theory 7:1 (2012), pp. 71-73.

%H Jean-Marc Deshouillers, <a href="http://dx.doi.org/10.1515%2FUDT-2016-0018">Yet Another Footnote to the Least Non Zero Digit of n! in Base 12</a>. Unif. Distrib. Theory 11 (2016), no. 2, 163-167.

%H Gregory P. Dresden, <a href="http://dresden.academic.wlu.edu/files/2017/08/lnzd2.pdf">Three transcendental numbers from the last non-zero digits of n^n, F_n and n!</a>, Mathematics Magazine, pp. 96-105, vol. 81, 2008.

%H Gregory P. Dresden, <a href="http://www.jstor.org/stable/2691105">Two Irrational Numbers from the Last Nonzero Digits of n! and n^n</a>, Mathematics Magazine, Vol. 74, No. 4 (2001), 316-320.

%H Gregory P. Dresden, <a href="http://dresden.academic.wlu.edu/research/">Research Papers</a>.

%H Fritz Jacob (fritzjacob(AT)gmail.com), <a href="/A008904/a008904.txt">A way to compute a(n)</a>

%H MathPages, <a href="http://www.mathpages.com/home/kmath489.htm">Least Significant Non-Zero Digit of n!</a>

%H J. C. Martin, <a href="http://dx.doi.org/10.1090/S0002-9947-1977-0463400-1">The structure of generalized Morse minimal sets on m symbols</a>, Trans. Amer. Math. Soc. 232 (1977), 343-355.

%H T. Sillke, <a href="http://www.mathematik.uni-bielefeld.de/~sillke/SEQUENCES/">What are the next entries for the following sequences?</a> Puzzle U asks for the next number after 2642242888682886824484644846.

%H Eric Weisstein's World of Mathematics, <a href="http://mathworld.wolfram.com/Factorial.html">Factorial</a>

%H David W. Wilson, <a href="/A008904/a008904a.txt">Minimal state machine for this sequence</a>

%H David W. Wilson, <a href="/A008904/a008904b.txt">Another method for computing this sequence</a>

%H <a href="/index/Fi#final">Index entries for sequences related to final digits of numbers</a>

%H <a href="/index/Fa#factorial">Index entries for sequences related to factorial numbers</a>

%F 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

%F 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

%F From _Washington Bomfim_, Jan 09 2011: (Start)

%F a(0) = 1, a(1) = 1, if n >= 2, with

%F n represented in base 5 as (a_h, ..., a_1, a_0)_5,

%F t = Sum_{i = h, h-1, ... , 0} (a_i even),

%F x = Sum_{i=h, h-1, ... , 1} (Sum_{k=h, h-1, ..., i}(a_i)),

%F z = (x + t/2) mod 4, and y = 2^z,

%F a(n) = 6*(y mod 2) + y*(1-(y mod 2)).

%F For n >= 5, and n mod 5 = 0,

%F i) a(n) = a(n+1) = a(n+3),

%F ii) a(n+2) = 2*a(n) mod 10, and

%F iii) a(n+4) = 4*a(n) mod 10.

%F For k not equal to 1, a(10^k) = a(2^k). See second Dresden link, and second Bomfim link.

%F (End)

%e 6! = 720, so a(6) = 2.

%t f[n_]:=Module[{m=n!},While[Mod[m,10]==0,m=m/10];Mod[m,10]]

%t Table[f[i],{i,0,100}]

%t 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 *)

%t 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 *)

%t 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 *)

%o (Python)

%o def a(n):

%o if n <= 1: return 1

%o return 6*[1,1,2,6,4,4,4,8,4,6][n%10]*3**(n/5%4)*a(n/5)%10

%o # _Maciej Ireneusz Wilczynski_, Aug 23 2010

%o (Python)

%o from functools import reduce

%o from sympy.ntheory.factor_ import digits

%o 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

%o (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

%o (Sage)

%o def A008904(n):

%o # algorithm from David Wilson, http://oeis.org/A008904/a008904b.txt

%o if n == 0 or n == 1: return 1

%o dd = n.digits(base=5)

%o x = sum(i*d for i,d in enumerate(dd))

%o y = sum(d for d in dd if d % 2 == 0)/2

%o z = 2**((x+y) % 4)

%o if z == 1: z = 6

%o return z # _D. S. McNeil_, Dec 09 2010

%o (Haskell)

%o a008904 n = a008904_list !! n

%o a008904_list = 1 : 1 : f 2 1 where

%o f n x = x' `mod` 10 : f (n+1) x' where

%o x' = g (n * x) where

%o g m | m `mod` 5 > 0 = m

%o | otherwise = g (m `div` 10)

%o -- _Reinhard Zumkeller_, Apr 08 2011

%Y Cf. A008905, A000142, A004154, A034886.

%Y Indices of 2,4,6,8: A045547, A045548, A045549, A045550.

%Y Other bases: A136690, A136691, A136692, A136693, A136694, A136695, A136696, A136697, A136698, A136699, A136700, A136701, A136702.

%K nonn,base,nice

%O 0,3

%A _Russ Cox_

%E More terms from _Greg Dresden_, Feb 21 2006