mersenneforum.org  

Go Back   mersenneforum.org > Great Internet Mersenne Prime Search > Math

Reply
 
Thread Tools
Old 2017-05-23, 02:29   #12
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by a1call View Post
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););
        );
      );
    );
  );
})
You can do

Code:
forvec(v=vector(4,i,[1,5]), print(fromdigits(apply(n->2*n-1, v))))
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.)
CRGreathouse is offline   Reply With Quote
Old 2017-05-23, 02:48   #13
a1call
 
a1call's Avatar
 
"Rashid Naimi"
Oct 2015
Remote to Here/There

40178 Posts
Default

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.
a1call is offline   Reply With Quote
Old 2017-05-23, 03:31   #14
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by a1call View Post
I have never been able to make forvec work before. I find the documentation vague without any examples.
Yes, the forvec documentation is bad.

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, ...]
    ...
  )
)
with

Code:
forvec(v=[ [start1,end1], [start2,end2], ...],
  v
)
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)))))
with
Code:
forvec(v=vector(4,i,[0,9]), f(v[1],v[2],v[3],v[4]), 2)
which can be abbreviated
Code:
forvec(v=vector(4,i,[0,9]), call(f,v), 2)
Quote:
Originally Posted by a1call View Post
For 0,1,3,5,7,9 , wouldn't it be easier to include them in the vector rather than formulate a conversion?
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:
Originally Posted by a1call View Post
Apply and fromdigits is news to me.
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))))
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 is offline   Reply With Quote
Old 2017-05-23, 03:33   #15
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

175B16 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Yes, the forvec documentation is bad.
Also, I think scienceman88 uses forvec a lot, he might have more useful examples if mine don't help.
CRGreathouse is offline   Reply With Quote
Old 2017-05-23, 04:11   #16
a1call
 
a1call's Avatar
 
"Rashid Naimi"
Oct 2015
Remote to Here/There

2,063 Posts
Default

Thank you for the very detailed explanation CRG. Much appreciated.
a1call is offline   Reply With Quote
Old 2017-05-23, 10:27   #17
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Also, I think scienceman88 uses forvec a lot, he might have more useful examples if mine don't help.
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).
science_man_88 is offline   Reply With Quote
Reply

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

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


Mon Aug 2 15:52:05 UTC 2021 up 10 days, 10:21, 0 users, load averages: 2.63, 2.29, 2.30

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

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.