nで乗算または除算

Nov 26 2020

これは簡単な課題なので、多くの言語が参加できることを願っています。

正の整数が与えられた\$n\$、出力\$A076039(n)\$OEISから。

つまり、\で始まります$a(1)=1\$。次に\$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. $$

テストケース:

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

その他のテストケースは、OEISページにあります。

通常のシーケンスルールに従って、一般的に受け入れられている方法で入力および出力できます。1ベースまたは0ベースのインデックス付け、無限シーケンスの出力、最初の\の出力$n\$値、出力のみ\$n^\text{th}\$ 値などですが、答えにそれを指定してください。

これはコードゴルフなので、各言語のバイト単位の最短コードが優先されます。

回答

15 JonathanAllan Nov 26 2020 at 01:35

ゼリー、6バイト

R×:<?/

正の整数を受け入れるモナドリンク\$n\$、正の整数、\を生成します$a(n)\$

オンラインでお試しください!または、テストスイートを参照してください。

どうやって?

R×:<?/ - Link:
R      - range -> [1..n]
     / - reduce by (i.e. evaluate f(f(...f(f(f(1,2),3),4),...),n) with this f(a,b):
    ?  -   if...
   <   -   ...condition: (a) less than (b)?
 ×     -   ...then: multiply -> a×b
  :    -   ...else: integer divide -> a//b

\までのシーケンスを出力します$a(n)\$ と:

R×:<?\
14 Lyxal Nov 26 2020 at 06:16

スクラッチ3.0、29個の27ブロック/ 234 167バイト

SB構文として:

define f(n)
if<(n)=(1)>then
add(1)to[v v
else
f((n)-(1
set[d v]to(item(length of[v v])of[v v
if<(n)>(d)>then
add((n)*(d))to[v v
else
add([floor v] of ((n)/(d)))to[v v]
end
end
when gf clicked
delete all of [v v
ask()and wait
f(answer)

スクラッチで試してみてください

入出力の方法が少しわからないので、安全だと思ってヘルパー関数付きの完全なプログラムにしました。

これに答えることで、私のアカウントを「新規」から「標準」に昇格させることができたので、それはいつも楽しいことです。

-@ attのおかげで67バイト

13 RobinRyder Nov 26 2020 at 05:21

Shakespeareプログラミング言語、221バイト

,.Ajax,.Puck,.
Act I:.Scene I:.[Enter Ajax and Puck]
Ajax:You cat.
Scene V:.
Puck:You is the sum ofYou a cat.
Ajax:Open heart.Is I nicer you?If notYou is the quotient betweenyou I.
      If soYou is the product ofyou I.Let usScene V.

オンラインでお試しください!

無限リストを出力します。ただし、出力値の間に区切り文字がないため、出力が多少読みにくいことに注意してください。

セパレーター(ヌルバイト)を追加するための私の最善の試みは、次のようになります。

Shakespeareプログラミング言語、297バイト

,.Ajax,.Puck,.Page,.
Act I:.Scene I:.
[Enter Ajax and Puck]
Ajax:You cat.
Scene V:.[Exit Puck][Enter Page]
Ajax:Speak thy.
Page:You is the sum ofYou a cat.
Scene X:.[Exit Page][Enter Puck]
Ajax:Open heart.Is I nicer you?If notYou is the quotient betweenyou I.
      If soYou is the product ofyou I.Let usScene V.

オンラインでお試しください!

11 Noodle9 Nov 26 2020 at 01:51

パイソン2、47の 43 39バイト

xnorのおかげで4バイト節約できました!!! Neilの
おかげで4バイト節約できました!!!

r=i=1
while 1:r=r/i or r*i;print r;i+=1

オンラインでお試しください!

プリント\$\{a(n)\mid n \in \mathbb{N}\}\$ 無限のシーケンスとして。

10 RobinRyder Nov 26 2020 at 00:55

R、43の39バイト

-ジュゼッペのおかげで4バイト。

for(i in 1:scan())T=T%/%i^(2*(i<T)-1);T

オンラインでお試しください!

\を出力します$n\$第3項、1-インデックス付き。

\でシーケンスを初期化する$a(0)=1\$数式が\を与えるので、また機能します$a(1)=1\$望んだ通りに。変数Tは整数1に強制変換され、式のよりコンパクトなバージョンを繰り返し適用します。

$$a(n)=\left\lfloor \frac{a(n-1)}{n^{2\mathbb{I_{n<a(n-1)}} -1}}\right\rfloor $$

\付き$\mathbb I\$インジケーター機能)。これは、元の定義の両方の場合をカバーします。

8 user Nov 26 2020 at 00:20

APL(Dyalog Unicode)、18バイト(SBCS)

{⍺>⍵:⍺×⍵⋄⌊⍵÷⍺}/⌽ö⍳

オンラインでお試しください!

シーケンスのn番目の要素を出力する、ほとんどゴルフではないが安全な関数。


APL(Dyalog Unicode)、15 14バイト(SBCS)

@ Adámのおかげで1バイト節約できました

(⌊⊢×⊣*∘×-)/⌽ö⍳

オンラインでお試しください!

シーケンスのn番目の要素を出力します。\の場合、これは機能しないことに気づきました。$n = a(n-1)\$nを\の累乗にするため$n - a(n-1)\$そしてそれを\で乗算します$a\$、私が知る限り、この関数は少なくともn = 2,000,000まで機能します。

(⌊⊢×⊣*∘×-)/⌽ö⍳
              ⍳  ⍝ Make a range to n
           ⌽ö   ⍝ Then reverse it and
(⌊⊢×⊣*∘×-)/      ⍝ reduce it with a train:
   ×             ⍝ Multiply
  ⊢             ⍝ a(n-1) with
    ⊣           ⍝ n
     *∘×        ⍝ to the power of the sign of
        -       ⍝ n - a(n-1)
⌊                ⍝ Floor it
8 AZTECCO Nov 26 2020 at 06:11

Haskell、40バイト

a#n|n>a=a*n|1>0=a`div`n
a=scanl1(#)[1..]

オンラインでお試しください!

  • 無限シーケンスを出力します。

中置演算子#は次の項を計算します。これを使用してすべての正の整数[1 ..]を折りますが、代わりにscanl1を使用すると、すべてのステップが得られます。

7 DominicvanEssen Nov 26 2020 at 03:00

R、41バイト

for(m in 1:scan())T=`if`(m>T,T*m,T%/%m);T

オンラインでお試しください!

これを試してみる前に、ロビンライダーのRの答えを見ないように強制しました。幸いなことに、私たちはお互いに異なるアプローチを考え出しましたが、どちらも(これまでのところ)バイト単位でまったく同じ長さであるように見えますが、残念ながら彼のアプローチは2バイト短くなっています...

7 ErikF Nov 26 2020 at 08:42

C(gcc)、35バイト

1ベースの開始インデックスを取得し、n番目のシーケンス値を返します。

f(i,j){i=i?i>(j=f(i-1))?j*i:j/i:1;}

オンラインでお試しください!

7 Razetime Nov 26 2020 at 10:53

Forth(gforth)、82バイト

: f 2dup 2dup > if * else swap / then dup . swap drop swap 1+ swap recurse ;
1 1 f

オンラインでお試しください!

スペースで区切られた無限のシーケンスを出力します。

7 NahuelFouilleul Nov 26 2020 at 00:02

Perlの5 -Minteger -06136、27のバイト

-@ Abigailと@Sisyphusのおかげで9バイト。

無限のシーケンスを出力します

say$/while$/=$//++$i||$/*$i

オンラインでお試しください!

7 JonathanAllan Nov 26 2020 at 02:15

パイソン3.8+、 45の 39バイト

-2 xnorのおかげで(while print(...)!=0:while[print(...)]:
-4 Neilのおかげで([a*n,a//n][a>n]a//n or a*n

a=n=1
while[print(a:=a//n or a*n)]:n+=1

\を出力する完全なプログラム$a(n)\$ すべての自然数に対して。

オンラインでお試しください!


再帰関数として、49:

f=lambda v,n=1,a=1:a*(v<n)or f(v,n+1,a//n or a*n)
6 Arnauld Nov 26 2020 at 00:12

JavaScriptの(Node.jsの)、 38の 35バイト

@Neilのおかげで3バイト節約できました

\を返します$n\$-第1項、1-インデックス付き。

f=(n,k=i=1n)=>i++<n?f(n,k/i||k*i):k

オンラインでお試しください!

6 Bubbler Nov 26 2020 at 13:49

因数分解、45バイト

[ [1,b] 1 [ 2dup < [ * ] [ /i ] if ] reduce ]

オンラインでお試しください!

簡単な削減。1ベースのインデックスを取得し、n番目の項を返します。

[                         ! anonymous lambda
  [1,b] 1 [ ... ] reduce  ! reduce {1..n} by the following, starting with 1:
    2dup <                !   ( an n -- an n an<n)
    [ * ] [ /i ] if       !   ( a_n+1 ) multiply if an < n, int-divide otherwise
]
5 DominicvanEssen Nov 26 2020 at 03:33

ハスク、11バイト

Fμ?*`÷<¹³)ḣ

オンラインでお試しください!

F               # Fold a function over
          ḣ     # sequence from 1..input;
 μ?*`÷<¹³)      # function with 2 arguments:
  ?             # if
      <¹³       # arg 2 is smaller than arg 1
   *            # arg 1 times arg 2
    `÷          # else arg 1 integer divided by arg 2
5 Xcali Nov 26 2020 at 04:51

Perl 5、35 -Minteger -pバイト

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

オンラインでお試しください!

かかるn入力として、および印刷nthリスト内の項目を。

5 ovs Nov 26 2020 at 04:58

05AB1E、12の10バイト

無限シーケンスを出力します。

λN>₁N›i÷ë*

オンラインでお試しください!

コメント

λ              # infinite list generation
               # implicitly push a(n-1) (initially 1)
 N>            # push n, since N is 0-indexed, this needs to be incremented
   ₁N›         # is a(n-1) > n-1?
      i÷       # if this is true, integer divide a(n-1) by n
        ë*     # else multiply a(n-1) and n
5 coltim Nov 26 2020 at 03:27

K(OK) 、22の20バイト

{_x*(1%y;y)y>x}/1+!:

オンラインでお試しください!

を使用するの$[y>x;y;1%y]ではなく(1%y;y)、ブール条件y>xを使用してリストにインデックスを付け、数バイトを節約します。

5 reffu Nov 27 2020 at 22:44

Forth(gforth)、51バイト

: f 1+ 1 tuck ?do i 2dup <= if * else / then loop ;

オンラインでお試しください!

コードの説明

: f        \ start word definition
  1+       \ add 1 to n
  1 tuck   \ set up accumulator and loop parameters
  ?do      \ loop from 1 to n (if n > 1)
    i 2dup \ set up top two stack values and duplicate 
    <= if  \ if a(n-1) <= n
      *    \ multiply
    else   \ otherwise
      /    \ divide
    then   \ end if
  loop     \ end loop
;          \ end word definition
5 DMiddendorf Nov 30 2020 at 22:45

Java(JDK)、52バイト

n->{int i,a=i=1;for(;i++<n;)a=i>a?i*a:a/i;return a;}

オンラインでお試しください!

注:-1バイトの@RedwolfProgramsと-10(?)バイトの@userに感謝します。

4 cairdcoinheringaahing Nov 26 2020 at 00:39

ゼリー、11バイト

1’ß×:>@?$Ị?

オンラインでお試しください!

使い方

1’ß×:>@?$Ị? - Main link f(n). Takes n on the left
          ? - If statement:
         Ị  -   If: n ≤ 1
1           -   Then: Yield 1
        $   -   Else:
 ’          -     n-1
  ß         -     f(n-1)
       ?    -     If statement:
     >@     -       If: n > f(n-1)
   ×        -       Then: n × f(n-1)
    :       -       Else: n : f(n-1)
4 UnrelatedString Nov 26 2020 at 01:56

Brachylog、10バイト

⟦₁{÷ℕ₁|×}ˡ

オンラインでお試しください!

n = 1の[1]代わりにシングルトンリストを1提供しますが、それ以外の場合は通常とは異なりません。

         ˡ    Reduce
⟦₁            1 .. n
  {     }     by:
   ÷          integer division
    ℕ₁        if the result is 1 or greater,
      |×      multiplication if not.
4 Giuseppe Nov 26 2020 at 02:07

ガイア、9バイト

┅⟪<₌×/?⟫⊢

オンラインでお試しください!

基本的に短いゼリーの答えと同じです。プリント、1はインデックスa(n)が、と交換することができる第一取得するn代わりに、要素。

		# implicit input n
┅		# push 1...n
 ⟪      ⟫⊢	# reduce the list by the following function:
  <₌		# push an extra copy of a(i-1) and i and check if less than?
    × ?		# if true, then multiply
     /		# else integer divide
		# implicitly print top of stack
4 Neil Nov 26 2020 at 03:00

網膜、58バイト

K`_ _
"$+"+L$`(^_+|_)(?<=(\1)+) (\1)+
_$`$1 $#3*$#2*
r`_\G

オンラインでお試しください!スクリプトが履歴を使用する方法のため、テストスイートはありません。説明:

K`_ _

入力を1のペア(単項)に置き換えます。1つ目はループインデックスで、2つ目は出力です。

"$+"+

ループn時間。

L$`(^_+|_)(?<=(\1)+) (\1)+

出力とループインデックスの両方をループインデックスで除算するか、除算がゼロの場合は1で除算します。

_$`$1 $#3*$#2*

ループインデックスをインクリメントし、2つの商を乗算します。これにより、output/index*index/indexまたはoutput/1*index/1それぞれになります。

r`_\G

最終出力を10進数に変換します。

4 640KB Nov 26 2020 at 03:19

PHP、57バイト

function a($n){return$n?($n>$x=a($n-1))?$x*$n:$x/$n|0:1;}

オンラインでお試しください!

4 Stephen Nov 26 2020 at 03:32

cQuents、14バイト

=1:$>Z?$Z:Z_/$

オンラインでお試しください!

説明

=1             first term is 1
  :            mode sequence: given n, output nth term; otherwise, output indefinitely
               each term equals:

   $>Z?  :     if n > seq(n - 1)                else
       $Z n * seq(n - 1) Z_/$                                       seq(n - 1) // n
4 GalenIvanov Nov 26 2020 at 15:19

ラケット、66バイト

(λ(n)(foldl(λ(x y)((if(< y x)* quotient)y x))1(range 1(+ 1 n))))

オンラインでお試しください!

4 J42161217 Nov 26 2020 at 03:40

Wolfram言語(Mathematica)、40バイト

a@1=1;a@n_:=If[#<n,n#,⌊#/n⌋]&@a[n-1]

オンラインでお試しください!

-@ attから2バイト

4 GalenIvanov Nov 26 2020 at 18:39

J、21バイト

[:(]<.@*[^*@-)/1+i.@-

オンラインでお試しください!

A J @userのポートのAPLソリューションは、 -それをupvoteすることを忘れないでください!

4 KevinCruijssen Nov 27 2020 at 15:51

MathGolf、11の9バイト

1k{î`<¿*/

- @ ovsのおかげで2バイト。

\を出力します$n^{th}\$ 値。

オンラインでお試しください。

説明:

1         # Push 1
 k{       # Loop the input amount of times:
   î      #  Push the 1-based loop index
    `     #  Duplicate the top two items
     <¿   #  If the current value is smaller than the 1-based loop index: a(n-1)<n:
       *  #   Multiply the value by the 1-based loop index
          #  Else:
       /  #   Integer-divide instead
          # (after the loop, the entire stack joined together is output implicitly)