mersenneforum.org  

Go Back   mersenneforum.org > Math Stuff > Computer Science & Computational Number Theory > PARI/GP

Reply
 
Thread Tools
Old 2010-08-23, 17:57   #771
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

597910 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
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])
there's one thing I still can't do and that's take off the end that repeats so no number repeats.
Yes, vecextract could be used here. I'd just use vector, to be honest:
Code:
v=vector(#v-1,i,v[i])
replaces v with v without the last element.

Quote:
Originally Posted by science_man_88 View Post
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.
That's a better solution.

Last fiddled with by CRGreathouse on 2010-08-23 at 17:57
CRGreathouse is offline   Reply With Quote
Old 2010-08-23, 18:05   #772
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

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.
science_man_88 is offline   Reply With Quote
Old 2010-08-23, 18:17   #773
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

175B16 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
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.
Can you explain this in more detail? What does he need a lower bound on?
CRGreathouse is offline   Reply With Quote
Old 2010-08-23, 18:21   #774
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

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
science_man_88 is offline   Reply With Quote
Old 2010-08-23, 18:34   #775
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
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.
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.
CRGreathouse is offline   Reply With Quote
Old 2010-08-23, 18:42   #776
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
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.
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.
science_man_88 is offline   Reply With Quote
Old 2010-08-23, 18:58   #777
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

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.
CRGreathouse is offline   Reply With Quote
Old 2010-08-23, 19:09   #778
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

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])
this what i came up with on your advice earlier on in the thread.

and your pm.

Last fiddled with by science_man_88 on 2010-08-23 at 19:10
science_man_88 is offline   Reply With Quote
Old 2010-08-23, 19:51   #779
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

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
science_man_88 is offline   Reply With Quote
Old 2010-08-23, 20:37   #780
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

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])
};
My code:
Code:
crg(lim)={
  my(v=vector(primepi(lim)),i=0);
  forprime(p=2,lim,v[i++]=p);
  v
};
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.
CRGreathouse is offline   Reply With Quote
Old 2010-08-23, 20:39   #781
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

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.
science_man_88 is offline   Reply With Quote
Reply



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

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


Fri Aug 6 23:07:26 UTC 2021 up 14 days, 17:36, 1 user, load averages: 3.82, 3.85, 3.90

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.