10
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ Sandbox \$\endgroup\$ – Mego Dec 22 '17 at 19:00
  • 2
    \$\begingroup\$ Also somewhat related. \$\endgroup\$ – Mr. Xcoder Dec 22 '17 at 19:12
  • \$\begingroup\$ With 8333 is x,c,b,a=33,2,3,8 and therefore satisfying? \$\endgroup\$ – Jonathan Allan Dec 22 '17 at 20:18
  • \$\begingroup\$ @JonathanAllan No, because x is greedy. \$\endgroup\$ – Mego Dec 22 '17 at 20:18
  • 1
    \$\begingroup\$ @JonathanAllan Exactly. A number having at least two digits before a repeating portion is a necessary condition for being satisfying. \$\endgroup\$ – Mego Dec 22 '17 at 20:21
2
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ Don't worry, you're not alone. This piece of unfinished torture should prove my point. \$\endgroup\$ – Erik the Outgolfer Dec 22 '17 at 22:34
  • 1
    \$\begingroup\$ If the Jelly answer is >20 bytes, you know somethings wrong... \$\endgroup\$ – FantaC Dec 23 '17 at 3:44
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ You can alter your while condition to save a byte: TIO \$\endgroup\$ – FlipTack Dec 22 '17 at 22:44
0
\$\begingroup\$

Perl 6, 66 bytes

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

Try it online!

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

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.

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

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()" />

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

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.