27
\$\begingroup\$

Given a nonnegative integer, return whether it is a three digit number ending in one, in any consistent integer base. In other words, the number needs to be represented in base-N, N being an integer greater than zero.

Rules

  • This is , so shortest answer wins.
  • Since unary behaves weirdly, the behavior with input 310 is undefined.
  • Standard loopholes are prohibited.

Examples

True:

5   
73  
101 
1073
17
22
36
55
99  

False:

8
18
23
27
98
90
88
72
68

A handful of large numbers:

46656 true
46657 true
46658 true
46659 true
46660 true
46661 false
46662 false
46663 true
46664 false
46665 true
46666 true
46667 false
46668 false
46669 false
46670 true
46671 true
\$\endgroup\$
  • 1
    \$\begingroup\$ Since unary behaves weirdly no, it doesn't behave weirdly, the unary representation of a non-negative integer n is just n 1s, e.g. 0 = ()₁, 3 = (111)₁, 10 = (1111111111)₁, etc. \$\endgroup\$ – Erik the Outgolfer Dec 16 '17 at 9:28
  • 6
    \$\begingroup\$ @EriktheOutgolfer It does behave quite differently; you can't divide by 1 to n-itshift, for example. \$\endgroup\$ – wizzwizz4 Dec 16 '17 at 12:27
  • 3
    \$\begingroup\$ What does consistent integer base mean? (Also, instead of excluding unary in the rules you could just specify N ≥ 2.) \$\endgroup\$ – Lynn Dec 16 '17 at 14:25
  • 1
    \$\begingroup\$ @Lynn A positional notation with a single radix, e.g. base ten, as opposed to a position-dependent radix like you see with imperial units or time. \$\endgroup\$ – HAEM Dec 16 '17 at 20:38
  • 1
    \$\begingroup\$ @Lynn as an addendum, I was also trying to exclude rational, negative, complex etc. bases. As for your second point, the rule about unary is intended to neither include nor exclude unary. Unless my grasp of language lawyering is even feebler than I thought, "undefined behavior" means "whatever the implementing party wants", which is what I was going for. \$\endgroup\$ – HAEM Dec 16 '17 at 20:57

20 Answers 20

10
\$\begingroup\$

Jelly, 7 bytes

bRṫ€3ċJ

Returns the number of bases (non-zero being truthy, zero being falsy) in which the input is a three digit number ending in one.

Try it online!

How it works

bRṫ€3ċJ  Main link. Argument: n

 R       Range; yield [1, ..., n].
b        Base; convert n to bases 1, ..., n.
  ṫ€3    Tail each 3; remove the first two elements of each digit array.
      J  Indices of [n]; yield [1].
     ċ   Count the number of times [1] appears in the result to the left.
\$\endgroup\$
10
\$\begingroup\$

JavaScript (ES7), 43 40 39 bytes

f=(n,b)=>n<b*b?0:n%b==1&n<b**3|f(n,-~b)

Test cases

f=(n,b)=>n<b*b?0:n%b==1&n<b**3|f(n,-~b)

console.log('[Truthy]')
console.log(f(5))
console.log(f(73))
console.log(f(101))
console.log(f(1073))
console.log(f(17))
console.log(f(22))
console.log(f(36))
console.log(f(55))
console.log(f(99))
console.log(f(46656))
console.log(f(46657))
console.log(f(46658))
console.log(f(46659))
console.log(f(46660))
console.log(f(46663))
console.log(f(46665))
console.log(f(46666))
console.log(f(46670))
console.log(f(46671))

console.log('[Falsy]')
console.log(f(8))
console.log(f(18))
console.log(f(23))
console.log(f(27))
console.log(f(98))
console.log(f(90))
console.log(f(88))
console.log(f(72))
console.log(f(68))
console.log(f(46661))
console.log(f(46662))
console.log(f(46664))
console.log(f(46667))
console.log(f(46668))
console.log(f(46669))

Commented

f = (n,           // given n = input
        b) =>     // and using b = base, initially undefined
  n < b * b ?     // if n is less than b²:
    0             //   n has less than 3 digits in base b or above -> failure
  :               // else:
    n % b == 1 &  //   return a truthy value if n is congruent to 1 modulo b
    n < b**3 |    //   and n is less than b³ (i.e. has less than 4 digits in base b)
    f(n, -~b)     //   or the above conditions are true for some greater value of b
\$\endgroup\$
7
\$\begingroup\$

Python 3, 50 47 bytes

-2 bytes thanks to @LeakyNun
-1 byte thanks to @Dennis

lambda n:any(i>n/i/i>n%i==1for i in range(2,n))

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Haskell, 41 40 bytes

f n=or[mod n k==1|k<-[2..n],k^2<n,n<k^3]

Thanks to @Zgarb for golfing off 1 byte!

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Brachylog, 10 bytes

≥ℕ≜;?ḃ₍Ṫt1

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ 8 bytes, although I'm not entirely sure why it works but some close variants don't. \$\endgroup\$ – Zgarb Dec 16 '17 at 18:37
4
\$\begingroup\$

05AB1E, 11 8 bytes

Saved 3 bytes thanks to Adnan.

Lв3ù€θ1å

Try it online!

Explanation

Lв            # convert input to bases [1 ... input]
  ʒg3Q}       # keep only elements of length 3
       €θ     # get the last item of each
         1å   # is there any 1?
\$\endgroup\$
3
\$\begingroup\$

Jelly, 12 bytes

bRµL€żṪ€3,1e

Try it online!

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

Mathematica, 43 bytes

!FreeQ[IntegerDigits[#,2~Range~#],{_,_,1}]&

Try it online!

or Try it online! (large numbers)

Martin Ender saved 3 bytes

\$\endgroup\$
  • \$\begingroup\$ !FreeQ[#~IntegerDigits~Range@#,{_,_,1}]& is a bit shorter if you don't mind seeing the IntegerDigits::ibase: Base 1 is not an integer greater than 1. warning. (It still returns the correct answers.) \$\endgroup\$ – Misha Lavrov Dec 16 '17 at 14:21
3
\$\begingroup\$

Wolfram Language (Mathematica), 35 bytes

Or@@Array[x~Mod~#==1<x/#^2<#&,x=#]&

Try it online!

Explicitly checks whether n % i = 1 and i2 < n < i3 for any possible base i. For golfing purposes, the inequality is rearranged to 1 < n/i2 < i, so that it can be chained onto the equality.

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

Clean, 58 56 bytes

-2 thanks to Dennis

import StdEnv
@n=or[n>m^2&&n<m^3&&n rem m==1\\m<-[2..n]]

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Husk, 10 bytes

€;1ṠMo↓2Bḣ

Try it online! Pretty close to the Jelly answer of Dennis.

\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Unicode), 21 20 14 bytesSBCS

-5 thanks to @ngn.

Purely arithmetic solution (doesn't actually do any base conversions) and thus very fast.

3∊⊢(|×∘⌈⍟)⍨1↓⍳

Try it online!

⊢()⍨1↓⍳ on one dropped from the ɩndices 1…argument and the argument, apply:

| the division remainders

×∘⌈ times the rounded-up

 logN Argument

3∊ is three a member of that?

\$\endgroup\$
  • \$\begingroup\$ you can save 1 with the standard trick of swapping the arguments: ⊢(∨/(3=∘⌈⍟)∧1=|)⍨1↓⍳ \$\endgroup\$ – ngn Jan 15 '18 at 15:15
  • \$\begingroup\$ @ngn Thanks. Next time, feel free to just edit such (if you feel like it). \$\endgroup\$ – Adám Jan 15 '18 at 15:23
  • \$\begingroup\$ ok. Here's a more complex improvement which I leave for you to handle - it makes this as short as your other solution: (⊂1 3)∊⊢(⌈|,¨⍟)⍨1↓⍳ \$\endgroup\$ – ngn Jan 15 '18 at 15:49
  • 1
    \$\begingroup\$ 3∊⊢(|×|×∘⌈⍟)⍨1↓⍳ \$\endgroup\$ – ngn Jan 15 '18 at 15:59
  • 2
    \$\begingroup\$ @ngn 1=⌈a⍟b, a≤ba=b0=a|b0=b|b \$\endgroup\$ – Adám Jan 15 '18 at 16:57
1
\$\begingroup\$

Pyth, 10 bytes

}]1>R2jLQS

Verify all the test cases.

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

Husk, 15 bytes

V§&o=1→o=3LṠMBḣ

Try it online!

Explanation

V§&(=1→)(=3L)ṠMBḣ  -- implicit input, for example: 5
             ṠMB   -- map "convert 5 to base" over..
                ḣ  --   range [1..5]
                   -- [[1,1,1,1,1],[1,0,1],[1,2],[1,1],[1,0]]
V                  -- does any of the elements satisfy the following
 §&( 1 )( 2 )      --   apply functions 1,2 and join with & (logical and)
         =3L       --     is length equals to 3?
    =1→            --     is last digit 1?
\$\endgroup\$
1
\$\begingroup\$

PHP, 48+1 bytes

while(++$b**2<$n=$argn)$n%$b-1|$n>$b**3||die(1);

exits with 0 for falsy (or input 3), 1 for truthy.
Run as pipe with -nR or try it online.

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

C, 60 bytes

A function that returns non-zero if the argument can be represented as a three-digit number ending in 1:

i,j;f(n){for(j=0,i=sqrt(n);i>cbrt(n);)j+=n%i--==1;return j;}

Note: this works with GCC, where the functions are built-in. For other compilers, you probably need to ensure that the argument and return types are known:

#include<math.h>

Explanation

The lowest base in which n is represented in 3 digits is ⌊∛n⌋, and the lowest base in which n is represented in 2 digits is ⌊√n⌋, so we simply test whether the number is congruent to 1 modulo any bases in the 3-digit range. We return the count of the number of bases satisfying the condition, giving a non-zero (truthy) or zero (falsy) value as appropriate.

Test program

Pass any number of inputs as positional parameters:

#include<stdio.h>
int main(int c,char**v)
{
    while(*++v)
        printf("%s => %d\n", *v, f(atoi(*v)));
}
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 19 bytesSBCS

Dennis' method.

(⊂,1)∊2↓¨⊢⊥⍣¯1¨⍨1↓⍳

Try it online!

(⊂,1)∊ Is [1] a member of

2↓¨ two elements dropped from each of

⊢⊥⍣¯1¨⍨ the argument represented in each of the bases

1↓⍳ one dropped from the ɩndices 1 through the argument?

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

Julia, 31 bytes

n->any(i>n/i^2>n%i==1for i=2:n)

Try it online!

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

Pyt, 35 33 bytes

←ĐĐ3=?∧∧:ŕĐ2⇹Ř⇹Ľ⅟⌊⁺3=⇹Đ2⇹Ř%*ž1⇹∈;

Explanation:

←ĐĐ                                             Push input onto stack 3 times
   3=?  :                       ;               If input equals 3, execute code after the question mark;otherwise, execute code after the colon. In either case, afterwards, execute the code after the semicolon
      ∧∧                                        Get 'True'
        :                                       Input not equal to 3
         ŕ                                      Remove 'False'
          Đ2⇹Ř                                  Push [2,3,...,n]
              ⇹Ľ⅟⌊⁺                             Push [floor(log_2(n))+1,floor(log_3(n))+1,...,floor(log_n(n))+1]
                   3=                           Is each element equal to 3
                     ⇹                          Swap the top two elements on the stack (putting n back on top)
                      Đ2⇹Ř                      Push [2,3,...,n]
                          %                     Push [n%2,n%3,...,n%n]
                           *                    Multiply [n%x] by the other array (i.e. is floor(log_x(n))+1=3?)
                            ž                   Remove all zeroes from the array
                             1⇹∈                Is 1 in the array?
                                ;               End conditional
                                                Implicit print

Try it online!

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

><>, 42 bytes

1<v?)}:{*::+
n~\1-:::**{:}):?!n~:{:}$%1=:?

Try it online!

Returns 10 for truthy, 00 for falsey.

\$\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.