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.

Showing 1-6 of 6 results.

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

A045547 Numbers whose factorial has '2' as its final nonzero digit.

Original entry on oeis.org

2, 5, 6, 8, 14, 19, 34, 35, 36, 38, 40, 41, 43, 47, 50, 51, 53, 62, 67, 74, 84, 85, 86, 88, 90, 91, 93, 97, 109, 110, 111, 113, 115, 116, 118, 122, 129, 132, 145, 146, 148, 150, 151, 153, 162, 167, 174, 177, 180, 181, 183, 189, 194, 200, 201, 203, 212, 217
Offset: 1

Views

Author

Keywords

Comments

From Robert Israel, Dec 16 2016: (Start)
If k is in the sequence, then:
if k == 0 (mod 5), k+1 is in the sequence;
if k == 1 (mod 5), k+1 is in A045548;
if k == 2 (mod 5), k+1 is in A045549;
if k == 3 (mod 5), k+1 is in A045550. (End)

Crossrefs

Programs

  • Maple
    count:= 0:
    r:= 1:
    for n from 2 while count < 100 do
      r:= r*n;
      if r mod 10 = 0 then r:= r/10^padic:-ordp(r, 5) fi;
      if r mod 10 = 2 then count:= count+1; A[count]:= n fi;
    od: seq(A[i], i=1..100); # Robert Israel, Dec 16 2016
  • Mathematica
    f[ n_Integer, m_Integer ] := (c = 0; p = 1; While[ d = Floor[ n/5^p ]; d > 0, c = c + d; p++ ]; Mod[ n!/10^c, m ] ); Select[ Range[ 250 ], f[ #, 10 ] == 2 & ]
    Join[{2},Select[Range[5,220],Most[Split[IntegerDigits[#!]]][[-1,1]] == 2&]] (* Harvey P. Dale, May 04 2016 *)
    f[n_] := Mod[6 Times @@ (Rest[ FoldList[{1 + #1[[1]], #2! 2^(#1[[1]] #2)} &, {0, 0}, Reverse[ IntegerDigits[n, 5]]]]), 10][[2]] (* after Jacob A. Siehler & Greg Dresden in A008904 *); f[0] = f[1] = 1; Select[ Range[150], f[#] == 2 &] (* Robert G. Wilson v, Dec 28 2016 *)
  • PARI
    lnz(n)=if(n<2, return(1)); my(m=Mod(1,5)); for(k=2,n, m*=k/10^valuation(k,5)); lift(chinese(Mod(0,2),m))
    is(n)=lnz(n)==2 \\ Charles R Greathouse IV, Dec 16 2016
    
  • PARI
    list(lim)=my(v=List(),m=Mod(1,5)); for(k=2,lim, m*=k/10^valuation(k,5); if(m==2, listput(v, k))); Vec(v) \\ Charles R Greathouse IV, Dec 16 2016
    
  • Python
    from functools import reduce
    from itertools import count, islice
    from sympy.ntheory.factor_ import digits
    def A045547_gen(startvalue=1): # generator of terms
        return filter(lambda n:2==reduce(lambda x,y:x*y%10,((1,1,2,6,4)[a]*((6,2,4,8)[i*a&3] if i*a else 1) for i, a in enumerate(digits(n,5)[-1:0:-1])))*6%10, count(max(startvalue,1)))
    A045547_list = list(islice(A045547_gen(),30)) # Chai Wah Wu, Dec 07 2023

A045548 Numbers whose factorial has '4' as its final nonzero digit.

Original entry on oeis.org

4, 7, 20, 21, 23, 25, 26, 28, 37, 42, 49, 52, 55, 56, 58, 64, 69, 75, 76, 78, 87, 92, 99, 100, 101, 103, 112, 117, 124, 134, 135, 136, 138, 140, 141, 143, 147, 152, 155, 156, 158, 164, 169, 179, 182, 195, 196, 198, 202, 205, 206, 208, 214, 219
Offset: 1

Views

Author

Keywords

Comments

From Robert Israel, Dec 16 2016: (Start)
If n is in the sequence, then:
if n == 0 (mod 5), n+1 is in the sequence;
if n == 1 (mod 5), n+1 is in A045550;
if n == 2 (mod 5), n+1 is in A045547;
if n == 3 (mod 5), n+1 is in A045549. (End)

Crossrefs

Programs

  • Maple
    count:= 0:
    r:= 1:
    for n from 2 while count < 100 do
      r:= r*n;
      if r mod 10 = 0 then r:= r/10^padic:-ordp(r, 5) fi;
      if r mod 10 = 4 then count:= count+1; A[count]:= n fi;
    od: seq(A[i], i=1..100); # Robert Israel, Dec 16 2016
  • Mathematica
    f[n_] := Mod[6 Times @@ (Rest[ FoldList[{1 + #1[[1]], #2! 2^(#1[[1]] #2)} &, {0, 0}, Reverse[ IntegerDigits[n, 5]]]]), 10][[2]] (* after Jacob A. Siehler & Greg Dresden in A008904 *); f[0] = f[1] = 1; Select[ Range[150], f[#] == 4 &] (* Robert G. Wilson v, Dec 28 2016 *)
  • Python
    from itertools import count, islice
    from functools import reduce
    from sympy.ntheory.factor_ import digits
    def A045548_gen(startvalue=1): # generator of terms >= startvalue
        return filter(lambda n:4==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), count(max(startvalue,1)))
    A045548_list = list(islice(A045548_gen(),30)) # Chai Wah Wu, Dec 07 2023

A045549 Numbers whose factorial has '6' as its final nonzero digit.

Original entry on oeis.org

3, 12, 17, 24, 29, 32, 45, 46, 48, 59, 60, 61, 63, 65, 66, 68, 72, 79, 82, 95, 96, 98, 104, 107, 120, 121, 123, 127, 130, 131, 133, 139, 144, 159, 160, 161, 163, 165, 166, 168, 172, 175, 176, 178, 187, 192, 199, 209, 210, 211, 213, 215, 216, 218
Offset: 1

Views

Author

Keywords

Comments

From Robert Israel, Dec 16 2016: (Start)
If n is in the sequence, then:
if n == 0 (mod 5), n+1 is in the sequence;
if n == 1 (mod 5), n+1 is in A045547;
if n == 2 (mod 5), n+1 is in A045550;
if n == 3 (mod 5), n+1 is in A045548. (End)

Crossrefs

Programs

  • Maple
    count:= 0:
    r:= 1:
    for n from 2 while count < 100 do
      r:= r*n;
      if r mod 10 = 0 then r:= r/10^padic:-ordp(r,5) fi;
      if r mod 10 = 6 then count:= count+1; A[count]:= n fi;
    od:
    seq(A[i],i=1..100); # Robert Israel, Dec 16 2016
  • Mathematica
    Join[{3}, Select[Range[5,250], Most[Split[IntegerDigits[#!]]][[-1, 1]] == 6 &]] (* Vincenzo Librandi, Dec 16 2016 *)
    f[n_] := Mod[6 Times @@ (Rest[ FoldList[{1 + #1[[1]], #2! 2^(#1[[1]] #2)} &, {0, 0}, Reverse[ IntegerDigits[n, 5]]]]), 10][[2]] (* after Jacob A. Siehler & Greg Dresden in A008904 *); f[0] = f[1] = 1; Select[ Range[150], f[#] == 6 &] (* Robert G. Wilson v, Dec 28 2016 *)
    Select[Range[250],With[{f=#!},Drop[IntegerDigits[f],-IntegerExponent[f]][[-1]]]==6&] (* Harvey P. Dale, Sep 27 2024 *)
  • Python
    from itertools import count, islice
    from functools import reduce
    from sympy.ntheory.factor_ import digits
    def A045549_gen(startvalue=2): # generator of terms >= startvalue
        return filter(lambda n:6==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), count(max(startvalue,2)))
    A045549_list = list(islice(A045549_gen(),30)) # Chai Wah Wu, Dec 07 2023

A058376 Where the race of the count of final nonzero digit of k! changes, starting at k=2.

Original entry on oeis.org

2, 16, 50, 80, 88, 108, 110, 264, 273, 291, 326, 336, 669, 671, 678, 685, 718, 721, 738, 764, 773, 791, 826, 836, 1433, 1435, 1558, 1560, 1616, 1629, 1636, 1694, 1696, 1764, 1773, 1791, 1826, 1836, 1928, 1935, 1968, 1971, 1988, 2014, 2023, 2041, 2076, 2086
Offset: 1

Views

Author

Robert G. Wilson v, Dec 19 2000

Keywords

Examples

			a(1) = 2 to start the race. At 15! the number of final twos is 5 and so is the number of eights. But at 16, eights now lead twos, so a(2) = 16 to reflect this fact.
When k=10000 is reached, the count stands at 2509 twos, 2486 fours, 2494 sixes, and 2510 eights.
		

Crossrefs

Programs

  • Mathematica
    f[ n_Integer, m_Integer ] := (c = 0; p = 1; While[ d = Floor[ n/5^p ]; d > 0, c = c + d; p++ ]; Mod[ n!/10^c, m ]); a = Table[ 0, {4} ]; r = 4; Do[ b = f[ n, 10 ]; Switch[ b, 2, a[ [ 1 ] ]++, 4, a[ [ 2 ] ]++, 6, a[ [ 3 ] ]++, 8, a[ [ 4 ] ]++ ]; If[ a[ [ b/2 ] ] > a[ [ r/2 ] ], r = b; Print[ n ] ], {n, 2, 10^4} ]
  • Python
    from functools import reduce
    from itertools import count, islice
    from sympy.ntheory.factor_ import digits
    def A058376_gen(): # generator of terms
        a, k, i = [0]*4, 0, 1
        for n in count(2):
            m = (reduce(lambda x,y:x*y%10,((1,1,2,6,4)[a]*((6,2,4,8)[i*a&3] if i*a else 1) for i, a in enumerate(digits(n,5)[-1:0:-1])))*6%10>>1)-1
            a[m] += 1
            if a[m] > k:
                if m!=i:
                    yield n
                i, k = m, a[m]
    A058376_list = list(islice(A058376_gen(),48)) # Chai Wah Wu, Dec 07 2023

Extensions

Offset 1 from Michel Marcus, Jul 25 2021

A374015 Residue modulo 5 of n! divided by the highest power of 10 which divides n!.

Original entry on oeis.org

1, 1, 2, 1, 4, 2, 2, 4, 2, 3, 3, 3, 1, 3, 2, 3, 3, 1, 3, 2, 4, 4, 3, 4, 1, 4, 4, 3, 4, 1, 3, 3, 1, 3, 2, 2, 2, 4, 2, 3, 2, 2, 4, 2, 3, 1, 1, 2, 1, 4, 2, 2, 4, 2, 3, 4, 4, 3, 4, 1, 1, 1, 2, 1, 4, 1, 1, 2, 1, 4, 3, 3, 1, 3, 2, 4, 4, 3, 4, 1, 3, 3, 1, 3, 2, 2, 2, 4, 2, 3, 2, 2, 4, 2, 3, 1, 1, 2, 1, 4, 4, 4, 3, 4, 1, 3
Offset: 0

Views

Author

Cezary Glowacz, Jun 25 2024

Keywords

Comments

The sequence is not eventually periodic. This because by induction on k the eventual period must be a multiple of 5^k for every k.
a(5^k) = 2^k mod 5.
From Cezary Glowacz, Feb 07 2025: (Start)
The proportions p(m,s) of counts of pairs of consecutive terms s among a(1),...,a(m) converge to equidistribution (and as an immediate consequence, so do proportions of individual terms).
This can be seen, for example, by stating p(5^(4(n+1)+1)-1,s) as affine functions of p(5^(4n+1)-1,t) and examining the convergence of p(5^(4n+1)-1,u) to the equidistribution. Then, p(m,s) converges to the equidistribution because the maximum over s of the absolute values of deviations from 1/16 of p(m,s) for m>k*5^(4n+1)-1 is less than the corresponding maximum over t for p(5^(4n+1)-1,t) plus 2/(5^(4n+1)) + 1/k.
Consecutive terms 1,2,3 do not occur, so that triples do not have a similar equidistribution.
(End)
If n > 0 is not divisible by 5, a(n) == n * a(n-1) (mod 5). - Robert Israel, Jul 05 2024

Examples

			a(5) = 1*2*3*4*5/10 mod 5 = 2.
		

Crossrefs

Programs

  • Maple
    a:= n-> (f-> irem(f/10^padic[ordp](f, 10), 5))(n!):
    seq(a(n), n=0..105);  # Alois P. Heinz, Jun 25 2024
  • Mathematica
    a[n_]:=Mod[n!/10^IntegerExponent[n!, 10],5]; Array[a,106,0] (* Stefano Spezia, Jun 25 2024 *)
  • PARI
    a(n)=if(n>4, my(k=n\5); return(lift((n%5)!*a(k)*Mod(2,5)^k))); n!%5 \\ Charles R Greathouse IV, Jan 24 2025
  • Python
    v=[[((1,1,2,1,4)[j]*2**(i*j))%5 for j in range(5)] for i in range(4)]
    def a(n):
        c,p=0,1
        while n: c,n,p=(c+1)%4,n//5,(v[c][n%5]*p)%5
        return(p) # Cezary Glowacz, Feb 05 2025
    

Formula

a(n) = A010874(A004154(n)).
Showing 1-6 of 6 results.