41
2
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 code-golf so least amount of bytes wins
Related OEIS: A024023
4I 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 – 2017-01-06T14:56:02.173
Maybe instead of
-1
can we output some other distinct falsy value? – user41805 – 2017-01-06T15:04:05.457Yes you can. I edited my question, and now you can output a falsy value. – P. Ktinos – 2017-01-06T15:08:33.307
Would this kind of "magic formula" make sense?
if (OEIS Sequence contains the number) return number; else return -1
– devRicher – 2017-01-06T16:29:10.883Can 0 be returned instead of -1, seeing as the result will never be 0 (because X has to be positive)? – FlipTack – 2017-01-06T16:55:37.017
2Unless if it's a falsy statement in your programming language, then no. – P. Ktinos – 2017-01-06T16:57:33.510
You said it should output
-1
or falsy, so why is-1
invalid? – devRicher – 2017-01-06T17:18:59.443@devRicher I never said -1 is invalid, I answered to FlipTack who asked if 0 is valid.
Also I didnt understand your question. What do you mean if oeis sequence contains the number. The sequence for 3^x-1 is obviously infinite – P. Ktinos – 2017-01-06T17:25:21.137
2May I want the number to be input in ternary? – John Dvorak – 2017-01-06T17:35:13.220
@JanDvorak No you may not. Most programming languages can't accept base3 numbers / convert decimal to ternary or vice versa easily (having a built-in method) so it would be unfair for them – P. Ktinos – 2017-01-06T21:28:27.320
1How large of a number does our program need to support? – HyperNeutrino – 2017-01-07T03:06:58.110
2having to handle non-negative intergers would make 0
3^0-1
a valid output and thus not useable as false, – Jasen – 2017-01-07T07:40:44.0932anyone thinking of using
log()
in their answer should confirm it giives the correct answer5
when242
is input. – Jasen – 2017-01-07T09:57:32.353@AlexL. The input should only be limited by memory to store the number. If the algorithm would work with any number in real life, it would be fine to use any integer max value. – P. Ktinos – 2017-01-07T14:11:37.653
does that mean bignums, or is it ok to use a normal integer type? and if so how small? – Jasen – 2017-01-07T19:04:20.547
@Jasen Just use the "average" integer size. By average I mean, not "long", not "short". Eg, in VB.NET you have int16, int32, int64. In that case, you would use int32. Of course, if you want to use "long" becase it is less bytes, go ahead. Size doesnt matter, if you use something that was designed to store numbers. – P. Ktinos – 2017-01-07T19:10:51.207