Re: functional programming

*To*: mathgroup at smc.vnet.net*Subject*: [mg63013] Re: functional programming*From*: dh <dh at metrohm.ch>*Date*: Sun, 11 Dec 2005 04:56:35 -0500 (EST)*Organization*: TDC Switzerland AG, http://www.sunrise.ch*References*: <dnecum$t4e$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Hi Mike, You do not need to make a table if you only want a few elements. E.g.: instead of: Table[dioph[x, k], {x, 0, 10}, {k, 0, 10}]; try: res={}; Do[If[(t=dioph[x, k])!=Null,AppendTo[res,t], {x, 0, 10}, {k, 0, 10}]; Daniel mike_in_england2000 at yahoo.co.uk wrote: > 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 >