BigInt for swift V3.5 BigIntの関数 数学関数(平方根・階乗)

 

1. 平方根

 

 1) 整数平方根

 

    sqrt(of n: BigInt) -> BigInt

        

          let sq1: BigInt = "144"

          print(BigInt.sqrt(of: sq1))//→ 12

        

          let sq2: BigInt = "165"

          print(BigInt.sqrt(of: sq2))//→ 12

      

          let sq3 : BigInt = "-142"

          print(BigInt.sqrt(of: sq3))//→ エラー

 

    

 

  2) 高桁数の平方根

 

    let a : BigInt = "3147896615353149861913124819883179654931362215326826166852489146"

    let b : BigInt = BigInt.sqrt(of: a)

        print(b)//→ 56106119232692879788808316214799

 

2. 階乗

 

 1) factorial(_ n: Int) -> BigInt

  

   let n : Int = 10

   let m : BigInt = BigInt.factorial(n)

   print(m)  //→3628800

 

 2) 高桁数の階乗

   

  

  let n : Int = 679

        let m : BigInt = BigInt.factorial(n)

        print(m)  //→

5870858947699324831379884147351534458041006654789520050027727325189208038984138188081929146201346125331810774011617277051743003944806996028771711019965482970774034609929603104628344089877933710340238473116574219813837002805411728815505017297468886769643311693900333280248608712634976024961704133080788242936918817347564062694828762466531052021757281255069290400731583084728395089143989217210485109231671990646496441230053146476113980056383549363140271807652067453479647959362433685731422644768772354356784827200662498844987496140477407460471008549923419383014599474512054440683389436590240329137095730719393471444048831095664102349901212697231661094914829950571062807208954105246330269778856270948412156916723268155933778356379562937503690036045222203866255461109401690289673973775000657701855434307090180784750481977445885294633421252166496307377053696694208975499171260992166949746506072876165733797295410683319323690696874607894405400853335967975600870606758008281621815724916234394706020762282168577443688032506130932371324985925466486616301812801183274310798653577106174652359083224197700093970606231024772452671292093492150117775407519346539238924212316351343796532624470940369236227900306733240172985910706925358261360289225199932445735677503098667408201553796055189135463148255575952461894587016664808410211478357787219829267734814494180275282974456378863382363814575041937571581222468980559653644119230197093036422400823403213950748825527984907414929408000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000