Re: functional programming
- To: mathgroup at smc.vnet.net
- Subject: [mg63017] Re: functional programming
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Sun, 11 Dec 2005 04:56:38 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On 12/10/05 at 6:02 AM, mike_in_england2000 at yahoo.co.uk wrote:
>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}
> ]]
>How could I have rewritten my orginal code using 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}];
Here is one approach
Cases[
Flatten[
Table[{x, k, (5*x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7}, {x, 50},
{k, 50}],
1],
{__, 0}]
{{31, 6, 0}}
Another approach would be
dioph[x_, k_] := (5*x)^2 - 2^k*3*(5 + k)^2 - 131*k - 7
Cases[
Flatten[
Outer[{#1, #2, dioph[#1, #2]}&, Range[50], Range[50]],
1],
{__, 0}]
{{31, 6, 0}}
--
To reply via email subtract one hundred and four