![]() |
[QUOTE=CRGreathouse;435841]Welcome back! I wondered where you were.
I recommend closures rather than strings for arbitrary code. Much better to do [code]runThis(f, n)=f(n); runThis(x->print(x), 7)[/code] than [code]runThis(s,n)=local(x=n); eval(s); runThis("print(x)", 7)[/code] This incidentally removes the need for local(). As for your particular case, isn't this what forstep is all about? [code]forstep_sm88(code, startAt, endAt, step)= { forstep(n=startAt,endAt,step, code(n)); } forstep_sm88(n->if(isprime(n), print(n)), 1, 100, 4) \\ prints primes which are 1 mod 4[/code][/QUOTE] well this one is different than the normal gp forstep in that it can be altered on the fly if you replace a few things in the code given ( as per the notes) you can alter this one's number of steps so you could start out with a step vector of length one and grow that to say 65 over time, you could also start out with steps in that vector being say [1,2,3,5] and end up with a step vector of [539,540,541,543] or maybe with multiplication getting involved even higher. |
[QUOTE=science_man_88;435842]well this one is different than the normal gp forstep in that it can be altered on the fly if you replace a few things in the code given ( as per the notes) you can alter this one's number of steps so you could start out with a step vector of length one and grow that to say 65 over time, you could also start out with steps in that vector being say [1,2,3,5] and end up with a step vector of [539,540,541,543] or maybe with multiplication getting involved even higher.[/QUOTE]
I guess I just don't see the use case. What problem would you be solving with that function? |
[QUOTE=CRGreathouse;435843]I guess I just don't see the use case. What problem would you be solving with that function?[/QUOTE]
I was mostly just playing around but I could see it being used if you say could jump over more k for TF testing of mersennes etc. ( once k=1 is eliminated so are a whole list of k values like k=2p+2,4p+3,...,2np+n+1 ) etc. |
Hi,
* Is there a way to abort/quit/break a function based on time? In particular I would like to abort factor(n), and go on to the next item if n takes too long to factor. An alternative would be to limit factoring to to small primes. Thank you in advance. |
[QUOTE=a1call;439770]Hi,
* Is there a way to abort/quit/break a function based on time? In particular I would like to abort factor(n), and go on to the next item if n takes too long to factor. An alternative would be to limit factoring to to small primes. Thank you in advance.[/QUOTE] factor has a lim variable you can set, there's alarm to count the time you can either store the result in a variable or use iferr to catch the alarm type error it throws. etc. |
Thank you science man,
I knew you would pull through for me.:smile: Somehow I missed the Lim description in the manual (if any), but it's clearly described in the "?factor" help, now that I checked. Thanks again. |
Hi,
on my calculator I get 2^34 =1.717986918*10^10 with the last digit (4) truncated I know I can reproduce this with a lot of functions such as divide by 10^10 and truncate. Is there a way to do this easier. In other words give results which are not arbitrary large but rather based on significant digits. It sounds like precision () should do this but I can't make it work. Thanks in advance |
[QUOTE=a1call;440008]Hi,
on my calculator I get 2^34 =1.717986918*10^10 with the last digit (4) truncated I know I can reproduce this with a lot of functions such as divide by 10^10 and truncate. Is there a way to do this easier. In other words give results which are not arbitrary large but rather based on significant digits. It sounds like precision () should do this but I can't make it work. Thanks in advance[/QUOTE] closest thing I can think of is using bestappr with a power of 10 as the maximum of the denominator but then you still can get more exact than is needed ( and I guess this doesn't really work for int examples. |
Thank you for the reply Science man. I just saw your bestappr suggestion for my similar question in another thread.
I find it disappointing that such a feature that should probably be extremely easy to be programmed in is completely left out. After all the C engine should give results based on significant digits which is painstakingly converted to arbitrary precision. Such a function would be quite valuable for size comparison of values which would be too large (and unnecessary) to be computed with arbitrary precision. :featurebug: |
[QUOTE=a1call;440008]Hi,
on my calculator I get 2^34 =1.717986918*10^10 with the last digit (4) truncated I know I can reproduce this with a lot of functions such as divide by 10^10 and truncate. Is there a way to do this easier. In other words give results which are not arbitrary large but rather based on significant digits. It sounds like precision () should do this but I can't make it work. Thanks in advance[/QUOTE] Not clear what calculator, and what are you trying to do. Is that Pari/gp? Or windows calculator? Or an external device, pocket calculator? If you want to write 2^a=b*10^x, where x and b are unknown, you log10 everything and you have a*lg(2)=lg(b)+x*lg(10), where lg is the log10, or the log in base 10. From how the [U]normalized[/U] floating points are formed, you know that b has to be 1<=b<10 (it must have a [U]single[/U] significant digit before decimal dot), and x has to be integer. Therefore you have a*lg(2)=lg(b)+x, and you know that x is integer, so you take the int[] part, and the rest (fract part) is the log10 of b. On your example - I use windows calculator, typing in order, 3, 4, *, 2, log, (it shows 0.30102999566398119521373889472449), =, (it shows 10.235019852575360637267122420633). This is the result of 34*log10(2). Then x is 10, because it has to be integer, and lg(b) is 0.235....blah...blah... So (the calculator still shows 10.235019852575360637267122420633) I continue, pressing: - (minus, I intend to subtract), Int, (to take integer part), = (to do the subtraction), and I have the fractional part displayed. That is lg(b), or log in base 10 of b. One more press for "10^x" button and that is your result, i.e. b. The result is exact (1.7179869184), and all this calculus never uses more than 64 bits. Of course, you can substitute the decimal logarithm with natural logarithm (some calculators won't have the former), and use "e^x" instead of "10^x", in this case you have to press one more button to get the right exponent. I only pressed 10 buttons, and it takes less than few seconds. [edit: these types of approximations... that is what the logarithm tables are for! Some of us grown up with them, I still remember the books with logarithm tables, we could look in the book and add them in our minds, then look to the reverse function, and have the approximate product faster than we could do the product with pencil and paper] |
it refers to calculations on a cheap pocket calculator.
Here is a numerical example: Say you want to know if 1000000!>2^20000000 very few significant digits is sufficient to find out since 1000000!=8.264*10^5565708 and 2^20000000=8.19x10^6020599 Try doing this comparison on Pari-GP without allocating very large amounts of memory which is quite unnecessary for the comparison. In fact like I mentioned you can painstakingly recreate the operation in Pari by truncating and keeping track of number of digits. ETA: I may or may not have errors in caculation of number of digits, but I think the numerical example is sufficient to describe the concept. |
| All times are UTC. The time now is 06:54. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.