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

1

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

I 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.557

3-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.270

1Is 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.500

1What 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

11

# 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! – Erik the Outgolfer – 2016-05-24T12:17:16.863

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) – InitializeSahib – 2016-05-27T23:39:50.287

Done. while we are talking about test case you should add 10 to have a number that you round down on the fist operation. – Ether Frog – 2016-05-28T23:56:37.913

9

# 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

3Yay for unconventional languages! :P – user48538 – 2016-05-26T19:19:17.603

4

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

3The spec is to return an array, not the final result only. – Value Ink – 2016-05-24T00:27:47.203

3Also lol at those flagging for deletion in <2 minutes :D – David – 2016-05-24T00:36:49.853

@David D: I don't think I can retract deletion votes – Downgoat – 2016-05-24T00:39:07.677

@David upvoted your answer as my condolences :) – InitializeSahib – 2016-05-24T00:48:05.277

Haha I don't really mind, just found it amusing – David – 2016-05-24T00:51:56.320

1I'm pretty sure this isn't a function either, correct me if I'm wrong. – Maltysen – 2016-05-24T01:03:43.943

@Maltysen Maybe you're right, but I can't test it rn. Maybe the OP will give us some info? – InitializeSahib – 2016-05-24T01:05:19.613

@David it's fine then, and your program seems to pass all my checks – InitializeSahib – 2016-05-24T01:06:48.337

4

# 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

# PHP, 67 bytes

function f($x){return [$x,$y=round($x*4/3),$z=round($y*2/3),\$z+1];}

0

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

0

# 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

0

# 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

0

# 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 – Downgoat – 2016-06-20T04:12:11.997

@Upgoat ey it's Greek. And midnight again. :P – Conor O'Brien – 2016-06-20T04:13:08.923

ockquote>

> oh. Very sorry if I waked you up >>>>

– Downgoat – 2016-06-20T04:13:49.613

@Upgoat not asleep yet ;) – Conor O'Brien – 2016-06-20T04:14:22.943

:| idk if it's good thing I did lent wake you up or bad thing you're still awake – Downgoat – 2016-06-20T04:18:29.767

0

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

0

# Dyalog APL, 33 bytes

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

0

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

0

# CJam, 21 bytes

qi__3d/+mo_2d3/*i_)]

Feedback is welcome

# Explanation

• qi__ - Read the input as an integer and duplicate it twice
• 3D/+mo - Divide one instance of the input by 3, then add it to the second instance to make y
• _2d3/*i - Duplicate y, then multiply it by .6
• _)] - Dupe, increment, wrap in array, print as array (not in code because of the ` operator :( )

Edit: Forgot to make the first three a double, so the program was broken. Fixed.