9
Introduction
I stumbled across this (useless) pattern the other day while I was watching TV. I named it "the 9 pattern" because the first number to use it was 9. The gist of it is, you enter a number (let's say x), and then you get back:
- x
- x + (x / 3) [let's call this y]
- two-thirds of y [let's call this z]
- z + 1
So, if I put inside this pattern the number 9 as x, this is what would come out:
- 9 (9)
- 12 (9 + 9 / 3) [9 over 3 is 3, and 9 + 3 is 12]
- 8 (12 times two-thirds) [a third of 12 is 4, and 4 * 2 is 8]
- 9 (8 + 1 is 9)
Challenge
Write me a function (in any programming language) that takes in a number, and outputs an integer array using the pattern.
Somewhat like this psuedo-code:
function ninePattern(int myInt) returns IntegerArray {
int iterationA = myInt + (myInt / 3);
int iterationB = iterationA * (2 / 3);
int iterationC = iterationB + 1;
IntegerArray x = [myInt, iterationA, iterationB, iterationC];
return x;
}
Clarifications
Discussions have been arousing in comments regarding the specifications of the question. This section is meant to clarify some of those.
"better to count in bytes than characters"
I picked characters because (for me, at least) it would be easier to judge. Of course, I can't change that now. (lots of answers are already posted)
"rounding"
Rounding follows this rhyme:
If it's 5 or more, raise the score
If it's 4 or less, let it rest
Simply, put, if it is something like 4.7 or 3.85, round them to 5 and 4 respectively.
Examples
Input => Result
9 => [9, 12, 8, 9]
8 => [8, 11, 7, 8]
6 => [6, 8, 5, 6]
23 => [23, 31, 21, 22]
159 => [159, 212, 141, 142]
If, however, the numbers are something like 2.3 or 10.435446, round them to 2 and 10 respectively.
"language support"
You are free to not use functions and/or arrays IF AND ONLY IF the language of your choice does not support them. If it does (even if it will increase your characters count), you must use them.
1Must the output be an array, or are the numbers by themselves enough (like the Pyth answer)? – David – 2016-05-24T00:46:11.697
@David must be an array – InitializeSahib – 2016-05-24T00:47:19.850
2
You are free to restrict to just full programs, or just functions, but there is discussion on meta of the defaults, which gives some useful background in case it affects your decision for future challenges. By default challenges accept both, to allow more languages to compete.
– trichoplax – 2016-05-24T01:38:51.5631
There are defaults for input and output too. Again, you don't have to follow them, this is just to let you know.
– trichoplax – 2016-05-24T01:46:15.447I took a shot at it in brainfuck but I don't think it's possible for it to work with all numbers since brainfuck can't do floating point division. This code will work for all multiples of 9 as input (9,18,27,36,etc). Link: http://bit.ly/1Wen2Cl. (Had to shorten link because it's 1783 characters)
– Keatinge – 2016-05-24T03:13:30.5573-1 for the arbitrary array and function requirements, which prevents languages without an array/list type or functions from competing. – Mego – 2016-05-24T04:15:30.197
4
Also, you should score the contestants in bytes, not in characters. We have a Sandbox, where you can get feedback on your post before it goes live.
– Loovjo – 2016-05-24T05:21:08.2701Is
y = round(x + x/3); z = round(⅔ y)
, or does the rounding happen only in the final step of printing the results? – Lynn – 2016-05-24T10:59:15.5001What if the language does not have array? Can we use delimiters to separate them? – Leaky Nun – 2016-05-24T11:13:29.660
Another thing, counting in characters is often uninteresting, as it encourages boring base encoding of answers. In addition, you don't specify how rounding needs to be performed. Does 6.5 round to 6 or 7? What about 7.5? What about "near misses" due to floating point precision? – FryAmTheEggman – 2016-05-24T18:06:06.653
"Write me a function" Does this mean it's OK to write
int[] aJavaMethodOrFunctionOrWhateverThatIsNotInsideAClassOrAnEnum(int i)
? – user8397947 – 2016-05-24T19:10:58.050@dorukayhan That's acceptable by default. – Mego – 2016-05-24T19:51:18.393
@FryAmTheEggman The usual meaning of rounding is half-up. – Mego – 2016-05-24T19:51:48.460
@Mego Many languages actually use banker's rounding by default, where if the digit before the 5 in the decimal representation of the number is even, you round down, but if it is odd you round up. In any case, I don't think it is a default that can be assumed (I've never seen anything on meta about it, anyway). – FryAmTheEggman – 2016-05-24T19:55:21.583
I recommend adding more examples as some of the answers indeed work with the example 9 but fail with other inputs. – Frozn – 2016-05-24T20:54:59.847
I've explained some of the complaints in a new "Clarifications" section. – InitializeSahib – 2016-05-24T23:18:54.487
I'm sure you meant round to 5 and 4 instead of 4 and 3 in the clarification. Else I misunderstood maths. – Frozn – 2016-05-24T23:24:40.240
@Frozn Oh, yes I did mean that! Editing now – InitializeSahib – 2016-05-24T23:25:52.723