# Can this number be written in (3^x) - 1 format?

Challenge:

Create a program that accepts a positive integer and checks if it can be written in the form of (3^x)-1, where X is another positive integer.

If it can, output X

If it can't, output -1 or a falsy statement.

Example inputs/outputs

Input:

2


It can be written as (3^1) - 1, so we output x which is 1

Output:

1


Input:

26


26 can be written as (3^3) - 1, so we output x (3)

Output:

3


Input:

1024


1024 can't be written in the form of (3^x) - 1, so we output -1

Output:

-1


This is so least amount of bytes wins

Related OEIS: A024023

• I ask to output X because I believe it's more challenging that way. Simply finding if it is of format 3^x - 1 would be too easy for a challenge, in my opinion. – P. Ktinos Jan 6 '17 at 14:56
• Unless if it's a falsy statement in your programming language, then no. – P. Ktinos Jan 6 '17 at 16:57
• May I want the number to be input in ternary? – John Dvorak Jan 6 '17 at 17:35
• having to handle non-negative intergers would make 0 3^0-1 a valid output and thus not useable as false, – Jasen Jan 7 '17 at 7:40
• anyone thinking of using log() in their answer should confirm it giives the correct answer 5 when 242 is input. – Jasen Jan 7 '17 at 9:57

## Pyke, 9 6 bytes

3m^Qh@


Try it here!

3m^    -  map(3**i, range(input))
@ - V in ^
Qh  -  input + 1


Old 9 byte version:

b3'l}\2q*


Try it here!

## Pyth 8 bytes

xm^3dUQh

UQ  # generate all values 1..Q (Q is the input)
m^3d    # map 3^d over this ^ list
x      h # find the input+1 (hQ) in the result of the last command


Try here

# R, 34 25 bytes

a=log(scan()+1,3);a*!a%%1


Calculate the base 3 logarithm of the input + 1. Test if the result is an integer, if it is it outputs it, if not it outputs 0 as falsey value. Thanks to @Billywob for the extra 9 bytes off!

Test cases:

> a=log(scan()+1,3);a*!a%%1
1: 1024
2:
[1] 0

> a=log(scan()+1,3);a*!a%%1
1: 26
2:
[1] 3


Old version at 34 bytes which outputs -1 as falsey value.

a=log(scan()+1,3);if(!a%%1,a,-1)

• If you do a*!a%%1 it will output a if true and 0 otherwise and you can skip the if thing. – Billywob Jan 6 '17 at 16:07
• The spec says "If it can't, output -1 or a falsy statement." and 0 is interpreted as FALSE in R so I would say it's valid. – Billywob Jan 6 '17 at 16:19

# C, 81 bytes

i,j,k;f(n){for(i=0;++i<n;){for(k=3,j=0;++j<i;k*=3);if(n==k-1)return i;}return-1;}

• I think you save bytes by using pow(3,i) instead of defining your own. Gcc complains about the missing #include <math.h> but compiles it anyway. I did have to cast to int. You also may be able to gain some by adding an r variable, initializing to -1, and then if(n==k-1)r=i;}return r;} – nmjcman101 Jan 6 '17 at 18:54
• @nmjcman101 Thanks, but pow() produces some incorrect results because of floating point inaccuracy. (When cast to int, 2.9999 will be 2, not 3). Adding a variable r sounds like a good idea, but it actually results in a 2 bytes longer code. – Steadybox Jan 6 '17 at 22:33
• yeah, log doesn't work, pow probably won't either. – Jasen Jan 7 '17 at 9:28

# Japt, 8 bytes

o m!³a°U


Try it online!

This code expands into the following:

Uo m!p3 a++U

Uo            // Create the range [0...U).
m!p3       // Map each item X to 3**X.
a++U  // Take the index of U+1. Returns -1 if it doesn't exist.
// Implicit: output result of last expression

• Very nice solution. – Oliver Jan 7 '17 at 6:04

# GolfScript, 11 bytes

~..3\?\)%!*


Try it online!

Uses the fact that 3n is divisible by n+1 if and only if n+1 itself is a power of 3. Outputs its input n if n+1 is a power of 3, otherwise outputs 0 (which is falsy in GolfScript).

De-golfed:

~             # eval the input, converting it from string to integer
..           # make two copies of the input number
3\?        # raise 3 to the power of the input number
\)%     # reduce the result modulo the input number plus one
!    # boolean negate the result, mapping 0 to 1 and all other values to 0
*   # multiply the input number with the result


Ps. Here's a simple test harness that runs the code above (minus the initial ~, which is not needed since the inputs are already numbers) on all integers from 0 to 9999 and prints those for which it returns a truthy result:

10000,{ ..3\?\)%!* },


The output of this program should be:

[2 8 26 80 242 728 2186 6560]


(The output doesn't include 0 because, even though the formula used does correctly detect it as one less than a power of 3, the result is still 0 × 1 = 0, and thus falsy. Fortunately, 0 is not a positive integer, and thus isn't a valid input for this challenge anyway.)

# Octave, 23 bytes

@(x)find(3.^(1:x)-1==x)


Verify all test cases!

Explanation:

This is an anonymous function that takes a positive integer x as input. .^ is element-wise power in Octave, so 3.^(1:x) is 3^1, 3^2, 3^3 .... Subtracting 1 gives 3^1-1, 3^2-1, 3^3-1 ... which can be compared to x.

find(a,b) takes a vector a as input, and attempts to find the scalar b in that vector and returns its index. If it's not found then it will output an empty matrix []. An empty matrix is a falsey value in Octave.

find(3.^(1:x)-1==x) searches for x in the vector 3^1-1, 3^2-1, 3^3-1 ... and attempts to return its index. If it's not in the vector then it returns an empty (falsey) matrix.

# C, 76 bytes

main(i,a,c){scanf("%d",&a);for(c=0,++a;i<a;i*=3,++c);printf("%d",(i==a)*c);}


# Sagemath, 45 bytes

This is simply @dfernan's solution repackaged as Sagemath (which is basically Python + some math libraries loaded by default and syntactic sugar).

In Sagemath, we can avoid the import math and we can use ^ for exponentiation, so we save a few chars.

def f(n):x=ceil(log(n,3));print((3^x-1==n)*x)


Test it online

c++ 60 bytes

int f(n){float o=log1p(n)/log(3);return o/floor(o)!=1?-1:o;}


explanation:

int f(n){
float o=log1p(n)/log(3);       // eval for x using log3 function
return o/floor(o)!=1?-1:o;     // if no remainder output X
}

• I think you don't have to use uint16_t. You can use normal int. – Roman Gräf Jan 8 '17 at 19:55
• I guess I could to save a few bytes but it seemed to me that if I wasn't safeguarding against negative integer input then I wasn't following the guidelines. – mreff555 Jan 14 '17 at 21:04
• this compile to me: int f(n){float o=log1p(n)/log(3);return o/floor(o)!=1?-1:o;} – user58988 Apr 29 '17 at 15:53
• @mreff555 You don't need to safeguard against negative input, you can assume positive input. – Erik the Outgolfer Apr 29 '17 at 16:05

# Python 2, 34 bytes

lambda n:(~n>>3**n%-~n*n)**4/80%80


Try it online!

Works for all Python ints, up to at least 2^100.

# Pip, 13 bytes

aTB:3MNa=2&#a


Try it online!

### Explanation

               a is 1st command-line argument (implicit)
aTB:3          Convert a to base 3 and assign back to a
MNa=2     Does the min of a's digits equal 2?
&    Logical-and
#a  Length of a
If there are non-2 digits, we get the falsey value 0; otherwise, we get
the number of digits

• "26 can be written as (3^3) - 1" 124 can be written as (5^3)-1 but your code for 124 not print 5 print 0 – user58988 Feb 2 '18 at 15:48
• Ok I confuse exponent and base – user58988 Feb 2 '18 at 15:51

# 05AB1E, 6 bytes

>3.n.ï


Try it online!

> increments, 3 pushes a 3 to the stack, .n find the logarithm with base 3, .ï checks if it is equal to its integer part.

Returns 0 for falsy: If it can't, output -1 or a falsy statement.

• "26 can be written as (3^3) - 1" 124 can be written as (5^3)-1 but your code for 124 not print 5 print 0 – user58988 Feb 2 '18 at 15:47
• @RosLuP I believe the output of my program is correct, and other answers seem to agree – Mr. Xcoder Feb 2 '18 at 15:49
• Ok I confuse the exponent and the base – user58988 Feb 2 '18 at 15:50

# APL (Dyalog Unicode), 12 bytesSBCS

Anonymous tacit prefix function.

(⊢×⌊=⊢)3⍟1+⊢


Try it online!

1+⊢ increment

3⍟ log3

() apply the following function:

⌊=⊢ is the floor equal to the argument? (0 or 1)

⊢× multiply the argument by that

• "26 can be written as (3^3) - 1" 124 can be written as (5^3)-1 but your code for 124 not print 5 print 0 – user58988 Feb 2 '18 at 15:44
• Ok I confuse exponent and base – user58988 Feb 2 '18 at 15:51

# APL (Dyalog), 11 bytes

⊢|⊢⍳⍨¯1+3*⍳


Try it online!

Uses ⎕IO←0.

How?

⍳ - range of 0 to n-1.

3* - raise 3 to the power of each element.

¯1+ - decrement each by 1.

⊢⍳⍨ - search the index of n in that list (if not exists, this would return the maximum index plus 1 - which is n.

⊢| - modulo by n. This would keep the index, if found, and zero-out numbers not contained in the list that would produce n % n = 0.

# APL (Dyalog), 14 bytes

(∧/×≢)2=3⊥⍣¯1⊢


Try it online!

• "26 can be written as (3^3) - 1" 124 can be written as (5^3)-1 but your code for 124 not print 5 print 0 – user58988 Feb 2 '18 at 15:45
• Ok I confuse exponent and base – user58988 Feb 2 '18 at 15:52
• @RosLuP you mind deleting the comments? people use to DV without much thinking when seeing these – Uriel Feb 3 '18 at 16:17

# APL NARS, 14 chars or 28 bytes

{r×r=⌊r←3⍟1+⍵}


Test:

  f←{r×r=⌊r←3⍟1+⍵}
f 2
1
f 26
3
f 1024
0

• This is 28 bytes in NARS, but exactly the same solution is only 14 bytes in Dyalog APL. Also, you can save two bytes by conversion to tradfn, r×r=⌊r←3⍟1+⎕, letting the program prompt for input. – Adám Dec 25 '17 at 16:15
• @Adám i prefer functions – user58988 Dec 25 '17 at 19:47

# Befunge-93, 26 bytes

&1+>:3%v
1+\^v-1_3/\
.@.$_  Try It Online Prints 0 as the falsey. ### How it Works &1+... Gets the input and adds one ...... ...... ...>:3%v Check if the number is divisible by 3 1+\^..._3/\ If not, divide the number by 3 and increment a counter ... Repeat until the number is not divisible by 3 ......... If the final number is a one, print the counter ....v-1_... Else pop the counter and print a 0 .@.$_       End the program


## JavaScript (ES6), 40 bytes

f=(n,p=0,k=1)=>n<k?n>k-2&&p:f(n,p+1,k*3)


Returns false or the power. A simple port of @Arnauld's ES7 answer would have taken 43 bytes.

• I think f=(n,p,k=1)=>n<k?n>k-2&&p:f(n,-~p,k*3) works and saves 2 bytes. – Arnauld Jan 7 '17 at 0:24

# PHP, 36 47 bytes

If log(input+1,3) differs from its integer value, print 0; else print the logarithm:
<?=(0|$x=log($argv[1]+1,3))-$x?0:$x; (36 bytes) fails for 242.
<?=strstr($x=log($argv[1]+1,3),".")?0:$x; and <?=(0|$x=log($argv[1]+1,3))-$x>1e-7?0:$x; (41 bytes) may fail for larger $x.

This version is safe:

for(;3**++$x<$n=1+$argv[1];);echo$n<3**$x?0:$x;


1.Loop $x up from 1 while 3^$x is smaller than argument+1.
2.Print 0 if the expression is larger than input+1, \$x else.

Takes input from command line argument. Run with -nr.

• gives wrong answer for 242 input – Jasen Jan 7 '17 at 9:22
• @Jasen: The downvote was ridiculous, but it´s fixed now. – Titus Jan 7 '17 at 11:36

# Java 7, 180 bytes

class A{public static void main(String[]q)throws Exception{int a,b=0;while((a=System.in.read()-48)>=0)b=b*10+(a);double k=Math.log(b+1)/Math.log(3);System.out.print(k%1==0?k:-1);}}


Really simple approach. Input, then add one, then log3 the number, and if it's a integer, print it; otherwise, print -1. Could use some work.

Only works up to (3^19)-1.

• Why is this non-competing? The Non-Competing status is only reserved for languages or language features that were added after the challenge was posted. – user41805 Jan 7 '17 at 11:10
• As I said, size doesn't matter, so this can compete. – P. Ktinos Jan 7 '17 at 20:55
• @KritixiLithos Oh okay. I wasn't aware of the exact meaning of Non-Competing. Thanks. Also, I'll update the title. – HyperNeutrino Jan 8 '17 at 3:19
• You can use interface A{...} and drop the public from main(). Why do you have (a) instead of a in the while loop. I think you can use float k=... instead of double k=.... Should be -5 bytes if I counted right. – Roman Gräf Jan 8 '17 at 20:04
• @RomanGräf I appreciate your suggestions; however, none of them are of any use for me, unfortunately. Your first suggestion only works in Java 8, and there is already a far better Java 8 solution out there. I have (a) in the while loop because I am doing a comparison of an assignment statement, and assignment has the lowest priority on the order of operations and thus requires a set of brackets around it. Finally, I have double because Math#log returns a double and casting would obviously be much slower. Regardless, thank you for the suggestions, but I will not be incorporating them. – HyperNeutrino Jan 9 '17 at 3:20

# Common Lisp (SBCL), 83 50 bytes

(let*((i(read))(r(log(1+ i)3)))(if(=(mod r 1)0)r))


Old version:

(let((i(read)))(if(find-if(lambda(x)(not(eq x #\2)))(format()"~3R"i))()(log(1+ i)3)))


Feedback encouraged!

# CJam, 11 bytes

ri3b_2-!\,*


Basically it determines if the number's trinary (base 3) representation contains only 2's, and if it does, outputs the number of 2's. It outputs 0 if the number is not only 2's.

Try it online!

Explanation

ri          e# Get input as an integer
3b        e# Convert to base 3 (an array of the digits in base 3)
_       e# Duplicate
2-     e# Remove all 2's from the array
!    e# Boolean negation. Yields 1 if the array contained only 2's (and is now
e# empty), 0 otherwise
\   e# Swap top 2 elements of stack
,  e# Take the length of the base 3 digits array
* e# Multiply by the boolean value from before

• "Trinary" is more commonly referred to as Ternary. – FlipTack Jan 9 '17 at 18:23

## C 90 bytes

f(x){i=1;for(;i<x;i++){if((pow(3,i)-1)==x){printf("%d",i);break;}if(i==(x-1))puts("-1");}}


Ungolfed Version:

void f(int x)
{
int i=1;
for(;i<x;i++)
{
if((pow(3,i)-1)==x)
{
printf("%d",i);
break;
}
if(i==(x-1))
puts("-1");
}
}

• for x=1 what would return/print that above? – user58988 Apr 29 '17 at 15:43

# SmileBASIC, 32 bytes

INPUT X
X=LOG(X+1,3)?X*(X==X>>0)


Outputs 0 for false.

# Cardinal, 91 bytes

%
:
+
~
v~d<
0  /{<
+  A #-?+M?"-1"@
+  !   @.-<
+  M #  M!/        <
>~ # ^  } \       +^%


According to the specifications of Cardinal, this shouldn't work for inputs above 255. However, due to the implementation in TIO accepting values over 255, it will work past 255 up to 3^34.

Try it online!

## Explanation

%
:
+


Input + 1

~
v~d<
0  /{<
+  A
+  !
+  M
>~ # ^


While divisible by 3

#-?+M?"-1"@


Output -1 if not divisible by 3 or equal to 1 and end program

   .-<
#  M!/        <
} \       +^%


Output counter for how many times the input + 1 has been divided by 3 before equalling 1

## QBIC, 18 bytes

:[a|c=3^b-1~c=a|?b


Explanation

:        Get 'a' from the cmd-line
[a|      FOR b=1; b<=a; b++  (this runs a lot longer than we need...)
c=3^b-1  Set c to be 3^b-1
~c=a     IF this is the input given
|?b      THEN print b
END IF and NEXT are added implicitly


This would print multiple bs if it would be possible to have multiple solutions. Right now, it runs on after finding a solution. We could substitute ?b for _Xb to quit after finding a solution, but that adds a byte. Also, the FOR-loop [a| could be initialised as [a/3| to save us a lot of iterations, but that adds another 2 bytes.

# Axiom,72 65 63 bytes

g(x:PI):INT==(z:=floor(y:=log(x+1.)/log(3.));y-z~=0=>-1;z::INT)


some test

(51) -> [[x,g(x)]  for x in [1,2,3,4,5,6,7,8,9,10,26,27,79,80,242]]
(51)
[[1,- 1], [2,1], [3,- 1], [4,- 1], [5,- 1], [6,- 1], [7,- 1], [8,2],
[9,- 1], [10,- 1], [26,3], [27,- 1], [79,- 1], [80,4], [242,5]]


# TI-Basic (TI-84 Plus CE) 17 15 bytes

logBASE(Ans+1,3)
Ansnot(Ans-int(Ans


All tokens used are one-byte except logBASE(, which is two.

Explanation:

logBASE(Ans+1,3)   # inverse of 3^n-1 is log3(x+1)
Ansnot(Ans-int(Ans # if X is an integer, X-int(x is 0, so not(X-int(X is 1, which is multiplied by X
# if X is not an integer, X-int(X is not 0, so not(X-int(X is zero, which is multiplied by X
# last value evaluated is implicitly returned


# J, 14 bytes

[:(*>.=])3^.>:


Try it online!

Here's a variant using a range of powers and indexing (@Dennis's method).

(~:*])>:i.~3^i.


Here's a variant using base conversion (@orlp's method).

[:(#*&(*/)2=])3&#.inv


# Factor, 30 bytes

[ 1 + 3 over [0,b) n^v index ]


Try it online!

Port of Dennis' Jelly answer. The index word conveniently returns f (the only falsy value in Factor) when the item is not found.

[              ! anonymous lambda
1 + 3 over   ! ( n+1 3 n+1 )
[0,b)        ! ( n+1 3 {0..n} )
n^v          ! ( n+1 {3^0..3^n} )
index        ! 0-based index of n+1 in {3^0..3^n}; false if not found
]


# Factor, 45 bytes

[ 3 >base dup [ 50 = ] all? swap length and ]


Try it online!

A base-conversion approach.

[                    ! anonymous lambda, accepting a positive integer n
3 >base            ! ( str ) convert to base-3 string
dup [ 50 = ] all?  ! ( str ? ) test if it is all 2's
swap length and    ! ( len/f ) if true, return the length of str
! otherwise return false
]
`