Is it a Satisfying Number?

10

0

inspired by this chat conversation

A satisfying number is a number whose decimal representation is of the form abx, with the following properties:

  • x is the longest trailing repeating suffix, or the last digit if there is no repetition at the end (123333 -> 3333, 545656 -> 5656, 123 -> 3)
  • b is the single digit prior to x (123333 -> 2, 55545656 -> 4)
  • a is the remaining prefix (123333 -> 1, 55545656 -> 555)
  • a == c**b (** denotes exponentation), where c is the number of repetitions of the smallest repeating portion of x (1623333 -> 4 (3 3 3 3, not 33 33))

For example, 8300 is a satisfying number with a = 8, b = 3, c = 2, and x = 00. 24651 is not a satisfying number, because x = 1, b = 5, a = 246, and there is no integer c that satisfies c^5 = 246. 1222 is also not a satisfying number, because with x = 222 and b = 1, there are no remaining digits for a.

Given a positive integer n >= 100, output whether or not n is a satisfying number.

Examples

8300: True (a=8, b=3, c=2, x=00)
24651: False 
1222: False
92555: True (a=9, b=2, c=3, x=555)
64633: True (a=64, b=6, c=2, x=33)
512944: True (a=512, b=9, c=2, x=44)
123: True (a=1, b=2, c=1, x=3)
822809: False 
376664: False 
723799: False 
1234: False 
34330000000: True (a=343, b=3, c=7, x=0000000)
92313131: True (a=9, b=2, c=3, x=313131)
16424442444: True (a=16, b=4, c=2, x=24442444)

Mego

Posted 2017-12-22T19:00:07.183

Reputation: 32 998

Sandbox – Mego – 2017-12-22T19:00:26.203

2

Also somewhat related.

– Mr. Xcoder – 2017-12-22T19:12:31.580

With 8333 is x,c,b,a=33,2,3,8 and therefore satisfying? – Jonathan Allan – 2017-12-22T20:18:05.367

@JonathanAllan No, because x is greedy. – Mego – 2017-12-22T20:18:40.823

OK, thanks, so a is undefined here? (hence not satisfactory?) – Jonathan Allan – 2017-12-22T20:19:29.503

1@JonathanAllan Exactly. A number having at least two digits before a repeating portion is a necessary condition for being satisfying. – Mego – 2017-12-22T20:21:23.530

Huh. I’d thought a number like 477223 is satisfying. – Stan Strum – 2017-12-22T20:28:13.317

Must the number of repetitions be chosen as large as possible? For example, in 1645555, can we choose 55 as the repeated string so that x = 55 55 and c = 2? – Zgarb – 2017-12-23T10:19:42.493

Another test case: 16424442444. – Zgarb – 2017-12-23T11:19:08.043

@Zgarb "x is the longest trailing repeating suffix" - as explained in a previous comment, x is greedy. – Mego – 2017-12-23T12:22:39.163

@Mego But what about c? If x = 5555, c could be 2 (two repetitions of 55) or 4 (four repetitions of 5). – Dennis – 2017-12-23T14:57:43.480

@Dennis I see the confusion now. I'll clarify. – Mego – 2017-12-23T15:16:13.900

Does longest repeating suffix refer to longest in terms of what is repeated, the number of times it is repeated, the length of all of the repetitions, or does it not matter? So is the longest repeating suffix of 133333 equal to 3 3 3 3 3 or 33 33? – Daniel – 2017-12-26T04:32:07.227

@Dopapp Longest repeating suffix means the longest string at the end of the original string that has repetition. The grouping does not matter for the purpose of determining the longest repeating suffix. – Mego – 2017-12-27T02:02:29.387

Answers

2

Jelly, 26 bytes

feels too long

DŒṖṖEÐƤḄ$ÐṀLÐṂṪµḢ*@0¦LµṪ⁼Ḍ

A monadic link taking an integer and returning 1 if the input is satisfying and 0 if not.

Try it online! or see a test-suite

How?

DŒṖṖEÐƤḄ$ÐṀLÐṂṪµḢ*@0¦LµṪ⁼Ḍ - Link: integer, n    e.g. 8300
D                          - to decimal list          [8,3,0,0]
 ŒṖ                        - all partitions           [[[8],[3],[0],[0]],[[8],[3],[0,0]],[[8],[3,0],[0]],[[8],[3,0,0]],[[8,3],[0],[0]],[[8,3],[0,0]],[[8,3,0],[0]],[[8,3,0,0]]]
   Ṗ                       - pop (the "no tail" one)  [[[8],[3],[0],[0]],[[8],[3],[0,0]],[[8],[3,0],[0]],[[8],[3,0,0]],[[8,3],[0],[0]],[[8,3],[0,0]],[[8,3,0],[0]]]
         ÐṀ                - keep maximal under the operation: e.g. for [[8,3],[0],[0]]
        $                  -   last two links as a monad:
     ÐƤ                    -     for suffixes:   i.e. [[[8,3],[0],[0]],[[0],[0]],[[0]]]
    E                      -       all equal?         [0              ,1        ,1]
       Ḅ                   -     convert from binary  3
                           -          ...which yields [[[8],[3],[0],[0]],[[8,3],[0],[0]]]
            ÐṂ             - keep minimal under the operation:
           L               -   length
                           -          ...which yields [[[8,3],[0],[0]]]
              Ṫ            - tail (rightmost)         [[8,3],[0],[0]] 
               µ           - monadic chain separation
                Ḣ          - yield head and modify    [8,3]   ...leaving [[0],[0]]
                     L     - length (of modified)     2
                    ¦      - sparse application       (to the [8,3])
                   0       -   ...to index: 0         (to the rightmost digit, the 3)
                 *@        -   power (sw@p args)      [8,8]  ([8, 3*@2] = [8, 2*3] = [8,8])
                      µ    - monadic chain separation
                       Ṫ   - yield tail and modify    8   ...leaving [8]
                         Ḍ - from decimal (modified)  8
                        ⁼  - equal?                   1

Jonathan Allan

Posted 2017-12-22T19:00:07.183

Reputation: 67 804

1

Don't worry, you're not alone. This piece of unfinished torture should prove my point.

– Erik the Outgolfer – 2017-12-22T22:34:57.483

1If the Jelly answer is >20 bytes, you know somethings wrong... – FantaC – 2017-12-23T03:44:03.920

1

Python 3, 141 bytes

a=b=0;k=[]
c=[*input()];l=len(c)
while c[1:]>[]==k:a=a*10+b;b=int(c.pop(0));k=[i for i in range(2,l)if c==i*c[:l//i]];l-=1
a==(k+[1])[0]**b>q

Try it online!

Python 3, 144 bytes

a=b='';k=[]
c=[*input()];l=len(c)
while c[1:]>[]==k:a+=b;b,*c=c;k=[i for i in range(2,l)if c==i*c[:l//i]];l-=1
int(a or-1)==(k+[1])[0]**int(b)>q

Try it online!

output is via exit code

ovs

Posted 2017-12-22T19:00:07.183

Reputation: 21 408

You can alter your while condition to save a byte: TIO

– FlipTack – 2017-12-22T22:44:22.023

0

Perl 6, 66 bytes

{/^(\d+?)(\d)((\d+?)$0+||\d)$/&&($2.comb/($2[0]||1).comb)**$1==$0}

Try it online!

Sean

Posted 2017-12-22T19:00:07.183

Reputation: 4 136

0

JavaScript (ES6), 282 268 bytes

t=(d,n,l,i,r)=>d.slice((m=-l*i)-l,m).join``!=n?r:t(d,n,l,i+1,{p:r.p+n,r:r.r+1});p=(d,l,r)=>l<1?r:p(d,l-1,r.r<(z=t(d,(m=d.slice(-l).join``),l,1,{p:m,r:1})).r&&(z.r>1|l==1)?z:r);u=n=>(d=[...n]).slice(0,(q=(s=d.length)-(m=p(d,s,{p:"",r:0})).p.length-1)).join``==m.r**d[q]

function onChange() {
  var value = document.getElementById("input").value;
  console.log("%s => %s", value, u(value));
}
<input id="input" type="number" onchange="onChange()" />

Huntro

Posted 2017-12-22T19:00:07.183

Reputation: 459

0

Python 3, 101 bytes

import re
x,_,b,a=re.findall(r"((.+?)\2+)(.)(.*)",input()[::-1])[0]
if int(a[::-1])!=len(x)**int(b):d

Python 3, 107 bytes

import re
x,_,b,a=re.findall(r"((.+?)\2+)(.)(.*)",input()[::-1])[0]
if int(a[::-1])!=len(x)**int(b):int('')

Output is by exit code.

This code doesn't run properly on Tio due to a range error. Works perfectly in IDLE.

Neil

Posted 2017-12-22T19:00:07.183

Reputation: 2 417

0

Python 2, 286 bytes

yeesh.

n=`input()`
N=lambda p,l=0:N(n[:-len(p)],p,l+1)if n[-len(p):]==p else l
try:
    s=[(N(n[-i-1:]),n[-i-1:])for i,_ in enumerate(n)if N(n[-i-1:])!=1]
    if len(s)==0:s=[(1,n[-1])]
    q=max(s,key=lambda a:len(a[0]*a[1]))
    i=len(q[0]*q[1])
    print n[:-i-1]==`min(s)[0]**int(n[-i-1])`
except:print 0

N is a recursive function that finds the number of times a suffix substring is repeated in a string. This basically loops through all possible suffixes, finding the number of times each is repeated using N; this excludes all values where N==1 because they refer to no repeats; if the list ends up being empty, the suffix of the last character is appended to the list.

Then, the longest suffix is taken, (q), the number of characters it takes up is found (i), and a==c**b is checked (print ...).

If an error occurs along the way (which it often will), it is caught in the except block.

Any suggestions are more than welcome!

Daniel

Posted 2017-12-22T19:00:07.183

Reputation: 6 425