From db95b092e5e57ca7fcc7f58687594bc1e445d3d1 Mon Sep 17 00:00:00 2001 From: Artemy Date: Mon, 15 Aug 2022 18:52:32 +0300 Subject: [PATCH] doc: factorial --- examples/factorial.json5 | 77 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 examples/factorial.json5 diff --git a/examples/factorial.json5 b/examples/factorial.json5 new file mode 100644 index 0000000..8f57383 --- /dev/null +++ b/examples/factorial.json5 @@ -0,0 +1,77 @@ +[ + { + fn: { + name: "fact", + args: ["n"], + body: [ + { let: { result: { var: "fact.n" } } }, + { + loop: [ + //using loop because recursion is not supported + { + if: { + condition: { comp: [{ var: "fact.n" }, ">", 1] }, + body: [ + { + assign: { + "fact.n": { + calc: [{ var: "fact.n" }, "-", 1], + }, + }, + }, + { + assign: { + result: { + calc: [{ var: "result" }, "*", { var: "fact.n" }], + }, + }, + }, + ], + else: ["break"], + }, + }, + ], + }, + { return: { var: "result" } }, + ], + }, + }, + + { + fn: { + name: "_eq", + args: ["a", "b"], + body: [{ return: { comp: [{ var: "_eq.a" }, "==", { var: "_eq.b" }] } }], + }, + }, + + ["10! == 3 628 800: ", { _eq: [{ fact: [10] }, 3628800] }], + ["11! == 39 916 800: ", { _eq: [{ fact: [11] }, 39916800] }], + + ["12! == 479 001 600: ", { _eq: [{ fact: [12] }, 479001600] }], + ["13! == 6 227 020 800: ", { _eq: [{ fact: [13] }, 6227020800] }], + + ["14! == 87 178 291 200: ", { _eq: [{ fact: [14] }, 87178291200] }], + ["15! == 1 307 674 368 000: ", { _eq: [{ fact: [15] }, 1307674368000] }], + + ["16! == 20 922 789 888 000: ", { _eq: [{ fact: [16] }, 20922789888000] }], + ["17! == 355 687 428 096 000: ", { _eq: [{ fact: [17] }, 355687428096000] }], + + [ + "18! == 6 402 373 705 728 000: ", + { _eq: [{ fact: [18] }, 6402373705728000] }, + ], + [ + "19! == 121 645 100 408 832 000: ", + { _eq: [{ fact: [19] }, 121645100408832000] }, + ], + + [ + "20! == 2 432 902 008 176 640 000: ", + { _eq: [{ fact: [20] }, 2432902008176640000] }, + ], + // [ + // "21! == 51 090 942 171 709 440 000: ", + // { _eq: [{ fact: [21] }, 51090942171709440000] }, + // ], some json and yaml troubles with number `51090942171709440000` +]