Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9269 > unrolled thread

How to count

Started byArtur <grafix@csl.pl>
First post2012-03-29 07:56 +0000
Last post2012-04-03 08:55 +0000
Articles 5 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  How to count Artur <grafix@csl.pl> - 2012-03-29 07:56 +0000
    Re: How to count Costa Bravo <q13a27tt@aol.com> - 2012-03-30 09:37 +0000
    Re: How to count danl@wolfram.com - 2012-03-30 09:38 +0000
      Re: How to count Costa Bravo <q13a27tt@aol.com> - 2012-04-01 07:38 +0000
        Re: How to count danl@wolfram.com - 2012-04-03 08:55 +0000

#9269 — How to count

FromArtur <grafix@csl.pl>
Date2012-03-29 07:56 +0000
SubjectHow to count
Message-ID<jl14ig$l5e$1@smc.vnet.net>
Dear Mathematica Gurus!

I have seriously problem with counting distances between smallest square 
bigger than n! (for big n) e.g.

aa = {}; Do[k = 1 + Floor[Sqrt[n!]]; kk = k^2 - n!; Print[kk];
  kkk = N[Log[kk], 100]; Print[kkk];
  AppendTo[aa, kkk], {n, 1, 10000000, 1000000}]; aa

Who have idea how to count this on Mathematica?

Best wishes
Artur

[toc] | [next] | [standalone]


#9302

FromCosta Bravo <q13a27tt@aol.com>
Date2012-03-30 09:37 +0000
Message-ID<jl3utg$3k6$1@smc.vnet.net>
In reply to#9269
Artur wrote:

> I have seriously problem with counting distances between smallest square
> bigger than n! (for big n) e.g.
>
> aa = {}; Do[k = 1 + Floor[Sqrt[n!]]; kk = k^2 - n!; Print[kk];
>    kkk = N[Log[kk], 100]; Print[kkk];
>    AppendTo[aa, kkk], {n, 1, 10000000, 1000000}]; aa
>
> Who have idea how to count this on Mathematica?
>

funa[n_]:=Module[{lg,lg1,ns},
lg=N[LogGamma[n+1]];
lg1=N[LogGamma[n+1],102+Round[lg/2/Log[10]]];
ns=Exp[lg1];
N[Log[(1+Floor[Sqrt[ns]])^2-ns],100]]

aa={};w = 5 (* 6 *);
Do[Print[k,"   ",Timing[kkk=funa[k]]];
AppendTo[aa,kkk],{k, 1 ,10^(w+1),10^w}];

OK ?!

-- 
   Costa

[toc] | [prev] | [next] | [standalone]


#9303

Fromdanl@wolfram.com
Date2012-03-30 09:38 +0000
Message-ID<jl3uuf$3kp$1@smc.vnet.net>
In reply to#9269
On Thursday, March 29, 2012 2:56:00 AM UTC-5, Artur wrote:
> Dear Mathematica Gurus!
>
> I have seriously problem with counting distances between smallest square
> bigger than n! (for big n) e.g.
>
> aa = {}; Do[k = 1 + Floor[Sqrt[n!]]; kk = k^2 - n!; Print[kk];
>   kkk = N[Log[kk], 100]; Print[kkk];
>   AppendTo[aa, kkk], {n, 1, 10000000, 1000000}]; aa
>
> Who have idea how to count this on Mathematica?
>
> Best wishes
> Artur

It is likely to be slow no matter what you do. Here are a couple of approaches though I've not attempted to verify results and so I cannot be sure precision is sufficiently high. I show with numbers a factor of 10 smaller than yours, so expect your examples to be correspondingly slower.

bottom = 100000;
Timing[bb = Table[Ceiling[Exp[LogGamma[N[n+1,n]]/2]],
  {n,bottom,10*bottom,bottom}];]

Out[16]= {671.196962, Null}

Around 30% faster:

sqrtFactorial[n_] := Module[
  {start, x},
  start = Ceiling[Exp[LogGamma[N[n+1]]/2]];
  Ceiling[Exp[x] /. FindRoot[x==LogGamma[n+1]/2, {x,start},
    WorkingPrecision->3*n, AccuracyGoal->n, PrecisionGoal->n]]
  ]
Timing[dd = Table[sqrtFactorial[n],  {n,bottom,10*bottom,bottom}];]

Out[26]= {463.363558, Null}

In[27]:= dd===bb
Out[27]= True

I will check that at least the last one is correct.

In[28]:= dd[[-1]]^2 > (10*bottom)!
Out[28]= True

In[29]:= (dd[[-1]]-1)^2 < (10*bottom)!
Out[29]= True


Daniel Lichtblau
Wolfram Research

[toc] | [prev] | [next] | [standalone]


#9336

FromCosta Bravo <q13a27tt@aol.com>
Date2012-04-01 07:38 +0000
Message-ID<jl90l4$mpf$1@smc.vnet.net>
In reply to#9303
Daniel Lichtblau Wolfram Research   danl@wolfram.com wrote:

> bottom = 100000;
> Timing[bb = Table[Ceiling[Exp[LogGamma[N[n+1,n]]/2]],
>    {n,bottom,10*bottom,bottom}];]
>
> Out[16]= {671.196962, Null}

My computer needs more time
     {1256.916, Null}

Let's test a better solution

n = 100000;
{Timing[lg2 = Exp[LogGamma[N[n + 1, n]]/2];], Precision[lg2]}
{Timing[lg2 = N[Exp[LogGamma[n + 1]/2], n];], Precision[lg2]}
{Timing[lg2 = Exp[N[LogGamma[n + 1]/2, n]];], Precision[lg2]}

{{9.048, Null}, 556568.58}

{{3.338, Null}, 100000.}

{{0.593, Null}, 99994.11}    (* !!! *)

surprising result for n = 1 000 000

n = 1000000;
{Timing[lg2 = Exp[LogGamma[N[n + 1, n]]/2];], Precision[lg2]}
{Timing[lg2 = N[Exp[LogGamma[n + 1]/2], n];], Precision[lg2]}
{Timing[lg2 = Exp[N[LogGamma[n + 1]/2, n]];], Precision[lg2]}

{{218.557, Null}, 6.5657032*10^6}

{{217.528, Null}, 1.*10^6}  (* !!! *)

{{27.877, Null}, 999993.19}

The new version of the original expression

bottom = 10^5;
Timing[bb1 =
    Table[Ceiling[Exp[N[LogGamma[n + 1]/2, n]]], {n, bottom, 10*bottom,
       bottom}];]

{126.034, Null}

10 times faster !!

--
   Costa

[toc] | [prev] | [next] | [standalone]


#9388

Fromdanl@wolfram.com
Date2012-04-03 08:55 +0000
Message-ID<jledt9$h4k$1@smc.vnet.net>
In reply to#9336
On Sunday, April 1, 2012 2:38:12 AM UTC-5, Costa Bravo wrote:
> Daniel Lichtblau Wolfram Research
> 
> > bottom = 100000;
> > Timing[bb = Table[Ceiling[Exp[LogGamma[N[n+1,n]]/2]],
> >    {n,bottom,10*bottom,bottom}];]
> >
> > Out[16]= {671.196962, Null}
> 
> My computer needs more time
>      {1256.916, Null}
> 
> Let's test a better solution
> 
> n = 100000;
> {Timing[lg2 = Exp[LogGamma[N[n + 1, n]]/2];], Precision[lg2]}
> {Timing[lg2 = N[Exp[LogGamma[n + 1]/2], n];], Precision[lg2]}
> {Timing[lg2 = Exp[N[LogGamma[n + 1]/2, n]];], Precision[lg2]}
> 
> {{9.048, Null}, 556568.58}
> 
> {{3.338, Null}, 100000.}
> 
> {{0.593, Null}, 99994.11}    (* !!! *)
> 
> surprising result for n = 1 000 000
> 
> n = 1000000;
> {Timing[lg2 = Exp[LogGamma[N[n + 1, n]]/2];], Precision[lg2]}
> {Timing[lg2 = N[Exp[LogGamma[n + 1]/2], n];], Precision[lg2]}
> {Timing[lg2 = Exp[N[LogGamma[n + 1]/2, n]];], Precision[lg2]}
> 
> {{218.557, Null}, 6.5657032*10^6}
> 
> {{217.528, Null}, 1.*10^6}  (* !!! *)
> 
> {{27.877, Null}, 999993.19}
> 
> The new version of the original expression
> 
> bottom = 10^5;
> Timing[bb1 =
>     Table[Ceiling[Exp[N[LogGamma[n + 1]/2, n]]], {n, bottom, 10*bottom,
>        bottom}];]
> 
> {126.034, Null}
> 
> 10 times faster !!
> 
> --
>    Costa

Nice ketch.

Daniel Lichtblau
Wolfram Research

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web