Karim Belabas on Sat, 16 Sep 2017 17:10:42 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: how to define a function with loop


* Elim Qiu [2017-09-16 16:56]:
> I'd like to define a function returns the decimal approximation of
> 
> (1^n+2^n+...+n^n)/(n^n)
> 
> In python i have
> 
> def tm(n,k):
>     return exp(n*log((k+0.0)/n))
> 
> def sm(n,m):
>     s = 0.0
>     for j in range(m):
>         k = n -j
>         s += tm(n,k)
>     return s
> 
> How do I put a loop inside of a definition of pari function?

  sm(n,m) =
  { my(s = 1);
    for (j = 1, m,
      s += exp(n * log((n-j)/n))
    );
    return(s);
  }

  (17:08) gp > \p100
    realprecision = 115 significant digits (100 digits displayed)
  (17:09) gp > sm(10^5,10^5-1)
   time = 2,546 ms.
   %2 = 1.581966745495314367289816713125314892028479147529156465377909025096871544708934025943474991810037609


Cheers,

    K.B.
--
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite de Bordeaux         Fax: (+33) (0)5 40 00 21 23
351, cours de la Liberation    http://www.math.u-bordeaux.fr/~kbelabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux.fr/  [PARI/GP]
`