MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: functional programming

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63016] Re: [mg62981] functional programming
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Sun, 11 Dec 2005 04:56:37 -0500 (EST)
  • Reply-to: hanlonr at cox.net
  • Sender: owner-wri-mathgroup at wolfram.com

Do works fine in this case

Clear[dioph1,dioph2];

dioph1[x_,k_]:=
    (5 x)^2-2^k*3*(5+k)^2-131*k-7;

dioph2[x_,k_]:=
    If[(5 x)^2-2^k*3*(5+k)^2-131*k-7==0,
      Sow[{x,k}]];

n=400;

Reap[Do[If[dioph1[x,k]==0,Sow[{x,k}]],
        {x,0,n},{k,0,n}]][[-1,1]]//Timing

{7.1011679999999995*Second, {{31, 6}}}

Reap[Do[dioph2[x,k],
        {x,0,n},{k,0,n}]][[-1,1]]//Timing

{7.551326000000001*Second, {{31, 6}}}

Reap[Table[dioph2[x,k],
        {x,0,n},{k,0,n}]][[-1,1]]//Timing

{7.670505999999996*Second, {{31, 6}}}


Bob Hanlon

> 
> From: mike_in_england2000 at yahoo.co.uk
To: mathgroup at smc.vnet.net
> Date: 2005/12/10 Sat AM 06:02:50 EST
> Subject: [mg63016] [mg62981] functional programming
> 
> Hi All
> 
> As a long time C++ programmer I am terrible at functional programming.
> I recently replied to a post on here concerning diophantine equations
> and provided the following code:
> 
> dioph[x_, k_] := (5 x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7
> Reap[
>   Do[
>     Do[
>       If[dioph[x, k] == 0, Sow[{x, k}];];
>       , {x, 0, 1000}
>       ]
>     , {k, 0, 1000}
>     ]]
> 
> Someone else came up with a more efficient way of doing this and it was
> a nice solution.  However my question is this - How could I have
> rewritten my orginal code using Functional programming?  Nothing fancy
> - just a literal translation using the ideas of functional programming.
> 
> I tried
> 
> dioph[x_, k_] := If[(5 x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7 == 0, {x,
> k}];
> Table[dioph[x, k], {x, 0, 10}, {k, 0, 10}];
> 
> which is similar to something that was posted on here a while back but
> I hit the same problem they did - The If statement returns Null when
> the statement is false so you end up with large lists of Nulls.  Not
> good if I want to look at large ranges of numbers.
> 
> Any advice would be welcomed 
> Thanks,
> Mike
> 
> 


  • Prev by Date: Re: Re: Using The Random Function to predict Things
  • Next by Date: Re: Assign a value to a variable
  • Previous by thread: Re: functional programming
  • Next by thread: color points in TableForm