cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

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

Original entry on oeis.org

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, 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, 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
Offset: 0

Views

Author

Keywords

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)

Examples

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

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.

Crossrefs

Programs

  • 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
  • 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 *)
  • 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
    
  • 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
    
  • 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
    

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)

Extensions

More terms from Greg Dresden, Feb 21 2006