![]() |
|
|
#12 | |
|
Aug 2006
3·1,993 Posts |
Quote:
Code:
forvec(v=vector(4,i,[1,5]), print(fromdigits(apply(n->2*n-1, v)))) |
|
|
|
|
|
|
#13 |
|
"Rashid Naimi"
Oct 2015
Remote to Here/There
40178 Posts |
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. |
|
|
|
|
|
#14 | ||
|
Aug 2006
3×1,993 Posts |
Quote:
![]() 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:
forvec(v=[ [start1,end1], [start2,end2], ...], v ) 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:
forvec(v=vector(4,i,[0,9]), f(v[1],v[2],v[3],v[4]), 2) Code:
forvec(v=vector(4,i,[0,9]), call(f,v), 2) 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)))) |
||
|
|
|
|
|
#15 |
|
Aug 2006
175B16 Posts |
|
|
|
|
|
|
#16 |
|
"Rashid Naimi"
Oct 2015
Remote to Here/There
2,063 Posts |
Thank you for the very detailed explanation CRG. Much appreciated.
|
|
|
|
|
|
#17 |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
|
|
|
|
![]() |
| Thread Tools | |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What does my result mean? | Unregistered | Information & Answers | 6 | 2013-04-16 21:52 |
| Odd result | 1997rj7 | PrimeNet | 2 | 2009-12-04 08:48 |
| Combination Formula? | drake2 | Math | 10 | 2006-12-08 22:43 |
| LLT numbers, linkd with Mersenne and Fermat numbers | T.Rex | Math | 4 | 2005-05-07 08:25 |
| New Result | R.D. Silverman | NFSNET Discussion | 1 | 2005-04-19 23:45 |