![]() |
|
|
#771 | |
|
Aug 2006
597910 Posts |
Quote:
Code:
v=vector(#v-1,i,v[i]) That's a better solution. Last fiddled with by CRGreathouse on 2010-08-23 at 17:57 |
|
|
|
|
|
|
#772 |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
yeah the bad part for pi is unless there's a known lower bound for his idea then he can't use my second idea. though writing it both ways taught me a lot I think.
|
|
|
|
|
|
#773 |
|
Aug 2006
175B16 Posts |
|
|
|
|
|
|
#774 |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
well the reason I can set it to a number other than 0 is because i know the first answer already the reason i can change the lower in the loop to 3 is because i know it won't mess up my loop and ironically it's the next value. so unless you know the first time it comes up and possibly the second as the lower bound for a loop it's hard to use if not impossible.
Last fiddled with by science_man_88 on 2010-08-23 at 18:22 |
|
|
|
|
|
#775 | |
|
Aug 2006
3·1,993 Posts |
Quote:
|
|
|
|
|
|
|
#776 | |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
Quote:
first answer- the first solution to what the problem is. the lower- the smaller of the 2 values to denote a range given to the variable in the loop. next value- the value after the current value. my code is making a vector that contains only primes- no 0 values. |
|
|
|
|
|
|
#777 |
|
Aug 2006
3·1,993 Posts |
OK, and the solution you're comparing it to is when you use primepi(x) to determine exactly how many primes you're going to have, right?
There are ways around that, although they're not as nice. For example, if you have a reasonable upper bound, you can do v=vector(upperbound(x)) and then trim off the remaining values with a technique like I mentioned before. Faiing that, you can resize the vector periodically, then trim the values. If that's too much hassle, you can build up the vector by concatenating, but this is inefficient if the time spent per element is small compared to the number of elements. |
|
|
|
|
|
#778 |
|
"Forget I exist"
Jul 2009
Dumbassville
26·131 Posts |
Code:
v=vector(1000,n,if(isprime(n),n,0));c=vector(1,n,0);for(i=1,1000,if(v[i]!=0,c=concat(c,v[i])));for(i=1,(#c)-1,c[i]=c[i+1]);c=vector(#c-1,i,c[i]) and your pm. Last fiddled with by science_man_88 on 2010-08-23 at 19:10 |
|
|
|
|
|
#779 |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
1/3 times I tested each my 2 idea gave 47 ms for the latter it gave me 46 47 and 63 if you average these 3 you get it's slower I know why to get the same output you need to remove all the 0's so it takes more operations. which take longer especially if my fan starts running. so my first was faster and could store more primes as it didn't waste a entry of a 0 each time it wasn't prime hence 8771 0 removals in the extra time. I tested until 10000 for each.
Last fiddled with by science_man_88 on 2010-08-23 at 19:59 |
|
|
|
|
|
#780 |
|
Aug 2006
3×1,993 Posts |
Your code, slightly generalized:
Code:
sm(lim)={
my(v=vector(lim,n,if(isprime(n),n,0)),c=vector(1,n,0));
for(i=1,lim,
if(v[i]!=0,c=concat(c,v[i]))
);
for(i=1,(#c)-1,
c[i]=c[i+1]
);
vector(#c-1,i,c[i])
};
Code:
crg(lim)={
my(v=vector(primepi(lim)),i=0);
forprime(p=2,lim,v[i++]=p);
v
};
|
|
|
|
|
|
#781 |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
I mean the code I based on what you said versus my original with the 2 change.
on mine the first can be as much as 33% slower. |
|
|
|
![]() |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Why do I sometimes see all the <> formatting commands when I quote or edit? | cheesehead | Forum Feedback | 3 | 2013-05-25 12:56 |
| Passing commands to PARI on Windows | James Heinrich | Software | 2 | 2012-05-13 19:19 |
| Ubiquity commands | Mini-Geek | Aliquot Sequences | 1 | 2009-09-22 19:33 |
| 64-bit Pari? | CRGreathouse | Software | 2 | 2009-03-13 04:22 |
| Are these commands correct? | jasong | Linux | 2 | 2007-10-18 23:40 |