复利…与Wizard Money


15

Gringotts不仅是一个保险库,而且知名的金融机构和巫师也需要贷款。由于您不想被Gringotts妖精所困扰,因此您决定编写一个计算兴趣的程序是一个好主意。利息仅每年复利一次。

您的任务是在给定本金,利率和时间(全年)的情况下,计算利息后的总欠款,以向导货币的总面额进行操作,向下舍入到最接近的整数Knut。银色镰刀中有29枚青铜螺母,金色帆船中有17根镰刀。

Loan taken out:
 23 Knuts
 16 Sickles
103 Galleons
@ 7.250%
For 3 years

Total owed after interest:
 24 Knuts
  4 Sickles
128 Galleons

注释和规则

  • 输入和输出可以采用任何方便的格式。您必须输入Knuts,Sickles,Galleons,利率和时间。除利率外,所有都是整数。利率以0.125%的增量递增。
  • 不能保证所输入的金额是标准的(即,您可以有29个或更多的Knuts和17个或更多的Sickles。)
  • 输出必须是规范表示。(即少于29公斤和少于17镰刀)
  • 与任意精确度计算相比,所欠的总计(不超过1,000加仑)的年精确度应为每年感兴趣的1 Knut。
    • 您可以在感兴趣的每一年之后或仅在结束时四舍五入。参考计算可将此考虑在内,以进行准确性检查。

打高尔夫球快乐!


4
我们可以将利率作为小数而不是百分比吗?(例如,0.0725而不是7.25
Shaggy

@Shaggy我也想知道这一点
senox13

如果贷款正好是1 Knut,利息是每年99%,期限是1年,结果应该是“ 1 Knut”还是“ 2 Knuts”?
Chas Brown

换句话说,请说明该短语的数学含义rounding down
senox13

1
@ChasBrown:1克纳特。截断/底函数到最接近的整个Knut。
Beefster

Answers:


6

R70 62字节

function(d,i,y)(x=d%*%(a=c(1,29,493))*(1+i)^y)%/%a%%c(29,17,x)

在线尝试!

输入为d:存放在坚果,镰刀,大帆船中;i:利率为小数;y:年。将最终沉积物输出到坚果,镰刀,大帆船。感谢@Giuseppe使用矩阵乘法来保存一些字节(并指出了如何避免在1e99处换行)。


我不认识R;让它们环绕起来能赢得什么?
dfeuer

@dfeuer,他们接受了mod 1e99,因此,如果您的帆船达到如此之高,它们会降为零
Nick Kennedy

我想知道的是,通过采用1e99模组可以获得什么。
dfeuer

大多数R函数是矢量化的。在这种情况下,我将输出传递给%%mod函数。理想情况下,我想单独保留这些帆船,但是将数字无穷大返回NaN,所以我只使用了一个非常大的数字(但字节数很小)。我想出的替代方法更长(例如[ tio.run/##JYrLCsIwEEV/…在线尝试!])
Nick Kennedy

@NickKennedy你也可以做9e99...此外,你可以打高尔夫球到63字节
朱塞佩

4

Python 3.8(预发布)75 74 71字节

-1个字节归@EmbodimentofIgnorance
-3个字节归@@ xnor

这将Knuts,Sickles和Galleons用作整数,将利息表示为浮点数(小数而不是百分比),将年作为整数。它返回一个元组,其中包含分别对应于Knuts,Sickles和Galleons的利息的数字。

lambda K,S,G,R,Y:((k:=int((K+G*493+S*29)*(1+R)**Y))%29,k//29%17,k//493)

用法:

>>> print(I(23,16,103,0.0725,3))
(24, 4, 128)

在线尝试!


接得好。更新答案
senox13

问题说operating in whole denominations of wizard money, rounding down。我的rounding down意思是,chop off everything after the decimal point.使用标头绝对听起来像是一种更简单的处理方式。我会在以后的帖子中这样做,谢谢
senox13

这听起来更像是“截断”而不是“四舍五入”。但我已要求OP进行澄清(因为nit-picking是PPCG上的游戏名称:))。
Chas Brown

我不同意您的看法,这只是我一直看到的用于四舍五入的含义,因为您总是四舍五入到结果以下的整数。否则,这只是正常的舍入。让OP决定是一个好主意
senox19年

仅供参考,使匿名函数可在TIO上进行测试的一个有用技巧是将I\=标题放入如下所示。另外,它看起来k//29//17可以是k//493
xnor19,2016年

3

APL + WIN,37 28 26字节

⌊a⊤((a←0 17 29)⊥⎕)×(1+⎕)*⎕

感谢lirtosiast,节省了2个字节

在线尝试!由Dyalog Classic提供

说明:

(1+⎕)*⎕ prompts for years followed by decimal interest rate and calculates
         compounding multiplier

((a←0 17 29)⊥⎕) prompts for Galleons, Sickles and Knuts and converts to Knuts

⌊a⊤ converts back to Galleons, Sickles and Knuts and floor 
    after applying compound interest. 

⌊a⊤(⎕⊥⍨a←0 17 29)×⎕*⍨1+⎕24?
lirtosiast

@lirtosiast谢谢,但是我的古老APL + WIN解释器恐怕没有⍨函数。务必将其提交为您自己的APL解决方案。
格雷厄姆

@lirtosiast再次感谢,我已将分配给a的2个字节占用了。
格雷厄姆

3

Perl 6,47个字节

((1+*)*** *(*Z*1,29,493).sum+|0).polymod(29,17)

在线尝试!

令我惊讶的是,我设法将其放入匿名的任何lambda中!特别是*比其他任何东西都重要的部分。将输入作为,interest rate (e.g. 0.0725), years, [Knuts, Sickles, Galleons]并以相同顺序返回货币列表。

说明:

 (1+*)           # Add one to the interest rate
      ***        # Raise to the power of the year
          *      # And multiply by
           (*Z*1,29,493).sum      # The number of Knuts in the input
                            +|0   # And floor it
(                              ).polymod(29,17)   # Get the modulos after divmoding by 29 and 17

我很惊讶您没有想出一种方法也可以使胡桃木/镰刀/加隆子的数量也适合任何情况。然后就好了,就像************************** ;-)
user0721090601

@guifa无论输入是什么,因此实际上只能有3个输入(尽管我可以将货币输入分成更多*s但更多字节)。其余的*s来自乘法(*)和指数(**
Jo King

我的意思是,如果您也将转化率(29/17数字)纳入其中。但这当然是个玩笑,因为您需要多次使用这些数字。抱歉,如果我的幽默感没有得到解决
user0721090601

2

果冻,29 个字节

“¢×ø‘©×\
÷ȷ2‘*⁵×÷¢S×¢d®U1¦Ṫ€Ḟ

接受参数的完整程序: rate ; [Galleons, Sickles, Knuts]; years
印刷品[Galleons, Sickles, Knuts]

在线尝试!

在整个学期末发言。
÷ȷ2如果我们接受该比率为比率而不是百分比,则可以将其删除。

怎么样?

“¢×ø‘©×\ - Link 1 multipliers: no arguments
“¢×ø‘    - list of code-age indices = [1,17,29]
     ©   - (copy this to the register for later use)
       \ - reduce by:
      ×  -   multiplication  = [1,17,493]

÷ȷ2‘*⁵×÷¢S×¢d®U1¦Ṫ€Ḟ - Main Link
 ȷ2                  - 10^2 = 100
÷                    - divide = rate/100
   ‘                 - increment = 1+rate/100
     ⁵               - 5th command line argument (3rd input) = years
    *                - exponentiate = (1+rate/100)^years --i.e. multiplicand
      ×              - multiply (by the borrowed amounts)
        ¢            - call last Link as a nilad
       ÷             - divide (all amounts in Galleons)
         S           - sum (total Galleons owed)
           ¢         - call last Link as a nilad
          ×          - multiply (total owed in each of Galleons, Sickles, Knuts)
             ®       - recall from register = [1,17,29]
            d        - divmod (vectorises) = [[G/1, G%1], [S/17, S^17], [K/17, K%17]]
              U1¦    - reverse first one = [[G%1, G/1], [S/17, S%17], [K/17, K%17]]
                 Ṫ€  - tail €ach = [G/1, S%17, K%17]
                   Ḟ - floor (vectorises)

2

英特尔8087 FPU组件,86字节

d9e8 d906 7f01 dec1 8b0e 8301 d9e8 d8c9 e2fc df06 7901 df06 8701 df06
7b01 df06 8501 df06 7d01 dec9 dec1 dec9 dec1 dec9 9bd9 2e89 01df 0687
01df 0685 01d9 c1de c9d9 c2d9 f8d8 f2df 1e7b 01d8 fadf 1e7d 01d9 c9d9
f8df 1e79 01

未经汇编和记录:

; calculate P+I of loan from wizard
; input:
;   G: number of Galleons (mem16)
;   S: number of Sickles (mem16)
;   K: number of Knuts (mem16)
;   R: interest rate (float)
;   T: time in years (mem16)
;   GS: Galleons to Sickles exchange rate (mem16)
;   SK: Sickles to Knuts exchange rate (mem16)
; output:
;   G: number of Galleons (mem16)
;   S: number of Sickles (mem16)
;   K: number of Knuts (mem16)
WIZ_INT_CALC    MACRO   G, S, K, R, T, GS, SK
                LOCAL   LOOP_EXP
                    ; - calculate interet rate factor
    FLD1            ; load 1
    FLD   R         ; load interest rate
    FADD            ; ST = rate + 1
    MOV   CX, T     ; Exponent is count for loop
    FLD1            ; load 1 into ST as initial exponent value
LOOP_EXP:           ; loop calculate exponent
    FMUL  ST,ST(1)  ; multiply ST = ST * ST(1)
    LOOP  LOOP_EXP
                    ; - convert demonimations to Knuts
    FILD  K         ; load existing Knuts
    FILD  SK        ; load Sickles to Knuts rate 
    FILD  S         ; load existing Sickles
    FILD  GS        ; load Galleons-to-Sickles exchange rate
    FILD  G         ; load existing Galleons
    FMUL            ; multiply galleons to get sickles
    FADD            ; add existing sickles
    FMUL            ; multiply sickles to get knuts
    FADD            ; add existing knuts
    FMUL            ; calculate P+I (P in Knuts * Interest factor)
                    ; - redistribute demonimations to canonical form
    FLDCW  FRD      ; put FPU in round-down mode
    FILD   SK       ; load Sickles to Knuts rate
    FILD   GS       ; load Galleons-to-Sickles exchange rate
    FLD    ST(1)    ; copy Galleons-to-Sickles exchange rate to stack for later
    FMUL            ; multiply to get Galleons-to-Knuts rate
    FLD    ST(2)    ; push original total Knuts from ST(2) into ST (lost by FPREM)
    FPREM           ; get remainder
    FDIV   ST,ST(2) ; divide remainder to get number of Sickles
    FISTP  S        ; store Sickles to S
    FDIVR  ST,ST(2) ; divide to get number of Galleons
    FISTP  G        ; store Galleons to G
    FXCH            ; swap ST, ST(1) for FPREM
    FPREM           ; get remainder to get number of Knuts
    FISTP  K        ; store Knuts to K
        ENDM

实现为MACRO(基本上是一种功能),这是非OS专用的机器代码,仅使用Intel 80x87 FPU /数学协处理器进行计算。

输出示例测试程序:

    FINIT           ; reset FPU

    WIZ_INT_CALC    G,S,K,R,T,GS,SK     ; do the "Wizardy"

    MOV  AX, K      ; display Knuts
    CALL OUTDEC     ; generic decimal output routine
    CALL NL         ; CRLF

    MOV  AX, S      ; display Sickles
    CALL OUTDEC     ; generic decimal output routine
    CALL NL         ; CRLF

    MOV  AX, G      ; display Galleons
    CALL OUTDEC     ; generic decimal output routine
    CALL NL         ; CRLF

    RET             ; return to DOS

K   DW  23          ; initial Kunts
S   DW  16          ; initial Sickles
G   DW  103         ; initial Galleons
R   DD  0.0725      ; interest rate
T   DW  3           ; time (years)
GS  DW  17          ; Galleons to Sickles exchange rate
SK  DW  29          ; Sickles to Knuts exchange rate
FRD DW  177FH       ; 8087 control word to round down

输出量

在此处输入图片说明


1

Japt,48个字节

XÄ pY *(U*493+V*29+W)f
Uu493
[Uz493 ,Vz29 ,Vu29]

我第一次在Japt中尝试@Shaggy的赏金!不用说,这不是很高尔夫:(

在线尝试!


1

Haskell,73个字节

(g#s)k r n|(x,y)<-truncate((493*g+29*s+k)*(1+r)^n)%29=(x%17,y)
(%)=divMod

在线尝试!

感谢@Laikoni提供了两个字节。

trick俩:输入中的硬币数为浮点数(Double),而输出中的硬币数为整数(Integer)。结果是形成了一对嵌套,((Galleons, Sickles), Knotts)以避免必须展平到三倍。

说明

-- Define a binary operator # that
-- takes the number of Galleons
-- and Slivers and produces a
-- function taking the number of
-- Knots, the rate, and the
-- number of years and producing
-- the result.
(g#s) k r n
   -- Calculate the initial value
   -- in Knotts, calculate the
   -- final value in Knotts,
   -- and divide to get the number
   -- of Galleons and the
   -- remainder.
  |(x,y)<-truncate((493*g+29*s+k)*(1+r)^n)%29
  -- Calculate the number of Slivers
  -- and remaining Knotts.
  =(x%17,y)
(%)=divMod

1
使用(truncate$ ... )-> truncate( ... )(g#s)k r n代替保存两个字节c g s k r n
Laikoni

@Laikoni,非常感谢!
dfeuer

@Laikoni,如果可以的话,如果您能在codegolf.stackexchange.com/questions/55960/…中找到我几个字节,我将不胜感激。
dfeuer

1
有时间我会调查一下。同时,我可以指出你到我们Haskell的聊天室单子和男人,也给了这个问题,你可能会想给你的拥抱/ GHC polyglots。
Laikoni'3

1

Stax,24 个字节

»♀(╪M╢ú!!«ε◘÷╛SI►U/)-f!ö

运行并调试

输入是用空格分隔的值。 interest years knuts sickles galleons

输出以换行符分隔。

knuts
sickles
galleons

1

TI-BASIC(TI-84), 96 90字节

:SetUpEditor C:Ans→∟C:∟C(1)+29∟C(2)+493∟C(3)→T:T(1+∟C(4))^∟C(5)→T:remainder(iPart(T),493→R:{remainder(R,29),iPart(R/29),iPart(T/493)}

输入为Ans,该列表包含5个项目:坚果,镰刀,帆船,利息(小数)和时间(年)。程序完成后,
输出已进入Ans并自动打印输出。

未打高尔夫球:

:SetUpEditor C 
:Ans→∟C
:∟C(1)+29∟C(2)+493∟C(3)→T
:T(1+∟C(4))^∟C(5)→T
:remainder(iPart(T),493→R
:{remainder(R,29),iPart(R/29),iPart(T/493)}

例:

{32,2,5,0.05,5}
       {32 2 5 .05 5}
prgmCDGF1
            {12 10 6}

说明:

:SetUpEditor C
:Ans→∟C

∟C创建一个新列表,然后Ans其存储在其中。

:∟C(1)+29∟C(2)+493∟C(3)→T

坚果,镰刀和大帆船被转换成坚果并存储到 T

:T(1+∟C(4))^∟C(5)→T

取一定数量的Knuts并对其应用复利。
在此计算利息。

:remainder(iPart(T),493→R

存储 nteger 部分T模493到R。用于缩短字节数。

:{remainder(R,29),iPart(R/29),iPart(T/493)}

评估包含3个项目(胡桃,镰刀和帆船)的列表。该列表将自动存储到中Ans


注意: 字节数是通过[MEM][2]中给出的字节数来评估的[7]中(RAM中的程序列表)中然后减去程序名称中的字符数和用于该程序的额外8个字节:

103-5-8 = 90字节


0

K,46字节

c:1000 17 29
t:{c\:{z(y*)/x}[c/:x;1+y%100;z]}

c 存储列表以进行基本转换

t 是计算总量的函数

使用示例:

t[103 16 23;7.25;3]

(128;4;24.29209)

说明:

  • c/:x 将列表(加利昂,镰刀,坚果)转换为kuts

  • 1+y%100 计算利率(例如,利率为7.25%的示例为1.0725)

  • lambda {z(y*)\x}完成工作:迭代3次,应用interes * main,然后返回最终的main。

  • c\: 从坚果中产生大帆船,镰刀,坚果

注意。-如果您不需要名称功能,我们可以使用lambda,节省2个字节 {c\:{z(y*)/x}[c/:x;1+y%100;z]}inputArgs


0

C#(Visual C#交互式编译器),86字节

(a,b,c)=>((k=(int)((a.a*493+a.b*29+a.c)*Math.Pow(1+b,c)))/493,(k%=493)/29,k%29);int k;

将inout作为一个命名元组,具有3个值,分别表示坚果,镰刀和加仑,利率为双精度值(不是百分比)。我真的希望C#有一个指数运算符。Math.Pow太长了:(

在线尝试!


0

批处理,171字节

@set i=%4
@set/af=0,i=8*%i:.=,f=%,f*=8
@set/ai+=%f:~,1%,k=%1*493+%2*29+%3
@for /l %%y in (1,1,%5)do @set/ak+=k*i/800
@set/ag=k/493,s=k/29%%17,k%%=29
@echo %g% %s% %k%

以Galleons,Sickles,Knuts,interest,years的顺序将输入作为命令行参数。利息是一个百分比,但没有%符号表示。每年之后截断。输出按Galleons,Sickles,Knuts的顺序排列。至少支持5000加仑。说明:

@set i=%4
@set/af=0,i=8*%i:.=,f=%,f*=8

批处理只有整数运算。幸运的是,利率始终是的倍数0.125。我们从对小数点进行分割开始,以便i成为利率和f小数部分的整数部分。然后将这些乘以8。现在的第一个数字f是百分比利率的八分位数。

@set/ai+=%f:~,1%,k=%1*493+%2*29+%3

然后使用字符串切片将其提取并相加,以得到1/800的利率。还计算了坚果的数量。

@for /l %%y in (1,1,%5)do @set/ak+=k*i/800

计算并加上每年的利息。

@set/ag=k/493,s=k/29%%17,k%%=29
@echo %g% %s% %k%

转换回Galleons和镰刀。


0

05AB1E(旧版),24 字节

>Im•1ýÑ•3L£I*O*ï29‰ć17‰ì

@JoKing的Perl 6答案的端口,因此,如果您喜欢此答案,请确保也对他!

由于新版本中的一个错误£不适用于整数,因此我使用旧版,因此将其显式转换为字符串§(介于第二个3)(直到该错误已修复)。

将利息作为小数,然后是年份,然后是[坚果,镰刀,帆船]列表。

在线尝试。

说明:

>                      # Increase the (implicit) interest decimal by 1
                       #  i.e. 0.0725 → 1.0725
 Im                    # Take this to the power of the year input
                       #  i.e. 1.0725 and 3 → 1.233...
1ýÑ•                  # Push compressed integer 119493
     3L                # Push list [1,2,3]
       £               # Split the integer into parts of that size: [1,19,493]
        I*             # Multiply it with the input-list
                       #  i.e. [1,19,493] * [23,16,103] → [23,464,50779]
          O            # Take the sum of this list
                       #  i.e. [23,464,50779] → 51266
           *           # Multiply it by the earlier calculated number
                       #  i.e. 51266 * 1.233... → 63244.292...
            ï          # Cast to integer, truncating the decimal values
                       #  i.e. 63244.292... → 63244
             29       # Take the divmod 29
                       #  i.e. 63244 → [2180,24]
                ć      # Extract the head; pushing the remainder-list and head separately
                       #  i.e. [2180,24] → [24] and 2180
                 17   # Take the divmod 17 on this head
                       #  i.e. 2180 → [128,4]
                    ì  # And prepend this list in front of the remainder-list
                       #  i.e. [24] and [128,4] → [128,4,24]
                       # (which is output implicitly as result)

看到这个05AB1E尖矿(部分如何压缩大整数?理解为什么•1ýÑ•119493


0

APL(NARS),37个字符,74个字节

{(x y z)←⍵⋄⌊¨a⊤(z⊥⍨a←0 17 29)×x*⍨1+y}

Graham用户将非常少的字节数的APL解决方案转换为使用一个函数而不是标准输入的解决方案...测试以及如何使用它:

  f←{(x y z)←⍵⋄⌊¨a⊤(z⊥⍨a←0 17 29)×x*⍨1+y}
  f 3 0.0725 (103 16 23)
128 4 24

(我不是说我已经了解算法)


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.