33
\$\begingroup\$

Here's a simple challenge, so hopefully lots of languages will be able to participate.

Given a positive integer \$n\$, output \$A076039(n)\$ from the OEIS.

That is, start with \$a(1)=1\$. Then for \$n>1\$:

$$a(n)=\left\{ \begin{array}{ll} n\cdot a(n-1), & \text{if } n>a(n-1) \\ \lfloor a(n-1)/n \rfloor, & \text{otherwise.}\end{array} \\ \right. $$

Test cases:

1 -> 1
2 -> 2 (2 > 1, so multiply)
3 -> 6 (3 > 2, so multiply)
4 -> 1 (4 < 6, so divide and take the integer part)
5 -> 5 
6 -> 30
17 -> 221
99 -> 12
314 -> 26

More test cases can be found on the OEIS page.

Per usual rules, you can input and output in a generally accepted manner: 1- or 0-based indexing, output an infinite sequence, output the first \$n\$ values, output only the \$n^\text{th}\$ value, and so forth, but specify that in your answer.

This is , so shortest code in bytes in each language wins!

\$\endgroup\$

52 Answers 52

1
2
4
\$\begingroup\$

Husk, 6 bytes

Ḟ§|*÷ṫ

Try it online!

How?

Ḟ§|*÷ṫ - function: integer, n
     ṫ - reversed range -> [n,n-1,...,2,1]
Ḟ      - right-fold using this f(a,b):
 §     -   fork:
  |    -     v logical-OR w
    ÷  -     v: b integer-divide a
   *   -     w: a multiplied by b
\$\endgroup\$
4
\$\begingroup\$

IBM/Lous Notes Formula Language, 92 bytes

i:=n:=1;o:=0;@While(i<=a;@Set("n";@If(i<n;@Integer(n/i);i*n));@Set("o";o:n);@Set("i";i+1));o

A bit lengthy and only posted to see if I can still remember how to use the language. Outputs the first 98 numbers of the sequence and has a leading 0 (not sure if that matters - please comment if so).

Edit Updated following feedback from @Giuseppe. Still the same byte count but now takes input from editable field a to define the length of the output. Theoretically the formula could calculate to infinity however Notes has a restriction of 64kb on what can be calculated in a field formula (see last screenshot).

There is no TIO for Formula so here are a couple of screenshot:

enter image description here

enter image description here

enter image description here

enter image description here

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Cheers, looks great to me now. Have my upvote! \$\endgroup\$
    – Giuseppe
    Commented Dec 1, 2020 at 17:29
3
\$\begingroup\$

Scala, 41 bytes

2.to(_)./:(1){(a,n)=>if(n>a)n*a else a/n}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

C (gcc), 47 bytes

-1 thanks to ceilingcat.

Displays x first sequence elements.

p,n;a(x){for(y(n=++p);n++<x;y(p=n>p?p*n:p/n));}

Try it online!

\$\endgroup\$
4
  • 3
    \$\begingroup\$ Is it really allowed to define y() outside the code section? \$\endgroup\$
    – Arnauld
    Commented Nov 25, 2020 at 17:28
  • \$\begingroup\$ @Arnauld it's a function I feed output to. I'm fairly sure that's an allowed output type. \$\endgroup\$ Commented Nov 25, 2020 at 20:05
  • 4
    \$\begingroup\$ I don't think so, otherwise all answers would never use print, here it's used twice. \$\endgroup\$ Commented Nov 25, 2020 at 20:39
  • \$\begingroup\$ @AZTECCO no C answers are interesting nowadays, especially not for such simple challenges. \$\endgroup\$ Commented Nov 26, 2020 at 14:33
3
\$\begingroup\$

Hy, 54 bytes

(defn f[n](or(+(= n 1))(//(f(- n 1))n)(*(f(- n 1))n)))

Try it online!

This function outputs the nth value of the sequence.

Ungolfed version:

(defn f [n]
  (or (+ (= n 1))
      (// (f (- n 1))
          n)
      (* (f (- n 1))
         n)))
\$\endgroup\$
3
\$\begingroup\$

convey, 47 bytes

Generates the sequence.

v0
+"
1">">v
 ).*vv
1"#"#%
"}">!`
^0=>#!
._<<,<

Try it online!

from n=2

The top left generates 1, 2, … and copies " them into )., * and %. The accumulator on the middle left starts with 1 and is also copied into the operators.

We have then i >= a, i * a and a % i. Based on the conditional, either i * a or i % a gets picked by taking ! it 0 or 1 times. For this we need to negate the boolean 0= for i * a and need a lot of crossings # to navigate everything into the right place.

When looping to the start, the accumulator passes floor _., and gets copied into the output "}, before going into the next round.

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

Japt, 12 bytes

Outputs the nth term, 1-indexed.

ȧY©Y*XªzY}g

Try it or run all test cases

ȧY©Y*XªzY}g     :Implicit input of integer U
È                :Function taking an integer X & iteration counter Y as input
 §Y              :  X<=Y?
   ©             :  Logical AND with
    Y*X          :  Y*X
       ª         :  Logical OR with
        zY       :  X floor divided by Y
          }      :End function
           g     :Run that function U times, starting with X=1 & Y=2
\$\endgroup\$
1
  • \$\begingroup\$ ÈzY ªY*X}g seems to work? \$\endgroup\$
    – Neil
    Commented Nov 25, 2020 at 20:05
2
\$\begingroup\$

Perl 5, 51 bytes

sub a{my$n=pop;$n<=1?1:$n>($a=a($n-1))?$a*$n:$a/$n}

Try it online!

sub a {
  my $n=pop;                 # n = input
  $n <= 1           ? 1      # n = 1      → 1
 :$n > ($a=a($n-1)) ? $a*$n  # n > a(n-1) → a(n-1) · n
                    : $a/$n  # n ≤ a(n-1) → ⌊a(n-1) / n⌋ in 'use integer' mode
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 50 \$\endgroup\$
    – 640KB
    Commented Nov 25, 2020 at 20:38
  • \$\begingroup\$ @640KB – yes, nice. \$\endgroup\$
    – Kjetil S
    Commented Nov 25, 2020 at 20:54
2
\$\begingroup\$

Charcoal, 17 bytes

F…·²N≔∨∕ψι×ψιψILψ

Try it online! Link is to verbose version of code. Explanation: The string y (ψ) is initialised to a single null byte.

F…·²N

Loop from 2 to n.

≔∨∕ψι×ψιψ

Try dividing the string into i pieces, but if that would result in the empty string, repeat the string i times intead.

ILψ

Output the final length of the string.

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

Julia 1.0, 40 bytes

a(n)=n<1 ? 1 : n>(A=a(n-1)) ? A*n : A÷n

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ a(n)=n<1||(n>(A=a(n-1)) ? A*n : A÷n) for -3 bytes \$\endgroup\$
    – MarcMush
    Commented Nov 26, 2020 at 12:30
  • \$\begingroup\$ thx, didnt know bools are turned into 0 or 1 when used in arithmetics \$\endgroup\$
    – Kjetil S
    Commented Nov 27, 2020 at 13:32
  • \$\begingroup\$ if fact 1 == true and 0 == false. Bool <: Integer <: Number so they can be added \$\endgroup\$
    – MarcMush
    Commented Dec 4, 2020 at 15:17
2
\$\begingroup\$

PHP, 41 bytes

<?for(;;)echo$x=!$x+$x/++$i|0?:$x*$i,'
';

Try it online!

Outputs the infinite sequence. A couple of PHP tricks in use, using |0 to cast to int, and !$x to elegantly handle the first iteration.

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

Stax, 9 bytes

ü↨╖Γ¥B╢╤┴

Run and debug it

The same reduce operation as the Jelly answer.

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

Wolfram Language (Mathematica), 36 33 bytes

Nest[⌊#/++n⌋/. 0->n#&,n=1,#]&

Try it online!

0-indexed.

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

Pyth, 20 bytes

J1V}2hQJ=J?>NJ*NJ/JN

Try it online!

1-indexed, outputs the first n values.

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

PowerShell Core, 65 bytes

Output the nth value:

$r=1
1..$args[0]|%{$r=([Math]::Floor($r/$_),($r*$_))[$_-gt$r]}
$r

Explanations

$r=1                           # Initialises the result as 1
1..$args[0]|%{                 # For 1 to the argument
    $r=(                       # In an array calculate the two possible values
        [Math]::Floor($r/$_),  # Divide and floor
        ($r*$_)                # Multiply
    )[$_-gt$r]                 # The condition to decide what index to take
}                              # $true becomes 1 and $false 0
$r                             # Returns the result

Does anyone know how to make the [Math]::Floor shorter?

Try it online!

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

MMIX, 132 bytes (17 instrs)

Given \$n\$, returns \$A076039(n)\$. Given \$0\$, returns \$A076039(2^{64})\$ (or, more accurately, runs until the machine breaks, but in theory it would return that).

C1010000 E3000001 E3FF0001 E3020000
4A020007 320300FF 44030005 1A0000FF
FE020003 F8000004 F6010002 E3020000
1E0000FF 23FFFF01 27010101 5B01FFF6
F8010000

Disassembly:

foo SET  $1,$0          // move n to $1
    SETL $0,1           // acc = 1
    SETL $255,1         // i = 1
    SETL $2,0           // top = 0 (comes in handy later)
0H  BNZ  $2,1F          // loop: if(top) goto divide
    CMPU $3,$0,$255
    BP   $3,1F          // if(acc > i) goto divide
    MULU $0,$0,$255     // rH:acc = acc * i
    GET  $2,rH          // top = rH
    JMP  2F             // goto cont
1H  PUT  rD,$2          // rD = top
    SETL $2,0           // top = 0
    DIVU $0,$0,$255     // divide: acc:rR = rD:acc /% i
2H  ADDU $255,$255,1    // i++
    SUBU $1,$1,1        // n--
    PBNZ $1,0B          // ifprob(n) goto loop
    POP  1,0            // return(acc)

Note that this returns an incorrect result if the correct result is at least \$2^{64}\$. However, the computation is otherwise carried out in terms of 128-bit arithmetic, with the accumulator stored as $2:$0 (this would break down if two divisions were to occur in a row, but that can't happen).

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

Vyxal, 9 bytes

ɾ‡<[*|ḭ]R

Try it Online!

Port of Jelly answer. That if statement takes up so much bytes tho...

How?

ɾ‡<[*|ḭ]R
ɾ          # Inclusive one range of (implicit) input
        R  # Reduce by:
 ‡         # Next two elements as a lambda
  <        # Is a<b?
   [*      # If so, multiply: a*b
     |ḭ]   # Else, floor divide: a//b
\$\endgroup\$
2
\$\begingroup\$

Nibbles, 5.5 bytes (11 nibbles)

=\,$?/@$$*@

Outputs the first n values.

=\              # scan from left across
  ,$            # 1..input
                # with function:
    ?           #   if
     /@$        #   right integer-divided by left
                #   is nonzero:
        $       #     return it
                #   otherwise:
         *@     #     return right multiplied by
                #     (implicitly) left     

enter image description here

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

Bash, 40 bytes

outputs an infinite sequence

for((r=1;;));{ echo $[r=++i>r?r*i:r/i];}

Try it online!

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

Perl 5, 40 + 2 (-pl flag) = 42 bytes

$==1;map$==$_>$=?$=*$_:$=/$_,2..$_;$_=$=

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think you can just count the language as "Perl 5 -pl" for 40 bytes, like this answer. Relevant meta post. \$\endgroup\$
    – Giuseppe
    Commented Nov 25, 2020 at 21:34
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 71 bytes

	Y =1
R	X =X + 1
	Y =Y * X GT(X,Y)	:S(O)
	Y =Y / X
O	OUTPUT =Y	:(R)
END

Try it online!

Prints the sequence forever.

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

Crystal, 41 bytes (-2 bytes thanks to Giuseppe)

def a(n)n<2?1: n>(f=a(n-1))?n*f: f//n end

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I think you have an extra space between n*f and the second :. Also, n<2 should work instead of n==1, so this can be 41 bytes. \$\endgroup\$
    – Giuseppe
    Commented Dec 1, 2020 at 17:32
  • \$\begingroup\$ Oh, my bad! I was having issues with removing the space after the second :, so I assumed it went both ways. Thanks for the fix! \$\endgroup\$
    – sugarfi
    Commented Dec 1, 2020 at 18:32
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.