Power-Ending Patterns

20

2

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.

Calvin's Hobbies

Posted 2020-02-02T12:52:04.993

Reputation: 84 000

I assume we can't indefinitely print the list? – RGS – 2020-02-02T14:02:53.137

4@RGS No. The point is to just print the minimal sequence of 1, 2, or 4 digits. – Calvin's Hobbies – 2020-02-02T14:03:36.013

5"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." & [tag:kolmogorov-complexity] - which is it? – Jonathan Allan – 2020-02-02T14:43:40.887

1...I guess the former since you give examples, I suggest removing the KC tag and "Pretty much reproduce the list above." – Jonathan Allan – 2020-02-02T14:52:34.357

@JonathanAllan I figured there could be elements of KC in some solutions since the data is pretty small. And for the inputs 0 to 9 the list would be reproduced. Sorry if that's not clear - I'm open to better phrasing? – Calvin's Hobbies – 2020-02-02T15:11:39.943

4If 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$. – Post Rock Garf Hunter – 2020-02-02T15:47:27.513

Are we allowed to produce incorrect results upon overflow? – S.S. Anne – 2020-02-02T23:24:47.913

@S.S.Anne Like if the input integer is too big? That's fine. As long as your program can accept integers up to your language's usual maximum, like 2^31-1. – Calvin's Hobbies – 2020-02-03T00:43:50.073

1No, if the calculations of the integers inside the functions are too big. – S.S. Anne – 2020-02-03T00:47:36.567

1@S.S.Anne Ah. Good point. It's ok if things overflow if you're taking powers. – Calvin's Hobbies – 2020-02-03T02:40:02.753

@Calvin'sHobbies: compactly encoding a lookup table can be part of a code-golf answer. That doesn't mean the overall problem is one of Kolmogorov complexity. It's nice that the question points out that the pattern for any number is the pattern for it's lowest decimal digit. Maybe you want to replace some of the other phrasing with that? But @ JonathanAllen already told you how to fix the biggest misleading part: simply remove the sentence "Pretty much reproduce the list above." The test cases are clearer without that. – Peter Cordes – 2020-02-03T10:33:43.547

1@640KB Nope, sorry. – Calvin's Hobbies – 2020-02-04T01:18:06.597

Answers

6

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

RGS

Posted 2020-02-02T12:52:04.993

Reputation: 5 047

1ah of course! I forgot about these – mabel – 2020-02-02T21:02:17.910

1you can replace :i for with :1for to save a byte – mabel – 2020-02-02T21:13:57.160

@mabel clever trick! – RGS – 2020-02-02T21:18:52.953

Ah I knew it was possible @xnor but I couldn't remember what it was called, thanks for sharing – mabel – 2020-02-03T12:14:05.020

5

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

Jonathan Allan

Posted 2020-02-02T12:52:04.993

Reputation: 67 804

5

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!

Arnauld

Posted 2020-02-02T12:52:04.993

Reputation: 111 334

4

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.

mabel

Posted 2020-02-02T12:52:04.993

Reputation: 1 489

3

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

user41805

Posted 2020-02-02T12:52:04.993

Reputation: 16 320

19 bytes – Jo King – 2020-02-02T22:59:04.677

1@JoKing *∘⍳∘4 -> ⎕*⍳4 to make it a "full program" – ngn – 2020-02-05T02:13:32.437

Thanks, to both of you. – user41805 – 2020-02-06T19:18:21.460

3

05AB1E, 7 6 bytes

-1 byte thanks to Kevin Cruijssen

4LmT%Ù

Try it online!

Just a port of Jonathan Allan's Jelly answer.

mabel

Posted 2020-02-02T12:52:04.993

Reputation: 1 489

You can remove the , since it's done implicitly. – Kevin Cruijssen – 2020-02-03T10:06:02.703

Thank you! I didn't know that @KevinCruijssen – mabel – 2020-02-03T12:10:29.500

3

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

Luis Mendo

Posted 2020-02-02T12:52:04.993

Reputation: 87 464

3

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!

RGS

Posted 2020-02-02T12:52:04.993

Reputation: 5 047

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!

– Laikoni – 2020-02-10T08:13:39.057

1

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!

– Laikoni – 2020-02-10T08:20:12.167

@Laikoni thanks for your suggestions! – RGS – 2020-02-10T08:55:05.703

2

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!

chau giang

Posted 2020-02-02T12:52:04.993

Reputation: 725

1do you need to take n modulo 10? – Expired Data – 2020-02-03T10:48:24.690

@Expired Data thank for your solution, I just updated my answer. – chau giang – 2020-02-03T11:09:39.820

1

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!

Guillermo Phillips

Posted 2020-02-02T12:52:04.993

Reputation: 561

1

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

Krzysztof Szewczyk

Posted 2020-02-02T12:52:04.993

Reputation: 3 819

1

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.

Neil

Posted 2020-02-02T12:52:04.993

Reputation: 95 035

1

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)

Wernisch

Posted 2020-02-02T12:52:04.993

Reputation: 2 534

1

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)

Kevin Cruijssen

Posted 2020-02-02T12:52:04.993

Reputation: 67 575

1

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!

Noodle9

Posted 2020-02-02T12:52:04.993

Reputation: 2 776

Wow. I feel ashamed now... – S.S. Anne – 2020-02-03T11:59:54.890

51 bytes – S.S. Anne – 2020-02-03T12:22:33.543

@S.S.Anne Nice one, was thinking a for loop might golf some bytes but hadn't figured it out. Thanks! – Noodle9 – 2020-02-03T12:25:27.933

https://codegolf.stackexchange.com/a/168932/89298 – S.S. Anne – 2020-02-03T12:27:22.520

@S.S.Anne Interesting, makes sense. :-) – Noodle9 – 2020-02-03T12:29:30.263

1

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

640KB

Posted 2020-02-02T12:52:04.993

Reputation: 7 149

0

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]

RGS

Posted 2020-02-02T12:52:04.993

Reputation: 5 047

0

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

Neil

Posted 2020-02-02T12:52:04.993

Reputation: 95 035

0

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.

Jo King

Posted 2020-02-02T12:52:04.993

Reputation: 38 234

0

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!

S.S. Anne

Posted 2020-02-02T12:52:04.993

Reputation: 1 161

Can you drop n%=10;? – Jonathan Frech – 2020-02-02T23:16:01.320

@JonathanFrech No. For larger integers omitting that will cause overflow. – S.S. Anne – 2020-02-02T23:18:18.880

1Most of the time such overflow deviations are acceptable; one may need to ask the OP for clarification. – Jonathan Frech – 2020-02-02T23:21:52.687

0

J, 15 bytes

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

Try it online!

K (Kona), 14 bytes

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

Try it online!

Galen Ivanov

Posted 2020-02-02T12:52:04.993

Reputation: 13 815

0

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

DeathIncarnate

Posted 2020-02-02T12:52:04.993

Reputation: 916

0

Ruby, 30 bytes

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

Try it online!

G B

Posted 2020-02-02T12:52:04.993

Reputation: 11 099

0

Pyth, 9 bytes

{m%^QdTS4

Try it online!

famous1622

Posted 2020-02-02T12:52:04.993

Reputation: 451