mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   Math (https://www.mersenneforum.org/forumdisplay.php?f=8)
-   -   result of combination ( few numbers) (https://www.mersenneforum.org/showthread.php?t=22330)

CRGreathouse 2017-05-23 02:29

[QUOTE=a1call;459555]Not most efficient code but it should work.

[CODE]theCounter = 19
theOldString = ""
forstep(i1=1,9,2,{
forstep(i2=1,9,2,
forstep(i3=1,9,2,
forstep(i4=1,9,2,
forstep(i5=1,9,2,
theString = Str(i1,i2,i3,i4,i5) ;
if (theOldString != theString ,
theOldString = theString;
print(theString );
);
theCounter = theCounter -1;
if (theCounter <1, next(19););
);
);
);
);
})

[/CODE][/QUOTE]

You can do

[code]forvec(v=vector(4,i,[1,5]), print(fromdigits(apply(n->2*n-1, v))))[/code]

where forvec does the work of the five loops, apply converts 1..5 to 1,3,5,7,9, and fromdigits converts [1,3,5,7] to 1357. (The base is 10 unless you give a different base as a second argument.)

a1call 2017-05-23 02:48

I have never been able to make forvec work before. I find the documentation vague without any examples. I will come back to your example next time I need it.
Apply and fromdigits is news to me.
For 0,1,3,5,7,9 , wouldn't it be easier to include them in the vector rather than formulate a conversion?
Thank you for code.

CRGreathouse 2017-05-23 03:31

[QUOTE=a1call;459572]I have never been able to make forvec work before. I find the documentation vague without any examples.[/QUOTE]

Yes, the forvec documentation is bad. :down:

The basic idea is that you can replace a collection of nested for loops like this:

[CODE]for(i1=start1,end1,
for(i2=start2,end2,
...
[i1, i2, ...]
...
)
)[/CODE]

with

[code]forvec(v=[ [start1,end1], [start2,end2], ...],
v
)[/code]

There is an optional final argument that if set to 1 gives only weakly increasing vectors ([1, 1, 2] but not [2, 1, 2]), and if set to 2 gives only strictly increasing vectors ([1, 2, 3] but not [1, 2, 2]). So you can replace
[code]for(a=0,6, for(b=a+1,7, for(c=b+1,8, for(d=c+1,9, f(a,b,c,d)))))[/code]
with
[code]forvec(v=vector(4,i,[0,9]), f(v[1],v[2],v[3],v[4]), 2)[/code]
which can be abbreviated
[code]forvec(v=vector(4,i,[0,9]), call(f,v), 2)[/code]

[QUOTE=a1call;459572]For 0,1,3,5,7,9 , wouldn't it be easier to include them in the vector rather than formulate a conversion?[/QUOTE]

For me it would be easier and more natural to use a vector rather than gluing them together as a decimal number, but I wanted to match your output.

[QUOTE=a1call;459572]Apply and fromdigits is news to me.[/QUOTE]

fromdigits is boring -- fromdigits([1, 2, 3]) is 123 and fromdigits([1, 0, 1], 2) is 5 (binary).

apply is great, I use it all the time. apply(f, [1, 2, 3]) returns [f(1), f(2), f(3)], where f is some function. (Of course the function is computed, it doesn't actually write "f(1)" unless that's what the function does.) apply(cos, [0, Pi/2, Pi]) gives approximately [1, 0, -1].

In my example I used a 'lambda' type expression to define an anonymous function. Instead I could have written
[code]doubleMinusOne(n)=2*n-1;
forvec(v=vector(4,i,[1,5]), print(fromdigits(apply(doubleMinusOne, v))))[/code]
but this way I don't have to think up a name for this little function that I'm only using in this one spot.

CRGreathouse 2017-05-23 03:33

[QUOTE=CRGreathouse;459574]Yes, the forvec documentation is bad. :down:[/QUOTE]

Also, I think scienceman88 uses forvec a lot, he might have more useful examples if mine don't help. :smile:

a1call 2017-05-23 04:11

Thank you for the very detailed explanation [B]CRG[/B]. Much appreciated.:smile:

science_man_88 2017-05-23 10:27

[QUOTE=CRGreathouse;459575]Also, I think scienceman88 uses forvec a lot, he might have more useful examples if mine don't help. :smile:[/QUOTE]

I actually don't even use fold as much as I should I did implement Jordan's totient function at one point ( see my pastebin).


All times are UTC. The time now is 15:52.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.