/* Last off digit of power of 2 (code golf) in Picat. https://codegolf.stackexchange.com/questions/261908/last-odd-digit-of-power-of-2 """ Last odd digit of power of 2 Task Given n, output position of the last odd digit in the decimal representation of 2n (counting from the end). Rules There are no odd digits for n=1,2,3,6,11 (2,4,8,64,2048) - you may output anything that is not a positive integer for them (no need to be consistent). You choose whether to handle n=0. Standard sequence I/O rules. This is code-golf. Test-cases n answer (2^n) 1 NA 2 2 NA 4 3 NA 8 4 2 16 5 2 32 6 NA 64 7 3 128 8 2 256 9 2 512 10 4 1024 11 NA 2048 12 2 4096 13 2 8192 14 3 16384 15 3 32768 16 2 65536 17 2 131072 18 3 262144 19 6 524288 20 2 1048576 Inspired by this Mathematics SE post [https://math.stackexchange.com/a/4714518/347293] and comments on OEIS A068994 [https://oeis.org/A068994] """ 0 = 1 = 1 1 = 2 = NA 2 = 4 = NA 3 = 8 = NA 4 = 16 = 2 5 = 32 = 2 6 = 64 = NA 7 = 128 = 3 8 = 256 = 2 9 = 512 = 2 10 = 1024 = 4 11 = 2048 = NA 12 = 4096 = 2 13 = 8192 = 2 14 = 16384 = 3 15 = 32768 = 3 16 = 65536 = 2 17 = 131072 = 2 18 = 262144 = 3 19 = 524288 = 6 20 = 1048576 = 2 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go ?=> member(N,0..20), println(N=(2**N)=p(N)), fail, nl. go => true. % 99 chars p(N)=cond(T.len==0,"NA",T.last)=>V=2**N,S=V.to_string.map(to_int),L=S.len,T=[L-P+1:P in 1..L,odd(S[P])]. /* % ungolfed p(N) = cond(T.len==0,"NA",T.last) => V=2**N, S=V.to_string.map(to_int), L=S.len, T=[L-P+1:P in 1..L,odd(S[P])]. */