mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   PARI/GP (https://www.mersenneforum.org/forumdisplay.php?f=155)
-   -   PARI's commands (https://www.mersenneforum.org/showthread.php?t=13636)

CRGreathouse 2010-08-23 17:57

[QUOTE=science_man_88;226679][CODE]v=vector(1,n,0);for(x=1,10,if(isprime(x),v=concat(v,x)));for(i=1,(#v)-1,v[i]=v[i+1])[/CODE]

there's one thing I still can't do and that's take off the end that repeats so no number repeats.[/QUOTE]

Yes, vecextract could be used here. I'd just use vector, to be honest:
[code]v=vector(#v-1,i,v[i])[/code]
replaces v with v without the last element.

[QUOTE=science_man_88;226681]made a code that works by replacing the 0 with 2 and starting at 3 and searching then I didn't need the index shift code.[/QUOTE]

That's a better solution.

science_man_88 2010-08-23 18:05

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.

CRGreathouse 2010-08-23 18:17

[QUOTE=science_man_88;226697]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.[/QUOTE]

Can you explain this in more detail? What does he need a lower bound on?

science_man_88 2010-08-23 18:21

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.

CRGreathouse 2010-08-23 18:34

[QUOTE=science_man_88;226700]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.[/QUOTE]

Whoa, man, slow down. Define the problem for me, then what you're doing. Right now I don't know what "it", "the first answer", "the lower", or the "next value" are.

science_man_88 2010-08-23 18:42

[QUOTE=CRGreathouse;226702]Whoa, man, slow down. Define the problem for me, then what you're doing. Right now I don't know what "it", "the first answer", "the lower", or the "next value" are.[/QUOTE]

it-the first number put in the vector to initialize it.
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.

CRGreathouse 2010-08-23 18:58

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.

science_man_88 2010-08-23 19:09

[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])[/CODE]

this what i came up with on your advice earlier on in the thread.

and your pm.

science_man_88 2010-08-23 19:51

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.

CRGreathouse 2010-08-23 20:37

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]

My code:
[code]crg(lim)={
my(v=vector(primepi(lim)),i=0);
forprime(p=2,lim,v[i++]=p);
v
};[/code]

Your code takes several minutes (hasn't finished by the time I posted this) to give the primes up to a million. Mine takes 187 milliseconds. The built-in primes() command is almost instantaneous, maybe 1 millisecond.

science_man_88 2010-08-23 20:39

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.


All times are UTC. The time now is 23:09.

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