使用Gitbash中的shell脚本编写一个程序,该程序接受命令行参数n并打印第n个谐波数

beq87vna  于 2022-11-25  发布在  Shell
关注(0)|答案(3)|浏览(169)

由于调和数包括分数,我不知道如何在程序中使用分数,因此我得到错误。请建议
下面是我试过的程序--〉〉〉

#!/bin/bash -x

read -p "Enter a number:" n
totalharmonic=0

for((count=1;count<=$n;count++))
do

harmonic=$((1/$count))
totalharmonic=$(($harmonic + $totalharmonic));
done

echo "nth harmonic number is $totalharmonic"
0g0grzrc

0g0grzrc1#

不要使用bash算术(仅限整数)进行浮点计算。例如,使用bcawkpython ... bc和10位数精度的示例:

$ n=20
$ printf 'scale=10; h=0; for(c=1; c<=%d; c++) h+=1/c; h\n' "$n" | bc
3.5977396567

awk示例:

$ awk -v n=20 'END {for(c=1; c<=n; c++) h+=1/c; printf("%.10f\n", h)}' /dev/null
3.5977396571
k97glaaz

k97glaaz2#

{ sequential integers …… } | 

gawk -v PREC=4096 -M -b -e '
BEGIN { 
        FS = "[ ]"
        __ =   ____^=_=_<_
    ______ = ((_____=(__)(____=_))+(++_))^++_+_--
} {      
     $++NF = sprintf("-%s harmonic-# :::\f\b\b\b\b\b\b\b\b%*.*f",
              substr("thstndrd", ( (_+_) * (___=__ % _____) +_\
                                 )^(___~("[" (______)"]")),_+ _), 
                     _____*(++_+_+_)-_,_____*(_+_+--_), ____+=_/__++)

} !(NR % (++_*_++^_+--_+--_*--_*++_*-(++_+_--)+_)*++_____*_____--)'

19 -th harmonic-# :::
                       3.54773965714368191148376906890838779383671024537898
    38 -th harmonic-# :::
                       4.22790201329535034549100970399459275464696303662619
    57 -th harmonic-# :::
                       4.62901321443235344158004104531631012859838417287837
    76 -th harmonic-# :::
                       4.91451352528988945182286719422645852107180801252460
    95 -th harmonic-# :::
                       5.13634648088877496798633442977633275463363512530370
   114 -th harmonic-# :::
                       5.31779366602824331940489375785429719215436642567080
   133 -rd harmonic-# :::
                       5.47131948062047451388417484204751179174503757003868

   152 -nd harmonic-# :::
                       5.60438205256859124059729492050060940921756955152169
   171 -st harmonic-# :::
                       5.72180034814239078142333334509500384180081954683010
   190 -th harmonic-# :::
                       5.82686900761319639743995851624302267172695962079204
   209 -th harmonic-# :::
                       5.92194035359758723502445031447838958087650057597461
   228 -th harmonic-# :::
                       6.00875267325784672513082218206968917640527973621779
   247 -th harmonic-# :::
                       6.08862692711103503609026975467087326974238667122259
   266 -th harmonic-# :::
                       6.16259049517659562890482737718553187367533415196213

   285 -th harmonic-# :::
                       6.23145820517965413743529012227191413848042900786518
   304 -th harmonic-# :::
                       6.29588720143107560851426403660127825953062935530623
   323 -rd harmonic-# :::
                       6.35641517698583421509773989498468070914781600545073
   342 -nd harmonic-# :::
                       6.41348767779890694488766951049252530102843770918596
   361 -st harmonic-# :::
                       6.46747802533945142692080402716830384029025641361459
   380 -th harmonic-# :::
                       6.51870212999540182472245149948920868711983947931950
   399 -th harmonic-# :::
                       6.56742969117650703400815359615575925873243561407876
xzabzqsa

xzabzqsa3#

!/bin/bash/

read -p“输入数字:“n;
hn=1/1;
如果[ $n -eq 1 ],则回显$hn,否则对于((i=2; i〈=$n;i++))做hn= echo "$hn + 1/$i"做fi
回波的第n次谐波数为:$hn”

相关问题