       Re: functional programming

• To: mathgroup at smc.vnet.net
• Subject: [mg63025] Re: functional programming
• From: Peter Pein <petsie at dordos.net>
• Date: Sun, 11 Dec 2005 04:56:47 -0500 (EST)
• References: <dnecum\$t4e\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

mike_in_england2000 at yahoo.co.uk schrieb:
> 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
>
Hi Mike,

one possibility is, to do some postprocessing:

tb = Table[dioph[x, k], {x, 30, 40}, {k, 0, 10}];
Flatten[tb, 1] /. Null -> Sequence[]
--> {{31, 6}}

Peter

• Prev by Date: Re: Re: Zero or one
• Next by Date: Re: functional programming
• Previous by thread: Re: functional programming
• Next by thread: Re: functional programming