Dodatkowe fakty!


17

W matematyce czynnikowy, skrócony „fakt” nieujemnej liczby całkowitej n , oznaczony przez n! , jest iloczynem wszystkich liczb całkowitych dodatnich mniejszych lub równych n . Na przykład 5! jest 1 * 2 * 3 * 4 * 5 = 120

Silnia 0 wynosi 1 , zgodnie z konwencją dla pustego produktu.


To są zwykłe fakty, do których jesteśmy przyzwyczajeni. Dodajmy kilka alternatyw:

  1. Silnia (zdefiniowana powyżej)
  2. Podwójna silnia: n !! = 1 + 2 + ... + n
  3. Potrójne silnia: n !!! = 1 - (2 - (3 - (... - n))) ...)
  4. Czterokrotna silnia: n !!!! = 1 / (2 / (3 ... / n))) ...) . Uwaga: Jest to podział zmiennoprzecinkowy, a nie podział całkowity.

Wyzwanie

Weź nieujemną liczbę całkowitą wejściową n , bezpośrednio po niej od 1 do 4 wykrzykników. Dane wejściowe będą wyglądały (dokładnie) w ten sposób: 0! , 5 !! , 132 !!! lub 4 !!!! . Niestety, w tym wyzwaniu nie możesz założyć elastycznego formatu wejściowego.

Wynik

Wynik powinien być wynikiem w dowolnym dogodnym formacie. Wynik poczwórnej silni musi mieć co najmniej 2 cyfry po przecinku, z wyjątkiem 0 !!!! = 0 .

Przypadki testowe:

0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
---
0!! = 0
1!! = 1
2!! = 3
3!! = 6
4!! = 10
5!! = 15
6!! = 21
7!! = 28
8!! = 36
9!! = 45
10!! = 55
---
0!!! = 0
1!!! = 1
2!!! = -1
3!!! = 2
4!!! = -2
5!!! = 3
6!!! = -3
7!!! = 4
8!!! = -4
9!!! = 5
10!!! = -5
---
0!!!! = 0
1!!!! = 1
2!!!! = 0.5
3!!!! = 1.5
4!!!! = 0.375
5!!!! = 1.875
6!!!! = 0.3125
7!!!! = 2.1875
8!!!! = 0.27344
9!!!! = 2.4609
10!!!! = 0.24609

Najkrótsze rozwiązanie w każdym języku wygrywa.


2
Czy czteroosobowy czynnik może być również racjonalnym podziałem?
Martin Ender

6
Definicja „podwójnej silni” pasuje…
Erik the Outgolfer

4
@Erik, to alternatywny podwójny fakt ;-)
Stewie Griffin

1
@StewieGriffin BTW to trochę podstępne, że 0!-> 1.
Erik the Outgolfer

5
Tytuł powinien być alternatywnym faktem
cyfrowa trauma

Odpowiedzi:


7

JavaScript (ES6), 88 bajtów

s=>eval(([a,b]=s.split(/\b/),g=k=>+a?k-a?k+'_*+-/'[b.length]+`(${g(k+1)})`:k:+!b[1])(1))

Przypadki testowe

Sformatowane i skomentowane

s =>                                // given the input string s,
  eval(                             // evaluate as JS code:
    ( [a, b] = s.split(/\b/),       //   a = integer (as string) / b = '!' string
      g = k =>                      //   g = recursive function taking k as input
        +a ?                        //     if a is not zero:
          k - a ?                   //       if k is not equal to a:
            k + '_*+-/'[b.length] + //         append k and the operation symbol
            `(${g(k + 1)})`         //         append the result of a recursive call
          :                         //       else:
            k                       //         just append k and stop recursion
        :                           //     else:
          +!b[1]                    //       return 1 for multiplication / 0 otherwise
    )(1)                            //   initial call to g() with k = 1
  )                                 // end of eval()

7

Łuska , 15 bajtów

§!ëΠΣF-F/#'!oṫi

Wypróbuj online!

Wyjaśnienie

Indeksowanie do listy funkcji: radości z używania funkcjonalnego języka.

§!ëΠΣF-F/#'!oṫi  Implicit input, say x = "6!!!"
              i  Convert to integer (parses the leading sequence of digits): 6
            oṫ   Descending range to 1: y = [6,5,4,3,2,1]
  ë              Four-element list containing the functions
   Π             product,
    Σ            sum,
     F-          left fold with subtraction (gives 0 for empty list), and
       F/        left fold with division (gives 0 for empty list).
 !               1-based index into this list with
         #'!     count of !-characters in input: gives F-
§                Apply to y and print implicitly: -3

Używam zakresu malejącego i lewych pasów, ponieważ -i /biorę ich argumenty w odwrotnej kolejności w Husk.


Indexing into a list of functionsjest woah ...
Erik the Outgolfer

Myślałem o Haskell, a potem to widzę ... Naprawdę wydaje się być właściwym narzędziem do tego zadania. +1
alleks

Do tego został stworzony Husk: D
Leo

6

C # (.NET Core) , 134 130 128 bajtów

s=>{double e=s.Split('!').Length,n=int.Parse(s.Trim('!')),i=n,r=n;for(;--i>0;)r=e>4?i/r:e>3?i-r:e>2?i+r:i*r;return n<1&e<3?1:r;}

Wypróbuj online!

Najlepszą częścią gry w golfa kodowego są rzeczy, których uczysz się podczas rozwiązywania problemów. W tym nauczyłem się, że w C # możesz przycinać inne znaki oprócz białych znaków z ciągów.

  • 4 bajty zapisane dzięki LiefdeWen!
  • 2 bajty zapisane, ponieważ nie muszę odejmować 1 do s.Split('!').Length, po prostu napraw ograniczenia w e>4?i/r:e>3?i-r:e>2?i+r:i*ri n<1&e<3?1:r.

1
Możesz zrobić, e na itakże doubleuniknąć deklarowania, aby r zapisał 4 bajty.
LiefdeWen,

1
@LiefdeWen Lub, floataby zapisać kolejny bajt.
Kevin Cruijssen


4

R , 113 111 bajtów

function(s){z=strtoi((n=strsplit(s,'!')[[1]])[1])
n=length(n)
`if`(z,Reduce(c('*','+','-','/')[n],1:z,,T),n<2)}

Wypróbuj kilka przypadków testowych!

bez golfa:

function(s){
  n <- strsplit(s,"!")[[1]]          # split on "!"
  z <- strtoi(n[1])                  # turn to integer
  n <- length(n)                     # count number of "!"
  FUN <- c(`*`,`+`,`-`,`/`)[[n]]     # select a function
  right <- TRUE                      # Reduce (fold) from the right
  if( z > 0)                         # if z > 0
    Reduce(FUN, 1:z,,right)          # return the value
  else    
    (n < 2)                          # 1 if n = 1, 0 if n > 1
}

el(strsplit(s,"!")) zapisuje 1 bajt
bouncyball

4

Python3, 124 130 121 119 bytes

At this point, I believe that recursion is the key to further byte saving.

s=input()
l=s.count('!')
v=int(s[:-l])+1
print(eval((" *+-/"[l]+"(").join(map(str,range(1,v)))+")"*(v-2)or"0")+(l<2>v))

Try the testcases on Try it online!

-9 bytes thanks to @Mr.Xcoder!

-2 bytes thanks to @Felipe Nardi Batista!


Fails for 6!. It should be 720.
Mr. Xcoder

I updated the Tio test suite.
Mr. Xcoder


Oh yeah, sure, didn't spot that
Mr. Xcoder


3

Pyth, 34 30 bytes

+uv++H@"/*+-"/Q\!G_tUK.vQKq"0!

Try it online!

Explanation

+uv++H@"/*+-"/Q\!G_tUK.vQKq"0!"Q    Implicit: append "Q
                                    Implicit: read input to Q
                      .vQ           Evaluate Q as Pyth code. This evaluates the integer,
                                    any !'s are parsed as unary NOT for the next expression
                                    and discarded.
                     K              Save the result to K.
                    U               Get a list [0, 1, ..., K-1].
                   t                Drop the first item to get [1, 2, ..., K-1].
                  _                 Reverse to get [K-1, K-2, ..., 1].
 u                       K          Starting from G = K, for H in [K-1, K-2, ..., 1] do:
             /Q\!                     Count the !'s in Q.
      @"/*+-"                         Get the correct operator.
    +H                                Prepend the current H.
   +             G                    Append the previous value G.
  v                                   Evaluate as Python code.
                          q"0!"Q    See if Q == "0!".
+                                   If so, add 1.

Using .U saves a byte.
Erik the Outgolfer

2

05AB1E, 27 bytes

þL"/*+-"¹'!¢©è".»"ì.VD_нi®Θ

Try it online!


DO you know why „.» doesn't work?
Riley

@Riley » is part of an unfinished compressed string, so it errors out and, as usually in 05AB1E, the error is ignored.
Erik the Outgolfer

I was trying to do "*+-/"èU then after using L follow up with .»X but it treats X as a string, not a command and .»X.V is even wonkier.
Magic Octopus Urn

@MagicOctopusUrn X doesn't eval. X.V are two commands.
Erik the Outgolfer

@EriktheOutgolfer yeah, but I was hoping it'd eval before processing the fold. Hoping, not expecting :(. Could've sworn there was a "use single character String as command in dyadic chain" or something.
Magic Octopus Urn

2

Ruby, 83 80 79 bytes

->s{z=s.count ?!;s<?1?1+1<=>z:eval([*1..w=s.to_i]*(".0"+"_*+-/"[z]+?()+?)*~-w)}

Try it online!

Explanation:

->s{
    # Get number of !
    z=s.count ?!

    # Special case: if the number is 0, then output 0 or 1 depending on z
    s<?1?1+1<=>z:

    # Otherwise build the full expression as a string and then evaluate it
    eval([*1..w=s.to_i]*(".0"+"_*+-/"[z]+?()+?)*~-w)
}

2

Java 8, 141 136 134 bytes

s->{float q=s.split("!",-1).length,n=new Float(s.split("!")[0]),i=n,r=n;for(;--i>0;r=q<3?i*r:q<4?i+r:q<5?i-r:i/r);return n<1&q<3?1:r;}

-5 bytes (141 → 136) thanks to @CarlosAlejo's C# answer.

Explanation:

Try it here.

s->{                                // Method with String parameter and float return-type
  float q=s.split("!",-1).length,   //  Amount of exclamation marks + 1
        n=new Float(s.split("!")[0]),
                                    //  The number before the exclamation marks
        i=n,                        //  Index (starting at `n`)
        r=n;                        //  Return sum (starting at `n`)
  for(;--i>0;                       //  Loop from `i-1` down to 1
    r=                              //   Change the result (`r`) to:
      q<3?                          //    If `q` is 2:
       i*r                          //     Multiply
      :q<4?                         //    Else if `q` is 3:
       i+r                          //     Addition
      :q<5?                         //    Else if `q` is 4:
       i-r                          //     Subtraction
      :                             //    Else (if `q` is 5):
       i/r                          //     Division
  );                                //  End of loop
  return n<1&q<3?                   //  Edge case if the input is `0!`:
          1                         //   Then return 1
         :                          //  Else:
          r;                        //   Return the result
}                                   // End of method

1
I have seen a similar answer somewhere else... :-D I keep forgetting that float is shorter than double.
Charlie

@CarlosAlejo Yeah, I noticed your answer after my initial 141 byte answer. Changing float q=s.length()-(s=s.replace("!","")).length(),n=new Float(s) to the current answer saved me 5 bytes. :) Forgot to add a "bytes saved thanks to" part I noticed now.. Sorry about that.
Kevin Cruijssen

oh, nevermind that, I'm glad you liked my answer. :-)
Charlie

2

Jelly,  24 23 26  25 bytes

+ 3  2 bytes patching up to fix after misinterpretation :(

×
+
_
÷
ṣ”!µḢVRṚȯL©Ị$®ŀ@/

A full program (a monadic link with helper links referenced by program location)

Try it online! or see a test suite.

How?

× - Link 1, multiply: number, number

+ - Link 2, add: number, number

_ - Link 1, subtract: number, number

÷ - Link 1, divide: number, number

ṣ”!µḢVRṚȯL©Ị$®ŀ@/ - Main link: list of characters, a
ṣ”!               - split s at '!' characters
   µ              - monadic separation, call that b
    Ḣ             - head - pop and yield the digit list from b, modifying b
     V            - evaluate as Jelly code (get the number, say N)
      R           - range = [1,2,3,...,N]
       Ṛ          - reverse = [N,...,3,2,1]
            $     - last two links as a monad:
         L        -   length of modified b (number of '!' characters)
          ©       -   (copy to register)
           Ị      -   insignificant? (1 when just one '!', 0 when two or more)
        ȯ         - logical or (1 for "0!", 0 for "0!!...", the reversed-range otherwise)
                / - cumulative reduce by:
               @  -  with swapped arguments:
              ŀ   -    dyadic call of link at index:
             ®    -      recall value from register (number of '!' characters)

Fails for 0!.
Erik the Outgolfer

Oh, haha - I had read your comment under the OP wrong - I thought they'd made 0! defined as 0 which would be wrong.
Jonathan Allan

All fixed up now :)
Jonathan Allan

Too bad TIO is broken right now so that I can't test if it's still invalid. :( :P Also too bad that you can't use / on an empty list. D: EDIT: Apparently valid for 0!, 0!!, 0!!! and 0!!!!. +1
Erik the Outgolfer

2

Self-modifying x86_64 machine code, 123 bytes

0f b6 0f 31 c0 eb 11 0f be c9 8d 04 80 8d 44 41 d0 0f b6 4f 01 48 ff c7 83 f9 21 75 ea b9 21 21 21 a1 33 0f 0f bc c9 81 c1 ff 07 00 00 c1 e9 03 0f b6 c9 89 ca 09 c2 74 35 55 48 89 e5 c7 45 fc 59 58 5c 5e 8a 4c 0d fc 88 0d 15 00 00 00 f3 0f 2a c8 83 f8 02 5d 7c 1f ff c8 0f 57 c0 f3 0f 2a c0 f3 0f 5e c1 83 f8 01 0f 28 c8 7f eb c3 f3 0f 10 05 03 01 00 00 c3 0f 28 c1 c3

Why would interpreted languages be able to dynamically run code with fancy evals, but not plain machine code?

Try it with:

#include <stdio.h>
#include <sys/mman.h>
#include <errno.h>

char ff[] = "\x0f\xb6\x0f\x31\xc0\xeb\x11\x0f\xbe\xc9\x8d\x04\x80\x8d\x44\x41\xd0\x0f\xb6\x4f\x01\x48\xff\xc7\x83\xf9\x21\x75\xea\xb9\x21\x21\x21\xa1\x33\x0f\x0f\xbc\xc9\x81\xc1\xff\x07\x00\x00\xc1\xe9\x03\x0f\xb6\xc9\x89\xca\x09\xc2\x74\x35\x55\x48\x89\xe5\xc7\x45\xfc\x59\x58\x5c\x5e\x8a\x4c\x0d\xfc\x88\x0d\x15\x00\x00\x00\xf3\x0f\x2a\xc8\x83\xf8\x02\x5d\x7c\x1f\xff\xc8\x0f\x57\xc0\xf3\x0f\x2a\xc0\xf3\x0f\x5e\xc1\x83\xf8\x01\x0f\x28\xc8\x7f\xeb\xc3\xf3\x0f\x10\x05\x03\x01\x00\x00\xc3\x0f\x28\xc1\xc3";
int main()
{
    char* page = (char*)((unsigned long)((char*)ff) & (~0xfffLL));
    if (mprotect(page, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC) < 0) {
        perror("mprotect");
        return -1;
    }
    float (*f)(char*) = (float (*)(char*))ff;
    char* testcases[] = { "0!","1!","2!","3!","4!","5!","6!","7!","8!","9!","10!",
                          "0!!","1!!","2!!","3!!","4!!","5!!","6!!","7!!","8!!","9!!","10!!",
                          "0!!!","1!!!","2!!!","3!!!","4!!!","5!!!","6!!!","7!!!","8!!!","9!!!","10!!!",
                          "0!!!!","1!!!!","2!!!!","3!!!!","4!!!!","5!!!!","6!!!!","7!!!!","8!!!!","9!!!!","10!!!!",
                        };
    for (int i = 0; i < 44; i++) {
        printf("%s -> %f\n", testcases[i], f(testcases[i]));
    }
}

Assembly:

_f:
100000d4f:  0f b6 0f    movzx   ecx, byte ptr [rdi]
100000d52:  31 c0   xor eax, eax
100000d54:  eb 11   jmp 17 <_f+18>
100000d56:  0f be c9    movsx   ecx, cl
100000d59:  8d 04 80    lea eax, [rax + 4*rax]
100000d5c:  8d 44 41 d0     lea eax, [rcx + 2*rax - 48]
100000d60:  0f b6 4f 01     movzx   ecx, byte ptr [rdi + 1]
100000d64:  48 ff c7    inc rdi
100000d67:  83 f9 21    cmp ecx, 33
100000d6a:  75 ea   jne -22 <_f+7>
100000d6c:  b9 21 21 21 a1  mov ecx, 2703302945
100000d71:  33 0f   xor ecx, dword ptr [rdi]
100000d73:  0f bc c9    bsf ecx, ecx
100000d76:  81 c1 ff 07 00 00   add ecx, 2047
100000d7c:  c1 e9 03    shr ecx, 3
100000d7f:  0f b6 c9    movzx   ecx, cl
100000d82:  89 ca   mov edx, ecx
100000d84:  09 c2   or  edx, eax
100000d86:  74 35   je  53 <_f+6E>
100000d88:  55  push    rbp
100000d89:  48 89 e5    mov rbp, rsp
100000d8c:  c7 45 fc 59 58 5c 5e    mov dword ptr [rbp - 4], 1583110233
100000d93:  8a 4c 0d fc     mov cl, byte ptr [rbp + rcx - 4]
100000d97:  88 0d 15 00 00 00   mov byte ptr [rip + 21], cl
100000d9d:  f3 0f 2a c8     cvtsi2ss    xmm1, eax
100000da1:  83 f8 02    cmp eax, 2
100000da4:  5d  pop rbp
100000da5:  7c 1f   jl  31 <_f+77>
100000da7:  ff c8   dec eax
100000da9:  0f 57 c0    xorps   xmm0, xmm0
100000dac:  f3 0f 2a c0     cvtsi2ss    xmm0, eax
100000db0:  f3 0f 5e c1     divss   xmm0, xmm1
100000db4:  83 f8 01    cmp eax, 1
100000db7:  0f 28 c8    movaps  xmm1, xmm0
100000dba:  7f eb   jg  -21 <_f+58>
100000dbc:  c3  ret
100000dbd:  f3 0f 10 05 03 01 00 00     movss   xmm0, dword ptr [rip + 259]
100000dc5:  c3  ret
100000dc6:  0f 28 c1    movaps  xmm0, xmm1
100000dc9:  c3  ret

Explanations will be added later. The basic idea is to modify the divss xmm0, xmm1 instruction at 0x100000db0 and replace it with a mulss, addss, subss or divss according to supplied operand. A small trick is also used to parse the input string.

Assembly generated with:

float f (char* s)
{
    int x;
    for (x=0; *s != '!'; s++) {
        x=10*x + (*s-'0');
    }
    unsigned char op = (__builtin_ctz(*(unsigned int *)s ^ 0xa1212121)-1) >> 3;
    if (x == 0 && op == 0) {
        return 1;
    }
    unsigned int lookup = 0x5e5c5859;
    unsigned char new_code = ((unsigned char*)&lookup)[op];
    asm("movb %0, 0x15(%%rip)" : : "r" (new_code));
    float sum;
    for (sum = x--; x>0; x--) {
        sum = x / sum;
    }
    return sum;
}

2

Haskell, 105 102 98 96 bytes

0!3=0
x!y=foldr([(*),(+),(-),(/)]!!y)([1,0,0,1]!!y)[1..x]
f s|[(n,b)]<-lex s=read n!(length b-1)

Saved 9 bytes thanks to Zgarb and nimi.

Try it online.


@Zgarb You are right. Fixed.
Cristian Lupascu

I think you can also drop the parens around read n, and f= is unnecessary as per our rules.
Zgarb

@Zgarb Right again :) . Thanks!
Cristian Lupascu

Switching back to a named function and using lex saves two bytes: f s|[(n,b)]<-lex s=read n!(length b-1).
nimi

@nimi Wow, thanks! I'm so new to Haskell that I didn't even know about lex. That's awesome! :) I don't see how that saves bytes though - I get 99 bytes after this.
Cristian Lupascu

1

Gaia, 26 25 bytes

ẋ)@d┅v;l“×+⁻÷”=“ₔ⊢”+e¤ḥ!∨

Try it online!

Explanation

ẋ                          Split the input into runs of the same character.
 )                         Get the last one (the !'s).
  @                        Push an input (since there's none left, use the last one).
   d                       Parse as number (ignores the !'s).
    ┅v                     Get the reverse range: [n .. 1]
      ;                    Copy the ! string
       l“×+⁻÷”=            Get its length and index into this string of operators.
               “ₔ⊢”+       Append 'ₔ⊢' to the operator.
                    e      Eval the resulting string, which is "reduce by <operator> with
                            swapped arguments." Reducing an empty list gives 0.
                     ¤     Bring the !'s back to the top.
                      ḥ!   Remove the first character and check if it's empty.
                        ∨  Logical OR; turns 0 from 0! to 1, doesn't change anything else.


1

APL (Dyalog), 30 bytes

Inspired by lstefano's solution.

{0::0⋄(⍎'×+-⌹'⊃⍨≢⍵~⎕D)/⍳⍎⍵∩⎕D}

Try it online!

{} anonymous function where the argument is represented by :

0:: if any error happens:

  0 return zero

 now try:

  ⍵∩⎕D the intersection of the argument and the set of Digits (removes exclamation points)

   execute that (turns it into a number)

  ɩndices of that

  ()/ insert (APL is right associative, as needed) the following function between terms:

   ⍵~⎕D argument without Digits (leaves exclamation points)

   tally that (i.e. how many exclamation points)

  '×+-⌹'⊃⍨ use that to pick from the list of symbols*

   execute (turns the symbol into a function)


(matrix division) is used instead of ÷ (normal division) to cause an error on an empty list


What does :: do in a dfn?
Zacharý

It is an error guard. If at any point after the error guard is set up, an error with any of the numbers (0=1…999, 1000=1001…) to the left of the :: happens, then the value to the right of the :: is immediately returned.
Adám

Well, I never knew about that, thanks!
Zacharý


0

Dyalog APL, at least 29 chars

{(⍎i⊃'×+-÷')/⍳⍎⍵↓⍨-i←+/'!'=⍵}

The expression is ALMOST correct. It passes all the test cases EXCEPT 0!!!! for which it gives 1 instead of the required 0 and that's because in APL the reduction of an empty vector is supposed to return the neutral element for the function used to reduce. For the quotient that's 1. At the moment I don't have time to try and fix it but I'll leave it here for a rainy day.


It is raining: {0::0⋄(⍎'×+-⌹'⊃⍨≢⍵~⎕D)/⍳⍎⍵∩⎕D} Try it online!
Adám

Very cool! I don't mind at all if you claim it as your solution, given that the differences are more than the similarities.
lstefano



0

Mathematica, 152 bytes

(T=ToExpression;If[#=="0!!!!",0,s=T@StringCount[#,"!"];t=T@StringDrop[#,-s];{#!,i~Sum~{i,#},Sum[-i(-1)^i,{i,#}],N@Product[1/i^(-1)^i,{i,#}]}[[s]]&[t]])&

0

Javascript, 111 163 bytes

s=>([a,b]=s.split(/\b/),c=b.length,a==0&c==1||eval((p=[...Array(+a+1).keys()].slice(1).join(c-1?c-2?c-3?'/(':'-(':'+':'*'))+')'.repeat((p.match(/\(/g)||[]).length)))

Readable Version

s=>([a,b]=s.split(/\b/),c=b.length,a==0&c==1||eval((p=
[...Array(+a+1).keys()].slice(1).join(c-1?c-2?c-3?'/(':'-
(':'+':'*'))+')'.repeat((p.match(/\(/g)||[]).length)))
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.