![]() |
[QUOTE=science_man_88;252333]because I want to try and make one that works without that so it's not limited by primelimit.[/QUOTE]
There are very few cases when you'd actually want to do that. In almost all cases the better approach would be to calculate more primes and keep them on hand -- and you can already do this with default(primelimit, 10^9) changing the number as appropriate. But if you insist, here's a really, really, really bad way: [code]primeN(n)={ my(p=1); while(n, if(isprime(p++), n--) ); p };[/code] But please don't do this! It takes 10.9 seconds to do primeN(10^6), compared to 1.5 milliseconds for prime(10^6) -- seven thousand times slower. primeN(10^7) takes several minutes while prime(10^7) gives an answer in ~10 ms. |
[QUOTE=CRGreathouse;252391]There are very few cases when you'd actually want to do that. In almost all cases the better approach would be to calculate more primes and keep them on hand -- and you can already do this with
default(primelimit, 10^9) changing the number as appropriate. But if you insist, here's a really, really, really bad way: [code]primeN(n)={ my(p=1); while(n, if(isprime(p++), n--) ); p };[/code] But please don't do this! It takes 10.9 seconds to do primeN(10^6), compared to 1.5 milliseconds for prime(10^6) -- seven thousand times slower. primeN(10^7) takes several minutes while prime(10^7) gives an answer in ~10 ms.[/QUOTE] fine what ever. want what my minimumprime (x) does ? or have you figured it out ? all it does is x-2 to find out how many to go into the [TEX]6n\pm1[/TEX] figures which one it's on and does a calucation based on where it lands to figure out a n value than it returns [TEX]6n\pm1[/TEX] |
Yes, and it's clear that that won't give the right answer because it's linear and the correct figure is about n log n.
This may be of interest to you: [url]http://primes.utm.edu/nthprime/[/url] |
[QUOTE=CRGreathouse;252414]Yes, and it's clear that that won't give the right answer because it's linear and the correct figure is about n log n.
This may be of interest to you: [url]http://primes.utm.edu/nthprime/[/url][/QUOTE] Since it's built to give the lowest possible value it could be I know it's not correct. I'm just trying to figure a easy way to correct for it. The first time it's wrong is x = 10 last I checked. |
[QUOTE=science_man_88;252416]Since it's built to give the lowest possible value it could be I know it's not correct. I'm just trying to figure a easy way to correct for it. The first time it's wrong is x = 10 last I checked.[/QUOTE]
I gave pretty much the easiest way to do it without using the built-in prime list. There are decently fast methods that use the prime list but avoid the prime() command itself, if you're interested. For example, [code]primeN(n)={ my(v=primes(n)); v[#v] };[/code] and [code]primeN(n)={ if(n<6, return([2,3,5,7,11][n])); my(l=log(n), lower=ceil(n*l+n*(log(l)-1)), upper=floor(n*l+n*log(l)), t); while(upper - lower > 1, if(primepi(t = (lower + upper) \ 2) < n, lower = t, upper = t) ); if(primepi(lower) == n, lower, upper) };[/code] These might be a hundred times slower rather than 10,000 times slower. |
By the way, I think that re-implementing basic features of a language is a good way to learn. Working through Prime Numbers: A Computational Perspective really helped me to understand how the various algorithms fit together. So I don't think there's anything wrong with doing this -- I just want you to code it, learn, and then use the built-in functions rather than your own. :smile:
|
[QUOTE=CRGreathouse;252426]By the way, I think that re-implementing basic features of a language is a good way to learn. Working through Prime Numbers: A Computational Perspective really helped me to understand how the various algorithms fit together. So I don't think there's anything wrong with doing this -- I just want you to code it, learn, and then use the built-in functions rather than your own. :smile:[/QUOTE]
yeah bad part is I'd be lucky to program all the things my pocket calculator can do. I have an idea for one thing I haven't done before mainly because it's like the base converter. |
[QUOTE=science_man_88;252634]yeah bad part is I'd be lucky to program all the things my pocket calculator can do. I have an idea for one thing I haven't done before mainly because it's like the base converter.[/QUOTE]
Actually that's a good project because PARI doesn't know how to do that on its own. I mean, it inputs and displays numbers in decimal while storing and calculating in binary, and it can return a number in binary (as a vector) if requested, but it doesn't have generic base-conversion routines. |
[QUOTE=CRGreathouse;252671]Actually that's a good project because PARI doesn't know how to do that on its own. I mean, it inputs and displays numbers in decimal while storing and calculating in binary, and it can return a number in binary (as a vector) if requested, but it doesn't have generic base-conversion routines.[/QUOTE]
we made the base converter last I checked, a list of what my calculator can do: [QUOTE]list of scientific constant (not sure what all of them are) unit conversion(40 in total, but I think I could cut that to 20) powers (not so hard in PARI) base 10 and natural logarithms (not so hard) some SI prefixes ( mega,giga,tera,micro,milli,kilo,fempto,pico,nano) combinations and permutations trig functions xth roots ( pretty much done) convert answer to fraction if possible. insert function. 9 variable storage. Pol( and Rec( (haven't used them but I'm pretty sure I know what they do). integrals ? "random" numbers rounding ( not hard) factorial Pi a+bi ( seen the equation before don't know what it is) percentage [TEX]Re\leftrightarrow lm[/TEX] degrees rad and grad base conversion( DEC,HEX,BIN,OCT)[/QUOTE] modes are : [QUOTE]COMP,CMPLX,SD,REG,BASE,EQN,MAT,VCT,Deg,Rad,Gra,Fix,Sci,Norm,Disp[/QUOTE] |
Most of those are of course already in GP. If you want to implement some of the others, give it a shot and I'll take a look.
|
[QUOTE=CRGreathouse;252703]Most of those are of course already in GP. If you want to implement some of the others, give it a shot and I'll take a look.[/QUOTE]
the conversion among units is basically a base conversion with each digit in the number as a different conversion base. I'm not completely sure which others aren't. |
| All times are UTC. The time now is 23:00. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.