21
\$\begingroup\$

When you look at the last decimal digit of each power of a non-negative integer a repeating pattern forms. For example, 3:

3^1 = 3 3^2 = 9 3^3 = 27 3^4 = 81 3^5 = 243 3^6 = 729 3^7 = 2187 3^8 = 6561 3^9 = 19683 

The last digits go 3971 which repeats indefinitely. In fact any number we choose that ends in 3 has that same pattern because none of the other digits can have an effect on the ones place during repeated multiplication.

What's curious is that some numbers have a much shorter cycle of power-ending digits. For example with 5 the powers all end in 5 so the pattern, written as short as possible, is simply 5.

Looking at the minimal power-ending digits patterns for 0 through 9 we get:

0 -> 0 1 -> 1 2 -> 2486 3 -> 3971 4 -> 46 5 -> 5 6 -> 6 7 -> 7931 8 -> 8426 9 -> 91 

(The lengths of these being 11442 repeated is a curious tidbit itself.)

Remember, any numbers above 9 will have the same pattern as their last digit as was explained above with 3.

Challenge

Your challenge here is to write a program that takes in any non-negative integer and outputs its minimal power-ending digit pattern.

The exact output formatting, whether string or list, doesn't matter. For example, here are some potential inputs followed by valid potential outputs:

900 -> [0] 11 -> 1 2 -> 2486 303 -> 3, 9, 7, 1 44 -> 4 6 45 -> 5 666 -> "6" 3857 -> [7 9 3 1] 118 -> '8426' 129 -> [9, 1] 

The shortest code in bytes wins.

\$\endgroup\$
12
  • 4
    \$\begingroup\$ @RGS No. The point is to just print the minimal sequence of 1, 2, or 4 digits. \$\endgroup\$ Commented Feb 2, 2020 at 14:03
  • 7
    \$\begingroup\$ "Your challenge here is to write a program that takes in any non-negative integer and outputs it's minimal power-ending digit pattern" but then "Pretty much reproduce the list above." & kolmogorov-complexity - which is it? \$\endgroup\$ Commented Feb 2, 2020 at 14:43
  • 1
    \$\begingroup\$ ...I guess the former since you give examples, I suggest removing the KC tag and "Pretty much reproduce the list above." \$\endgroup\$ Commented Feb 2, 2020 at 14:52
  • 4
    \$\begingroup\$ If we want to put this in the math jargon, this is the semigroup generated by \$n\$ on the monoid \$\langle\mathbb{Z}_{10},\times\rangle\$. \$\endgroup\$ Commented Feb 2, 2020 at 15:47
  • 1
    \$\begingroup\$ No, if the calculations of the integers inside the functions are too big. \$\endgroup\$ Commented Feb 3, 2020 at 0:47

25 Answers 25

6
\$\begingroup\$

Python 3, 44 43 40 bytes

Based off of mabel's answer but using dictionary comprehensions:

lambda n:[*{n**i%10:0for i in[1,2,3,4]}] 

Saved 1 byte thanks to mabel.

Saved 3 bytes thanks to xnor.

You can try it online

\$\endgroup\$
5
  • 1
    \$\begingroup\$ ah of course! I forgot about these \$\endgroup\$ Commented Feb 2, 2020 at 21:02
  • 1
    \$\begingroup\$ you can replace :i for with :1for to save a byte \$\endgroup\$ Commented Feb 2, 2020 at 21:13
  • \$\begingroup\$ @mabel clever trick! \$\endgroup\$ Commented Feb 2, 2020 at 21:18
  • 2
    \$\begingroup\$ Python 3's list unpacking can save a few bytes: tio.run/##K6gsycjPM/6fpmCrEPM/… \$\endgroup\$ Commented Feb 3, 2020 at 3:21
  • \$\begingroup\$ Ah I knew it was possible @xnor but I couldn't remember what it was called, thanks for sharing \$\endgroup\$ Commented Feb 3, 2020 at 12:14
5
\$\begingroup\$

Jelly, 6 bytes

*Ɱ4%⁵Q 

A monadic link which accepts an integer and yield a list of integers.

Try it online!

How?

*Ɱ4%⁵Q - Link: integer, n 4 - four Ɱ - map across (implicit range of [1..4]) with: * - exponentiate ⁵ - ten % - (powers) mod (ten) Q - de-duplicate 
\$\endgroup\$
5
\$\begingroup\$

JavaScript (ES6), 41 bytes

n=>[n%=10]+[[,,486,971,6,,,931,426,1][n]] 

Try it online!


JavaScript (ES6), 42 bytes

A recursive version.

n=>(g=k=>(d=(k*n)%10)-n?[k]+g(d):k)(n%=10) 

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Python 3, 54 bytes

lambda n:list(dict.fromkeys(n**i%10for i in(1,2,3,4))) 

Try it online!

Could have been 38 bytes if set() was ordered.

\$\endgroup\$
3
\$\begingroup\$

05AB1E, 7 6 bytes

-1 byte thanks to Kevin Cruijssen

4LmT%Ù 

Try it online!

Just a port of Jonathan Allan's Jelly answer.

\$\endgroup\$
2
  • \$\begingroup\$ You can remove the , since it's done implicitly. \$\endgroup\$ Commented Feb 3, 2020 at 10:06
  • \$\begingroup\$ Thank you! I didn't know that @KevinCruijssen \$\endgroup\$ Commented Feb 3, 2020 at 12:10
3
\$\begingroup\$

MATL, 7 bytes

4:^10\u 

Try it online!

Explanation

4: % Push [1 2 3 4] ^ % Implicit input, n. Element-wise power: gives [n n^2 n^3 n^4] 10\ % Modulo 10 u % Unique (remove duplicates). Implicit display 
\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog Unicode), 10 8 bytes

Jo King suggested a shorter function than what I originally had, ∪10⊤*∘⍳∘4 (a train at 9 bytes) vs my 10-byter dfn, and ngn pointed out a tradfn would be shorter than both altogether

∪10⊤⎕*⍳4 

Try it online!

⍳4 the vector 1 2 3 4

⎕* the input raised to the power of, i.e. (⍵*1)(⍵*2)(⍵*3)(⍵*4) (using to represent the value of the input)

10⊤ mod 10

unique

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 9 bytes \$\endgroup\$ Commented Feb 2, 2020 at 22:59
  • 1
    \$\begingroup\$ @JoKing *∘⍳∘4 -> ⎕*⍳4 to make it a "full program" \$\endgroup\$ Commented Feb 5, 2020 at 2:13
  • \$\begingroup\$ Thanks, to both of you. \$\endgroup\$ Commented Feb 6, 2020 at 19:18
3
\$\begingroup\$

Haskell, 48 46 bytes

import Data.List;f n=nub[n^i`mod`10|i<-[1..4]]

-2 bytes thanks to @Laikoni

Having to import nub is really annoying... Any suggestions to remove it?

You can Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Here's a version without nub, but it is also 48 bytes: n!i|x<-n^i`mod`10=x:[y|i<4,y<-n!(i+1),y/=x];(!1) Try it online! \$\endgroup\$ Commented Feb 10, 2020 at 8:13
  • 1
    \$\begingroup\$ However, your version can be shortened to 46 bytes: import Data.List;f n=nub[n^i`mod`10|i<-[1..4]] Try it online! \$\endgroup\$ Commented Feb 10, 2020 at 8:20
  • \$\begingroup\$ @Laikoni thanks for your suggestions! \$\endgroup\$ Commented Feb 10, 2020 at 8:55
2
\$\begingroup\$

JavaScript (Node.js), 42 bytes

n=>new Set([1,2,3,4].map(v=>(n%10)**v%10)) 

Try it online!

JavaScript (Node.js), 37 bytes

This solution is suggested by @Expired Data, it is shorter, elegant but it will have problems when working with large number.

n=>new Set([1,2,3,4].map(v=>n**v%10)) 

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ do you need to take n modulo 10? \$\endgroup\$ Commented Feb 3, 2020 at 10:48
  • \$\begingroup\$ @Expired Data thank for your solution, I just updated my answer. \$\endgroup\$ Commented Feb 3, 2020 at 11:09
2
\$\begingroup\$

x86-16 machine code, IBM PC DOS, 26 bytes

Binary:

00000000: b380 8a07 d72c 308a d850 0430 b40e cd10 .....,0..P.0.... 00000010: 58f6 e3d4 0a3a c375 f0c3 X....:.u.. 

Unassembled:

B3 80 MOV BL, 80H ; BX to command line input tail 8A 07 MOV AL, BYTE PTR[BX] ; input length into AL D7 XLAT ; AL = [BX+AL] (get the last char of input) 2C 30 SUB AL, '0' ; convert from ASCII 8A D8 MOV BL, AL ; save N to BL for compare/multiply POW_LOOP: 50 PUSH AX ; save AX 04 30 ADD AL, '0' ; convert to ASCII B4 0E MOV AH, 0EH ; BIOS tty function CD 10 INT 10H ; call BIOS, write char to console 58 POP AX ; restore AX F6 E3 MUL BL ; AX = AL * BL D4 0A AAM ; AL = AL % 10 3A C3 CMP AL, BL ; is sequence repeating? 75 F0 JNE POW_LOOP ; if not, keep looping C3 RET ; return to DOS 

A standalone PC DOS executable program. Input via command line args.

I/O:

enter image description here

enter image description here

\$\endgroup\$
1
\$\begingroup\$

PHP, 54 50 bytes

for($n=$o=$argn%10;print$n;$n-$o||die)$n=$n*$o%10; 

Try it online!


Alternate Solution:

PHP, 50 45 bytes

for(;$i<'11442'[$argn%5];)echo$argn**++$i%10; 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Retina 0.8.2, 58 50 bytes

.*(.) $1$1$*_,,486,971,6,,,931,426,1 +`_\d*, ,.* 

Try it online! Link includes test cases. Edit: Saved 8 bytes by not including the input digit in the digit patterns. Explanation:

.*(.) $1$1$*_,,486,971,6,,,931,426,1 

Create a unary copy of the last digit and append the list of power-ending digit pattern suffixes.

+`_\d+, 

Delete the appropriate number of entries from the start of the list.

,.* 

Delete unneeded entries from the end of the list.

Actually calculating the digits takes 69 bytes:

M!`.$ {`(.).* $&;$1$*_,$&$* _(?=.*,(1*))|,1* $1 ;(1{10})*(1*) $.2 D`. 

Try it online! Link includes test cases. Explanation:

M!`.$ 

Modulo the input by 10.

{` 

Repeat while new digits can be added.

(.).* $&;$1$*_,$&$* 

Create unary copies of the first digit and power-ending digits so far.

_(?=.*,(1*))|,1* $1 

Multiply them together.

;(1{10})*(1*) $.2 

Take the remainder modulo 10 and convert it to decimal.

D`. 

Deduplicate the digits.

Of course it only takes 34 bytes in Retina 1:

L`.$ {`(.).* $&_$.(*$1* _.*\B D`. 

Try it online! Link includes test cases. Explanation:

L`.$ 

Modulo the input by 10.

{` 

Repeat while new digits can be added.

(.).* $&_$.(*$1* 

Multiply the power-ending pattern so far by its first digit. (* has higher precedence than $^, so multiplying by its reverse ends up costing a byte more than it saves.)

_.*\B 

Modulo the result by 10.

D`. 

Deduplicate the digits.

\$\endgroup\$
1
\$\begingroup\$

Perl 6, 23 bytes

((*X**1..4)X%10).unique 

Try it online!

Returns the unique values of the input to the power of 1 through 4 modulo 10.

\$\endgroup\$
1
\$\begingroup\$

Excel, 53 bytes

=CHOOSE(RIGHT(A1)+1,,1,2486,3971,46,5,6,7931,8426,91) 

RIGHT(A1) defaults to RIGHT(A1,1), making it more efficient than MOD(A1,10)

\$\endgroup\$
1
\$\begingroup\$

MathGolf, 6 bytes

4╒#♂%▀ 

port of @JonathanAllan's Jelly answer, so make sure to upvote him as well!

Try it online.

Explanation:

4╒ # Push the list [1,2,3,4] # # Take the (implicit) input-integer to each of this power ♂% # Take modulo-10 for each of these ▀ # Uniquify the list # (after which the entire stack is output implicitly) 
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 56 51 bytes

Saved 5 bytes thanks to S.S. Anne!!!

i;f(n){for(i=n%10;putchar(i+48),i=n*i%10,i-n%10;);} 

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Wow. I feel ashamed now... \$\endgroup\$ Commented Feb 3, 2020 at 11:59
  • \$\begingroup\$ 51 bytes \$\endgroup\$ Commented Feb 3, 2020 at 12:22
  • \$\begingroup\$ @S.S.Anne Nice one, was thinking a for loop might golf some bytes but hadn't figured it out. Thanks! \$\endgroup\$ Commented Feb 3, 2020 at 12:25
  • \$\begingroup\$ codegolf.stackexchange.com/a/168932/89298 \$\endgroup\$ Commented Feb 3, 2020 at 12:27
  • \$\begingroup\$ @S.S.Anne Interesting, makes sense. :-) \$\endgroup\$ Commented Feb 3, 2020 at 12:29
1
\$\begingroup\$

x86_64 machine code, 63 61 bytes

B8 BF 84 7B 09 B9 0A 00 00 00 31 D2 C7 44 24 FC A4 10 0A 00 48 C1 E0 22 48 89 44 24 EC 48 B8 3C 00 00 00 00 00 5E 24 48 89 44 24 F4 89 F8 66 F7 F1 48 89 D0 83 E0 0F 66 03 54 44 EC C3 

Disassembly:

00000000 B8BF847B09 mov eax,0x97b84bf 00000005 B90A000000 mov ecx,0xa 0000000A 31D2 xor edx,edx 0000000C C74424FCA4100A00 mov dword [rsp-0x4],0xa10a4 00000014 48C1E022 shl rax,byte 0x22 00000018 48894424EC mov [rsp-0x14],rax 0000001D 48B83C0000000000 mov rax,0x245e00000000003c -5E24 00000027 48894424F4 mov [rsp-0xc],rax 0000002C 89F8 mov eax,edi 0000002E 66F7F1 div cx 00000031 4889D0 mov rax,rdx 00000034 83E00F and eax,byte +0xf 00000037 66035444EC add dx,[rsp+rax*2-0x14] 0000003C C3 ret 

Input: edi, Output: dx, requires rsp to be set to a correct value.

Example call:

main: mov edi, 1337 call f movzx eax, dx ret 
\$\endgroup\$
1
\$\begingroup\$

Ruby, 30 bytes

->n{(1..4).map{|g|n**g%10}|[]} 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Pyth, 9 bytes

{m%^QdTS4 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Charcoal, 24 bytes

⭆⁺0§⪪”←‴Ki⦃k‽” Iθ﹪⁺IθIιχ 

Try it online! Link is to verbose version of code. Uses the repetition between the first and second halves of the table. Explanation:

 ”←‴Ki⦃k‽” Compressed string ` 264 648 2` ⪪ Split on literal space § Iθ Cyclically index by the input as a number ⁺0 Prefix a literal `0` ⭆ Map over characters and join ﹪⁺IθIιχ Add to the input and reduce modulo 10 
\$\endgroup\$
0
\$\begingroup\$

Jelly, 7 bytes

4R*@%⁵Q 

You can try it online. [It doesn't beat Jonathan's answer nor was it posted first. I just got to this answer by myself and I like posting Jelly answers while I'm learning]

\$\endgroup\$
0
\$\begingroup\$

C (gcc), 116 bytes

x,c,a[4];f(n){n%=10;for(c=0,x=n;c<4;x*=n)a[c++]=x%10;n=*a*10+a[1];n=a[2]*10+a[3]-n?n*100+a[2]*10+a[3]:*a-a[1]?n:*a;} 

Can almost certainly be golfed more but I haven't figured it out yet.

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Can you drop n%=10;? \$\endgroup\$ Commented Feb 2, 2020 at 23:16
  • \$\begingroup\$ @JonathanFrech No. For larger integers omitting that will cause overflow. \$\endgroup\$ Commented Feb 2, 2020 at 23:18
  • 1
    \$\begingroup\$ Most of the time such overflow deviations are acceptable; one may need to ask the OP for clarification. \$\endgroup\$ Commented Feb 2, 2020 at 23:21
  • \$\begingroup\$ Suggest x=n%=10;for(c=0; instead of n%=10;for(c=0,x=n; \$\endgroup\$ Commented Jul 4, 2020 at 5:22
0
\$\begingroup\$

J, 15 bytes

[:~.10|]^1+i.@4 

Try it online!

K (Kona), 14 bytes

{?(x^1+!4)!10} 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Burlesque, 12 bytes

ri4ro?^)[~NB 

Try it online!

ri # Read input to int 4ro # Range [1,4] ?^ # Raise input to powers [1,4] )[~ # Take last digit of each NB # Remove duplicates 
\$\endgroup\$
0
\$\begingroup\$

Pip, 12 bytes

UQ(a**\,4)%t 

takes range from 1 to 4, takes powers, mod 10, uniquify.

Try it online!

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.