Post Reply 
(38G) OEIS A008904: Last Non-Zero Digit of Factorial
09-10-2017, 04:57 PM
Post: #1
(38G) OEIS A008904: Last Non-Zero Digit of Factorial
The programme inserts symbolics in the Sequence App to produce the sequence

https://oeis.org/A008904

NB The position indexes in the Sequence App start at 1, while the OEIS starts the sequence at index zero.

NB This sequence does NOT become periodic.

Code:
RECURSE(U,IFTE(N<6,IFTE(N==3,2,IFTE(N==4,6,4)),(IFTE(U3(N)<2,6,IFTE(U3(N)==2,12,IFTE(U3(N)==3,36,IFTE(U3(N)<7,24,IFTE(U3(N)==7,48,IFTE(U3(N)==8,24,36))))))*3^(U2(N) MOD 4)*U1(U2(N)+1)) MOD 10),1,1)►U1(N):
CHECK 1:
RECURSE(U,INT((N-1)/5),0,0)►U2(N):
RECURSE(U,(N-1) MOD 10,0,1)►U3(N):
Find all posts by this user
Quote this message in a reply
09-11-2017, 03:54 PM (This post was last modified: 09-11-2017 04:16 PM by Gerald H.)
Post: #2
RE: (38G) OEIS A008904: Last Non-Zero Digit of Factorial
A slightly improved version of the programme:

Code:
RECURSE(U,IFTE(N<6,IFTE(N==3,2,IFTE(N==4,6,4)),IFTE(U3(N)<2,6,IFTE(U3(N)==2,12,IFTE(U3(N)==3,36,IFTE(U3(N)==7,48,IFTE(U3(N)==9,36,24)))​))*3^(U2(N) MOD 4)*U1(U2(N)+1)) MOD 10,1,1)►U1(N):
CHECK 1:
RECURSE(U,INT((N-1)/5),0,0)►U2(N):
RECURSE(U,(N-1) MOD 10,0,1)►U3(N):
Find all posts by this user
Quote this message in a reply
09-26-2017, 11:54 AM (This post was last modified: 09-26-2017 11:59 AM by Gerald H.)
Post: #3
RE: (38G) OEIS A008904: Last Non-Zero Digit of Factorial
While the above symbolics work correctly, the recursion takes too long for large input.

Here a programme that takes integer input from the last entry to the stack & returns the result in Ans in the Home screen:

Code:
Ans►N:
IF Ans<2
THEN
1
ELSE
0►C:
0►P:
N:
WHILE Ans
REPEAT
RUN IQUOT5:
R*P+IFTE(R MOD 2,0,R/2)+C►C:
P+1►P:
Q:
END:
2^(C MOD 4):
IF Ans==1
THEN
6:
END:
END:

The programme IQUOT5 takes an integer from the previous calculation & stores the Remainder on division by 5 in R & quotient in Q, returning Q in Ans.

Code:
Ans►Q:
Ans MOD 5►R:
(Q-Ans)/5►Q:
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)