MathGroup Archive 2005

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

Search the Archive

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: The right context to declare a symbol
  • Next by Date: Re: Zero or one
  • Previous by thread: Re: The right context to declare a symbol
  • Next by thread: Re: functional programming