# 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.

• Must the output be an array, or are the numbers by themselves enough (like the Pyth answer)? May 24, 2016 at 0:46
• 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. May 24, 2016 at 1:38
• There are defaults for input and output too. Again, you don't have to follow them, this is just to let you know. May 24, 2016 at 1:46
• -1 for the arbitrary array and function requirements, which prevents languages without an array/list type or functions from competing.
– user45941
May 24, 2016 at 4:15
• 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. May 24, 2016 at 5:21

# MarioLANG, 659621591582556543516458418401352308 369 bytes

rounding is quite expensive :/

Try it online

;>>[![( [( [( [( [( [<(([!)))!+(((-<>( >((+
:"==#================"===#== #=====""[ "==
)(  -[!)>>[![)  [)[<(!>)[<)) >))) [!!-[!((
(  )"#="==#======="=#==="=<="=====##==#==<
+  +>) )-+<>+)[!)+! +))![-[)>[ [([-[![<<:
+  )-+ )(=""===#==#  ==#===)"=======#=====
+  >!>)!>  !(- < !:+:))<  ))!((++)))<
)  "#"=#===#===" ======" ===#======="
!
=#========================


Well this was more fun than expected, this is probably not optimal but I guess i'm getting there.

Explanation time:

(for the 352 bytes version)

first we get the argument and print it :

;
:


simple enough

we then move to the bulk of the program : the division input / 3

;>>[![              [( [( [<result
:"==#======================"======
)   -[!)>>[![        [<((((!
)   )"#="==#=========="====#
+(  +>) )  +>(+)[!)+))!
+(  )-+ )  -"====#====#
+   >!>)!  >! -  <
"#"=#  "#===="
!
=#


which is a slightly modified conversion of the brainfuck division

[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]


which take for input

n 0 d 0 0


and give you back

0 n d-n%d n%d n/d


once we got the division we use it to get the sum of n and n/d and print it

;>>[![              [( [( [<    !+(((-<
:"==#======================"===)#====="
)   -[!)>>[![        [<((((!    >))) [!(((
)   )"#="==#=========="====#   ="=====#==:
+(  +>) )  +>(+)[!)+))!
+(  )-+ )  -"====#====#
+   >!>)!  >! -  <
"#"=#  "#===="
!
=#


we then need to do another division : ( 2 * ( n + n / d ) ) / 3

so we get ( 2 * ( n + n / d )

;>>[![              [( [( [<    !+(((-<
:"==#======================"===)#====="
)   -[!)>>[![        [<((((!    >))) [!(((
)   )"#="==#=========="====#   ="=====#==:
+(  +>) )  +>(+)[!)+))! 2*2n/d>[   -[![  <
+(  )-+ )  -"====#====# ======"======#====
+   >!>)!  >! -  <            !((++))<
"#"=#  "#===="            #======"
!
=#


and put it with 3 back into the division

;>>[![              [( [( [<    !+(((-<
:"==#======================"===)#====="
)   -[!)>>[![        [<((((!    >))) [!(((
)   )"#="==#=========="====#   ="=====#==:
+(  +>) )  +>(+)[!)+))!      )>[   -[![  <
+(  )-+ )  -"====#====#      )"======#====
+   >!>)!  >! -  <       +++))!((++))<
"#"=#  "#====" ===========#======"
!
=#=================


at that point everything explose, mario is stuck in an infinite loop doing division on bigger and bigger number, forever.

and to fix that we need a way to diferenciate between the first and the second division, it end up that, oh joy, we do have a way

;>>[![              [( [( [<([!)!+(((-<
:"==#======================"==#)#====="
)   -[!)>>[![        [<((((!))< >))) [!(((
)   )"#="==#=========="====#)="="=====#==:
+(  +>) )  +>(+)[!)+))!!:+:)))>[   -[![  <
+(  )-+ )  -"====#====#======)"======#====
+   >!>)!  >! -  <       +++))!((++))<
"#"=#  "#====" ===========#======"
!
=#=================


basically we look if the x in

x 0 n d-n%d n%d n/d


is 0, if it is it mean we are on the first division

else we are on the second division, and we just print the result of the division, add 1 then print it again

and voilà easy as pie.

• Welcome to PPCG! May 24, 2016 at 12:17
• Doesn't round to the specs provided by the question (of course, I did update it after you posted your answer, but you should at least update your answer to fit the new specs) May 27, 2016 at 23:39
• Done. while we are talking about test case you should add 10 to have a number that you round down on the fist operation. May 28, 2016 at 23:56

# Emotinomicon 99 bytes, 33 characters

😷😭,😲🆙🆙😼🆙😨😎⏬😎🆙😍➗➕🆙😨😎⏬😎😉✖😍➗🆙😨😎⏬😎😅➕😨


Explanation:

😷                                 clear output
😭                                begin quote string
,
😲                              end quote string
🆙                             duplicate top of stack
🆙                            duplicate top of stack
😼                           take numeric input
🆙                          duplicate top of stack
😨                         pop N and output as a number
😎                        reverse stack
⏬                       pops and outputs top of stack as character
😎                      reverse stack
🆙                     duplicate top of stack
😍                    push 3 to the stack
➗                   divide top two elements on stack
➕                  add top two elements on stack
🆙                 duplicate top of stack
😨                pop N and output as a number
😎               reverse stack
⏬              pops and outputs top of stack as character
😎             reverse stack
😉            push 2 to the stack
✖           multiply top two elements on stack
😍          push 3 to the stack
➗         divide top two elements on stack
🆙        duplicate top of stack
😨       pop N and output as a number
😎      reverse stack
⏬     pops and outputs top of stack as character
😎    reverse stack
😅   push 1 to the stack
➕  add top two elements on stack
😨 pop N and output as a number

• Yay for unconventional languages! :P May 26, 2016 at 19:19

# MATL, 14 bytes

Xot4*3/tE3/tQv


Try it Online

Pretty simple, v concatenates the stack into an array. Xo converts to an integer data-type, and all operations thereafter are integer operations.

• The spec is to return an array, not the final result only. May 24, 2016 at 0:27
• Also lol at those flagging for deletion in <2 minutes :D May 24, 2016 at 0:36
• @David D: I don't think I can retract deletion votes May 24, 2016 at 0:39
• @David upvoted your answer as my condolences :) May 24, 2016 at 0:48
• I'm pretty sure this isn't a function either, correct me if I'm wrong. May 24, 2016 at 1:03

# Cheddar, 27 bytes

b=8/9*$0 [$0,$0+$0/3,b,b+1]


$0 is variable with input. Cheddar just isn't a golfy language ¯\_(ツ)_/¯ , also this is non-competing because Cheddar's input functionality was made after this challenge. Ungolfed: IterationB := 8 / 9 *$0  // 8/9ths of the Input
[ $0, // The input$0 + $0 / 3, // Input + (Input/3) IterationB, // (see above) IterationB + 1 // above + 1 ]  • Tears of joy! It's gone so far! :D May 24, 2016 at 15:44 # Java, 868284 85 characters class c{int[]i(int I){int a=I+(I/3),b=(int)(a*(2d/3d));return new int[]{I,a,b,b+1};}}  The letter d placed right after an integer makes the integer a double. Ungolfed: class c{ int[] i(int I) { int a = I + (I / 3), b = (int)(a * (2d / 3d)); return new int[]{I, a, b, b + 1}; } }  Without the class (class c{} is 8 chars long), it downsizes to 76 characters: int[]i(int I){int a=I+(I/3),b=(int)(a*(2d/3d));return new int[]{I,a,b,b+1};}  More accurate version in 110 chars (118 with the enum) - it uses floats because ain't nobody got space for casting Math#round(double): int[]i(int I){float a=I+(I/3f),b=(a*(2f/3f));return new int[]{I,Math.round(a),Math.round(b),Math.round(b+1)};}  • I think I should learn Pyth. May 24, 2016 at 1:26 • +1, because ya know, java May 24, 2016 at 1:29 • @dorukayhan Weird, I seem to get an error when I'm trying to run this in Eclipse, it can't convert from double to int. I'll see if I can figure out what the problem is tomorrow. May 24, 2016 at 19:29 • I just fixed the code May 24, 2016 at 21:09 • I see but it doesn't give correct results for inputs like 8 or 10. The first addition isn't working properly as I + (I / 3) is using an integer division, meaning that fractions are discarded and thus the result isn't rounded properly. May 24, 2016 at 22:36 # Java, 56 80 Bytes As some users pointed out, this solution (as some others in java) does not round data properly. So now I'm presenting slightly longer solution which should return correct result int[]h(int a){int[]b={a,Math.round(a+a/3f),a=Math.round(a*8f/9),++a};return b;}  or 60 bytes lamda version a->new int[]{a,Math.round(a+a/3f),a=Math.round(a*8f/9),++a}  Golfed version int[]g(int a){int[]b={a,a+a/3,a*8/9,a*8/9+1};return b;}  and ungolfed int[] g(int a) { int[] b = { a, a + a / 3, a * 8 / 9, a * 8 / 9 + 1 }; return b; }  or 36 bytes defined as lambda a->new int[]{a,a+a/3,a*8/9,a*8/9+1}  • Does not have the rounding required by the Question. – Marv May 24, 2016 at 14:47 • As @Marv mentioned this question doesn't work correcty, e.g. for input 8 the expected result would be [8,11,7,8] but it is [8,10,7,8] May 24, 2016 at 20:48 • why downvote? i fixed it and it working correctly now? May 25, 2016 at 19:27 • Sorry for the downvote after you fixed it. The downvote is locked now, so I cannot remove it unless you make some change to your answer (any trivial change is enough) May 29, 2016 at 20:25 • @edc65 ok, done May 29, 2016 at 21:59 # Scratch, 33 bytes (source: cubeupload.com) Asks for input, sets a to input rounded, sets b and c to their respective changes, then says all four numbers, seperated by commas. # Java, 64 bytes int[]f(int i){return new int[]{i,i+=i/3+0.5,i-=i/3-0.5,i+=1.5};}  ### Notes • This has the required rounding build in, not sure if you can do it shorter if mixed with @user902383's solution. ### Ungolfed int[] f(int i) { return new int[]{ i, i += i / 3 + 0.5, i -= i / 3 - 0.5, i += 1.5}; }  ### Output with i=9 [9, 12, 8, 9]  • May 24, 2016 at 15:04 • Same as in user902383's solution, this doesn't work correctly, e.g. for 8 expected [8,11,7,8] but is [8,10,7,8], for 6 expected [6,8,5,6] but is [6,8,6,7] May 24, 2016 at 20:50 • @Frozn this solution is not working properly as well, and for test cases which you gave return same result as my old solution ideone.com/LVK8FU May 25, 2016 at 8:31 # 05AB1E, 15 bytes Code: Ð3/+D·3/D>)1;+ï  Uses CP-1252 encoding. Try it online!. • 11 bytes. I know, I know, this wasn't possible when you've posted it. I simply leave this here in case someone else comes across your answer. :) Nov 13, 2020 at 10:41 ## Java 8 lambda, 1098179 75 characters Because... you know... even Java can be golfed... a->{int b=(int)(.5+a*4./3),c=(int)(.5+b*2./3);return new int[]{a,b,c,++c};}  Lambda ungolfed into class: class C { static int[] a(int a) { int b = (int) (.5 + a * 4. / 3), c = (int) (.5 + b * 2. / 3); return new int[]{a, b, c, ++c}; } }  I assume I'm allowed to use longs as they are also an integer type. Sadly one needs to correctly round integers and thus a "short" cast doesn't work. By using longs we don't need to cast the rounding results back to ints. ### Update Using the nice little + 0.5 and casting afterwards trick we keep the correct rounding and save 2 chars! Also this trick doesn't require the use of long any longer thus we can switch back to ints shaving of 4 more chars. • Welcome to PPCG! May 24, 2016 at 11:50 • Thanks :) I followed the questions around here for a while now and I figured that this may be a question which I could participate in though the answer is longer than I expected it to be. May 24, 2016 at 11:53 • I'll "+1", hoping you'll add the mandatory "because... you know... Java!" May 24, 2016 at 11:59 • @Frozn I'm not sure if you can remove the a->{ and the final } for -5 bytes. May 24, 2016 at 12:13 • @OlivierDulac Not yet :) May 24, 2016 at 12:14 # MathGolf, 14 bytes 3/+_∞3/_)Γ2∩+i  Try it online. Explanation: 3/ # Divide the (implicit) input by 3 + # Add it to the (implicit) input _ # Duplicate this ∞ # Double the copy 3/ # Divide it by 3 _ # Duplicate this ) # Increase the copy by 1 Γ # Wrap four values into a list (since the stack only contains three, it # will use the implicit input as fourth): 2∩+ # Add 1/2 to each i # And cast each float to an integer, removing any decimal values # [int(i+1/2),int(i+i/3+1/2),int((i+i/3)*2/3+1/2),int((i+i/3)*2/3+1+1/2)] # (after which the entire stack joined together is output implicitly)  # Python 3, 52 bytes f=lambda n,i=4:1//i*[n+1]or[n]+f(round(i*n/3),i%3+1)  Try it online! Contrary to the previous Python answer, this one rounds in between cycles. Every next value can be calculated using i*n/3, where n is the current value and i is taken from [4,2,3]. To complete the sequence, 1 is added to the result of the final iteration. # Thunno, 18 chars D4*3/D2*3/D1+ZMrZv  Attempt This Online! (This is actually a bit less in bytes but the challenge asks for chars, not bytes.) This is a full program since Thunno doesn't have functions. #### Explanation D4*3/D2*3/D1+ZMrZv # Implicit input D # Duplicate 4*3/ # Push x*(4/3) D # Duplicate 2*3/ # Push y*(2/3) D # Duplicate 1+ # Push z+1 ZMr # Push the stack Zv # Round each number # Implicit output  # Mathematica - 21 bytes Just got Mathematica from my brothers RPi, so trying it out for fun, and what better way than a PPCG challenge. {#,4#/3,8#/9,8#/9+1}&  Defines an anonymous function. Try it out like: In[26]:= x:={#,4#/3,8#/9,8#/9+1}& In[27]:= x[9] Out[27]= {9, 12, 8, 9}  • Will this give integers as a result, or fractions? May 24, 2016 at 1:43 • @David integers, unless the results are non-integral, in which case fractions. May 24, 2016 at 2:05 ## Python 3, 45 bytes f=lambda i:map(round,[i,i*4/3,i*8/9,i*8/9+1])  See this code running on ideone.com ## Lua, 52 Bytes This program takes a number by command-line argument and return the corresponding array. Programs in lua are technically functions, as the interpreter will always encapsulate them in a function. This is also this mechanic that is used when you "call" codes in other files (it basically uses loadfile/dofile). m=math.floor x=...z=m(x*8/9)return{x,m(x*4/3),z,z+1}  ## Actually, 21 bytes ;;3@/+;32/*;uk1½+♂≈  This program declares a function that performs the required operations on the top stack value. Try it online! (the extra . at the end evaluates the function and prints the result) Explanation: ;;3@/+;32/*;uk1½+♂≈ ;; make two copies of x 3@/+ divide by 3, add that to x to get y ;32/* make a copy of y and multiply by 2/3 to get z ;u make a copy of z and add one k push stack as a list 1½+ add 0.5 to each element ♂≈ apply int() to each element (make integers from floats by flooring; this is equivalent to rounding half-up because of adding 0.5)  # Mathcad, [tbd] bytes Mathcad codegolf byte equivalance is yet to be determined. Taking a keyboard count as a rough equivalent, the solution is approx 40 bytes. ## Pyke, 11 bytes D}[}3/bD)[h  Try it here!  - implicit input() D - a,b = ^ } - b*=2 [}3/bD) - macro: } - tos*=2 3/ - tos/=3 b - tos = round(tos) D - old_tos = tos = tos - macro [ - macro h - d +=1  # C++0x - 95102185189109 129 chars int * n(int p){static int a[3];a[0]=p;a[1]=round(p+(p/3));a[2]=round((a[1]/3)*2);a[3]=a[2]+1;return a;}  • This requires the cmath header to work. ## Degolfed #include <cmath> int * ninePattern(int p) { static int a[3]; // pattern array a[0] = p; // sets first iteration a[1] = round(p + (p / 3)); // sets second iteration a[2] = round((a[1] / 3) * 2); // sets third iteration a[3] = a[2] + 1; // sets fourth iteration return a; // returns array }  • Not being a C++ expert but you could shorten it by reusing already calculated values which you have in the array. Also you might be able to remove some white space between ) and { don't know how strict C++ is. May 24, 2016 at 23:44 • Are those last two correct in the current version? Because I just saw you calculate p+(p/3)*(2/3) which is p+(2*p/9) instead of (p+(p/3))*(2/3) May 25, 2016 at 0:00 • Turns out it was an error on my part. I put - 1 instead of +1 for the last iteration :P May 31, 2016 at 0:51 # Arturo, 55 bytes $[x][map@[x,y:round x+x//3y,1+dup round 2*y//3]=>floor]


Try it

# JavaScript, 47 bytes

(a,r=Math.round)=>[a,a=r(a*4/3),a=r(a*2/3),++a]


Try it:

f=(a,r=Math.round)=>[a,a=r(a*4/3),a=r(a*2/3),++a]

console.log(JSON.stringify(f(9)));
console.log(JSON.stringify(f(8)));
console.log(JSON.stringify(f(6)));
console.log(JSON.stringify(f(23)));
console.log(JSON.stringify(f(159)));

# Javascript, 51 bytes

I convert my java solution to JavaScript and skimmed it down little bit.

var r=Math.round,g=a=>[a,a=r(a+a/3),a=r(a*2/3),++a]

• This isn't correct. For 23 this code will output [23, 31, 20, 21] instead of [23, 31, 21, 22] Jan 25, 2023 at 23:46
• @EzioMercer fixed Feb 26, 2023 at 10:47
• Your solutions is 51 bytes, not 52 :) Feb 26, 2023 at 13:14
• @EzioMercer thanks Mar 9, 2023 at 19:46

Erlang, 80 bytes

-module(f).
-export([f/1]).
f(X)->Y=X+(X/3),Z=trunc(Y*(2/3)),[X,trunc(Y),Z,Z+1].


To run, save as f.erl, compile and call the function. It will return a list of ints:

fxk8y@fxk8y:/home/fxk8y/Dokumente/erlang/pcg# erl
Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:4:4] [async-    threads:10] [kernel-poll:false]

Eshell V7.0  (abort with ^G)
1> c(f).
{ok,f}
2> f:f(9).
"\t\f\b\t"
3>


Note that Erlang automatically converts ints to ASCII chars if you are in the ASCII value range because Erlang doesn't have a char type. Calling the function with 100 gives you the better readable [100,133,88,89].

Ungolfed:

-module(f).
-export([f/1]).

f(X) ->
Y = X+(X/3),
Z = trunc(Y*(2/3)),
[X, trunc(Y), Z, Z+1].


# Erlang, 46 bytes

F=fun(X)->Z=round(X*4/9),[X,Z*3,Z*2,Z*2+1]end.


Also you can see the results with:

2> io:fwrite("~w~n", [F(9)]).
[9,12,8,9]
ok


# Pyth, 20 bytes

m.RdZ[Jc*4Q3Kc*2J3hK


Explanation:

            (Implicit print)
m           For each d in array, d =
.RdZ        Round d to zero decimal places
[           The array of
J         The result of setting J to
c       The float division of
*4Q   Input * 4
3     and 3
,
K         The result of setting K to
c       The float division of
*2J   J * 2
3     and 3
, and
hK        K + 1.
(Implicit end array)


Test here

# Jolf, 17 bytes

Ώ‘Hγ+H/H3Βώ/γ3hΒ’


Defines a function Ώ. It takes input implicitly, so it also doubles as a full program. Try it here!

• Jolf is becoming ESMin slowly confirmed??? :P Jun 20, 2016 at 4:12
• @Upgoat ey it's Greek. And midnight again. :P Jun 20, 2016 at 4:13
• >_> oh. Very sorry if I waked you up >_>_>_> Jun 20, 2016 at 4:13
• @Upgoat not asleep yet ;) Jun 20, 2016 at 4:14
• :| idk if it's good thing I did lent wake you up or bad thing you're still awake Jun 20, 2016 at 4:18

## Mathematica, 51 bytes

FoldList[#2@#&,{#,Round[4#/3]&,Round[2#/3]&,#+1&}]&


Anonymous function that conforms to the current (at time of posting) version of post, which implies rounding at every step.

FoldList is a typical operation in programming. It is invoked as FoldList[f, list] and applies the two-argument function f repeatedly to the result (or the first element of the list in the first iteration), taking the next element of the list as its second argument.

Ungolfed: #2 @ # & is an anonymous functions that applies its second argument to the first. Therefore, the list argument of FoldList consists of the successive functions to be applied to the input.

FoldList[#2 @ # &,
{#, (* note the absence of '&' here,
this '#' stands for the argument
of the complete function and is
covered by the & at the end      *)
Round[4 # / 3] &, (* anonymous function that rounds 4/3 of its input *)
Round[2 # / 3] &, (* ditto, 2/3 *)
# + 1 &           (* add one function *)
}] &               (* and the '&' makes the entire
thing an anonymous function,
whose argument is the '#' up
at the top.                  *)


Because input is integer and the divisions are by 3, there will never be a result like 4.5, therefore there's no need to worry about rules of rounding when the last digit is a 5: it will always be clearly closer to one integer, or another.

# Dyalog APL, 33 bytes

(⌽,{1+⊃⍵})∘{⍵,⍨3÷⍨2×⊃⍵}(3÷⍨4∘×),⊢


# Desmos, 37 bytes

a=9
b=a+\frac{a}{3}
c=\frac{2}{3}b
d=c+1


Try it online
Yay for unconventional languages!