11
\$\begingroup\$

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.

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

35 Answers 35

1
2
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Axiom, 59 bytes

g(x)==round(x)::INT;f(x)==[x,a:=g(x+x/3.),b:=g(a*2./3),b+1]

test

(3) -> [[i,f(i)] for i in [9,8,6,23,159]]
   (3)
   [[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]]]
                                                      Type: List List Any
\$\endgroup\$
0
\$\begingroup\$

PHP, 60 Bytes

print_r([$a=$argn,$b=round($a+$a/3),$c=round($b*2/3),$c+1]);

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can knock off a byte by doing $a*4/3. \$\endgroup\$
    – halfmang
    Commented Sep 8, 2017 at 22:40
0
\$\begingroup\$

PHP, 67 bytes

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

Zsh*, 58 bytes

r()<<<${$(($1+.5))%.*}
r $1;r $[_+_/3];r $[2*_/3];r $[_+1]

Try it online!

(*) Using force_float and extendedglob options.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.