9
\$\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\$
  • 1
    \$\begingroup\$ Must the output be an array, or are the numbers by themselves enough (like the Pyth answer)? \$\endgroup\$ – David May 24 '16 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\$ – trichoplax May 24 '16 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\$ – trichoplax May 24 '16 at 1:46
  • 3
    \$\begingroup\$ -1 for the arbitrary array and function requirements, which prevents languages without an array/list type or functions from competing. \$\endgroup\$ – Mego May 24 '16 at 4:15
  • 4
    \$\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\$ – Loovjo May 24 '16 at 5:21

29 Answers 29

11
\$\begingroup\$

MarioLANG, 659 621 591 582 556 543 516 458 418 401 352 308 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.

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Erik the Outgolfer May 24 '16 at 12:17
  • \$\begingroup\$ 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) \$\endgroup\$ – InitializeSahib May 27 '16 at 23:39
  • \$\begingroup\$ Done. while we are talking about test case you should add 10 to have a number that you round down on the fist operation. \$\endgroup\$ – Ether Frog May 28 '16 at 23:56
9
\$\begingroup\$

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
\$\endgroup\$
  • 3
    \$\begingroup\$ Yay for unconventional languages! :P \$\endgroup\$ – user48538 May 26 '16 at 19:19
4
\$\begingroup\$

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.

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

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
]
\$\endgroup\$
  • 1
    \$\begingroup\$ Tears of joy! It's gone so far! :D \$\endgroup\$ – Conor O'Brien May 24 '16 at 15:44
3
\$\begingroup\$

Java, 86 82 84 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)};}
\$\endgroup\$
  • \$\begingroup\$ I think I should learn Pyth. \$\endgroup\$ – dorukayhan wants Monica back May 24 '16 at 1:26
  • 5
    \$\begingroup\$ +1, because ya know, java \$\endgroup\$ – DJMcMayhem May 24 '16 at 1:29
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Loovjo May 24 '16 at 19:29
  • 1
    \$\begingroup\$ I just fixed the code \$\endgroup\$ – dorukayhan wants Monica back May 24 '16 at 21:09
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Frozn May 24 '16 at 22:36
3
\$\begingroup\$

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}

\$\endgroup\$
  • \$\begingroup\$ Does not have the rounding required by the Question. \$\endgroup\$ – Marv May 24 '16 at 14:47
  • 1
    \$\begingroup\$ 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] \$\endgroup\$ – Frozn May 24 '16 at 20:48
  • \$\begingroup\$ why downvote? i fixed it and it working correctly now? \$\endgroup\$ – user902383 May 25 '16 at 19:27
  • \$\begingroup\$ 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) \$\endgroup\$ – edc65 May 29 '16 at 20:25
  • \$\begingroup\$ @edc65 ok, done \$\endgroup\$ – user902383 May 29 '16 at 21:59
2
\$\begingroup\$

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]
\$\endgroup\$
  • 1
    \$\begingroup\$ +1. Cuz, ya know, Java \$\endgroup\$ – user48538 May 24 '16 at 15:04
  • \$\begingroup\$ 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] \$\endgroup\$ – Frozn May 24 '16 at 20:50
  • 3
    \$\begingroup\$ @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 \$\endgroup\$ – user902383 May 25 '16 at 8:31
2
\$\begingroup\$

Scratch, 33 bytes

Script
Asks for input, sets a to input rounded, sets b and c to their respective changes, then says all four numbers, seperated by commas.

\$\endgroup\$
2
\$\begingroup\$

Java 8 lambda, 109 81 79 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.

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Erik the Outgolfer May 24 '16 at 11:50
  • \$\begingroup\$ 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. \$\endgroup\$ – Frozn May 24 '16 at 11:53
  • \$\begingroup\$ I'll "+1", hoping you'll add the mandatory "because... you know... Java!" \$\endgroup\$ – Olivier Dulac May 24 '16 at 11:59
  • \$\begingroup\$ @Frozn I'm not sure if you can remove the a->{ and the final } for -5 bytes. \$\endgroup\$ – Erik the Outgolfer May 24 '16 at 12:13
  • \$\begingroup\$ @OlivierDulac Not yet :) \$\endgroup\$ – Erik the Outgolfer May 24 '16 at 12:14
1
\$\begingroup\$

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}
\$\endgroup\$
  • \$\begingroup\$ Will this give integers as a result, or fractions? \$\endgroup\$ – David May 24 '16 at 1:43
  • \$\begingroup\$ @David integers, unless the results are non-integral, in which case fractions. \$\endgroup\$ – Maltysen May 24 '16 at 2:05
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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}
\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
1
\$\begingroup\$

Mathcad, [tbd] bytes

enter image description here


Mathcad codegolf byte equivalance is yet to be determined. Taking a keyboard count as a rough equivalent, the solution is approx 40 bytes.

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 15 bytes

Code:

Ð3/+D·3/D>)1;+ï

Uses CP-1252 encoding. Try it online!.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Javascript, 50 bytes

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

var r=Math.round,g=a=>[a,r(a+a/3),a=r(a*8/9),++a]
\$\endgroup\$
1
\$\begingroup\$

C++0x - 95 102 185 189 109 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
}
\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Frozn May 24 '16 at 23:44
  • \$\begingroup\$ 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) \$\endgroup\$ – Frozn May 25 '16 at 0:00
  • \$\begingroup\$ Turns out it was an error on my part. I put - 1 instead of +1 for the last iteration :P \$\endgroup\$ – InitializeSahib May 31 '16 at 0:51
0
\$\begingroup\$

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].
\$\endgroup\$
0
\$\begingroup\$

Erlang, 46 bytes

Answer inspired by @fxk8y (I couldn't post a comment to his answer)

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
\$\endgroup\$
0
\$\begingroup\$

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

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

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!

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

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.

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

Dyalog APL, 33 bytes

(⌽,{1+⊃⍵})∘{⍵,⍨3÷⍨2×⊃⍵}(3÷⍨4∘×),⊢
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
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\$
  • \$\begingroup\$ You can knock off a byte by doing $a*4/3. \$\endgroup\$ – jstnthms Sep 8 '17 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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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