Is this a three-digit number ending in one?

27

2

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

HAEM

Posted 2017-12-16T09:21:24.847

Reputation: 651

1Since 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. – Erik the Outgolfer – 2017-12-16T09:28:48.250

@EriktheOutgolfer every position is worth the same in unary, which is a bit weird for a positional notation. But really, I'm just unsure whether including or excluding unary would waste more bytes for the one special case. – HAEM – 2017-12-16T09:43:31.497

Actually the only thing special about it is that it doesn't have the 0 digit, otherwise it works as any other base. However, your challenge is your decision. – Erik the Outgolfer – 2017-12-16T09:48:32.753

6@EriktheOutgolfer It does behave quite differently; you can't divide by 1 to n-itshift, for example. – wizzwizz4 – 2017-12-16T12:27:56.133

3What does consistent integer base mean? (Also, instead of excluding unary in the rules you could just specify N ≥ 2.) – Lynn – 2017-12-16T14:25:20.670

1@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. – HAEM – 2017-12-16T20:38:35.597

1@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. – HAEM – 2017-12-16T20:57:35.480

Sorry but I don't quite follow the discussion about unary. If unary is allowed, then you can hardcode "true" as output because any integer will end in 1 in unary representation. – CompuChip – 2017-12-18T09:46:08.463

@CompuChip this is why the "three digit" requirement is also there, which makes 3 the only unary that returns true. – HAEM – 2017-12-18T09:50:40.517

Ah, that's where I misread... thought the input was a three-digit number (in decimal) and the question was whether it ends in one in any base. Makes sense then. – CompuChip – 2017-12-18T14:50:46.897

Answers

10

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

Arnauld

Posted 2017-12-16T09:21:24.847

Reputation: 111 334

10

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.

Dennis

Posted 2017-12-16T09:21:24.847

Reputation: 196 637

7

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!

ovs

Posted 2017-12-16T09:21:24.847

Reputation: 21 408

6

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!

Dennis

Posted 2017-12-16T09:21:24.847

Reputation: 196 637

5

Brachylog, 10 bytes

≥ℕ≜;?ḃ₍Ṫt1

Try it online!

Leaky Nun

Posted 2017-12-16T09:21:24.847

Reputation: 45 011

28 bytes, although I'm not entirely sure why it works but some close variants don't. – Zgarb – 2017-12-16T18:37:13.180

4

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?

Emigna

Posted 2017-12-16T09:21:24.847

Reputation: 50 798

3

Jelly, 12 bytes

bRµL€żṪ€3,1e

Try it online!

Erik the Outgolfer

Posted 2017-12-16T09:21:24.847

Reputation: 38 134

3

Mathematica, 43 bytes

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

Try it online!

or Try it online! (large numbers)

Martin Ender saved 3 bytes

J42161217

Posted 2017-12-16T09:21:24.847

Reputation: 15 931

!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.) – Misha Lavrov – 2017-12-16T14:21:41.577

3

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!

Οurous

Posted 2017-12-16T09:21:24.847

Reputation: 7 916

3

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.

Martin Ender

Posted 2017-12-16T09:21:24.847

Reputation: 184 808

2

Husk, 10 bytes

€;1ṠMo↓2Bḣ

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

Zgarb

Posted 2017-12-16T09:21:24.847

Reputation: 39 083

2

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?

Adám

Posted 2017-12-16T09:21:24.847

Reputation: 37 779

you can save 1 with the standard trick of swapping the arguments: ⊢(∨/(3=∘⌈⍟)∧1=|)⍨1↓⍳ – ngn – 2018-01-15T15:15:01.540

@ngn Thanks. Next time, feel free to just edit such (if you feel like it). – Adám – 2018-01-15T15:23:08.697

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↓⍳ – ngn – 2018-01-15T15:49:33.800

13∊⊢(|×|×∘⌈⍟)⍨1↓⍳ – ngn – 2018-01-15T15:59:40.077

@ngn Why the double ? – Adám – 2018-01-15T16:23:52.183

to avoid matching residue=3 and logarithm=1 – ngn – 2018-01-15T16:35:44.330

2@ngn 1=⌈a⍟b, a≤ba=b0=a|b0=b|b – Adám – 2018-01-15T16:57:33.067

excellent :) I didn't think about that – ngn – 2018-01-15T17:04:54.247

1

Pyth, 10 bytes

}]1>R2jLQS

Verify all the test cases.

Mr. Xcoder

Posted 2017-12-16T09:21:24.847

Reputation: 39 774

1

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?

ბიმო

Posted 2017-12-16T09:21:24.847

Reputation: 15 345

1

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.

Titus

Posted 2017-12-16T09:21:24.847

Reputation: 13 814

1

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)));
}

Toby Speight

Posted 2017-12-16T09:21:24.847

Reputation: 5 058

1

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?

Adám

Posted 2017-12-16T09:21:24.847

Reputation: 37 779

0

Julia, 31 bytes

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

Try it online!

LukeS

Posted 2017-12-16T09:21:24.847

Reputation: 421

0

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!

mudkip201

Posted 2017-12-16T09:21:24.847

Reputation: 833

0

><>, 42 bytes

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

Try it online!

Returns 10 for truthy, 00 for falsey.

Jo King

Posted 2017-12-16T09:21:24.847

Reputation: 38 234