![]() |
[CODE]c=vector(10, n, n)
vecextract(c, concat("1..", #c-1)) vecextract(c, concat("2..", #c))[/CODE] |
[CODE]c=concat(c,c[1]);c=vector(#c-1,n,c[n+1])[/CODE]
I think a couple of this add-on to the basics would be nice as 2 of them could possibly be used to crack a basic shift cipher. |
[QUOTE=axn;241984][CODE]c=vector(10, n, n)
vecextract(c, concat("1..", #c-1)) vecextract(c, concat("2..", #c))[/CODE][/QUOTE] didn't know I could do that syntax. |
[QUOTE=CRGreathouse;241880]How about
[code]primesUPTO(x)=primes(primepi(x));[/code] ? Also: I'm proud of you for making the function return a vector instead of just printing values. You're learning![/QUOTE] Meh. I don't quite know the code to make a custom vector that returns something other than 1 and 0, other than insert code into the vector command. |
[QUOTE=3.14159;242066]Meh. I don't quite know the code to make a custom vector that returns something other than 1 and 0, other than insert code into the vector command.[/QUOTE]
what exactly do you want to return ? [CODE]x=vector(100,n,n) ; makes x have 100 indexes with each indexes value equals to the index.[/CODE] [CODE]x=vector(100,n,if(isprime(n,n))) ; makes a vector of 100 indexes with the index value equal to the index if the index value is prime. otherwise the index value is 0.[/CODE] |
[QUOTE=3.14159;242066]Meh. I don't quite know the code to make a custom vector that returns something other than 1 and 0, other than insert code into the vector command.[/QUOTE]
Well, you need to make a vector, either from scratch with the vector command or by converting something to a vector with Vec. Once you have that you can put in values at appropriate indices as needed, then return it. Certainly vectors aren't appropriate for all tasks, but it's good to use them when they are. ---- Here's a trick* I just learned. When you want to build a vector element-by-element, a better way than v = concat(v, new_element) is to make a list, store elements with listput, then convert (if needed) to a vector with Vec. So [code]natural(N)={ my(l=List()); for(n=1,N,listput(l,n)); Vec(l) };[/code] is faster than [code]natural1(N)={ my(v=[]); for(n=1,N,v=concat(v,n)); v };[/code] Actually, with the changes made to concat() a few versions ago, the difference isn't great unless you aren't doing too much calculation, and it's better to make the whole vector at once when you can (vector(N,i,i) will be faster than either in this case). But I thought I'd share in case anyone was interested. * Not "something clever" but "the way I always should have been doing it". |
[QUOTE=CRGreathouse;242108]So
[code]natural(N)={ my(l=List()); for(n=1,N,listput(l,n)); Vec(l) };[/code] is faster than [code]natural1(N)={ my(l=List()); for(n=1,N,listput(l,n)); Vec(l) };[/code] [/QUOTE] I seriously doubt if the first one is faster than the second :wink: |
Hah! Fixed it.
|
[QUOTE=axn;242113]I seriously doubt if the first one is faster than the second :wink:[/QUOTE]
[CODE](09:42)>natural(100000) %3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1851, 1852, 1853, 1854, 1855, 1856, 1857, 185 7, 3518, 3519, 3520, 3521, 3522, 3523, 3524, 5184, 5185, 5186, 5187, 5188, 5189, 5190, 519 0, 6851, 6852, 6853, 6854, 6855, 6856, 6857, 8517, 8518, 8519, 8520, 8521, 8522, 8523, 852 157, 10158, 10159, 10160, 10161, 10162, 10163 , 11586, 11587, 11588, 11589, 11590, 11591, 1 3014, 13015, 13016, 13017, 13018, 13019, 1302 2, 14443, 14444, 14445, 14446, 14447, 14448, 15871, 15872, 15873, 15874, 15875, 15876, 158 99, 17300, 17301, 17302, 17303, 17304, 17305, 18728, 18729, 18730, 18731, 18732, 18733, 18 156, 20157, 20158, 20159, 20160, 20161, 20162 , 21585, 21586, 21587, 21588, 21589, 21590, 2 3013, 23014, 23015, 23016, 23017, 23018, 2301 1, 24442, 24443, 24444, 24445, 24446, 24447, 25870, 25871, 25872, 25873, 25874, 25875, 258 98, 27299, 27300, 27301, 27302, 27303, 27304, 28727, 28728, 28729, 28730, 28731, 28732, 28 155, 30156, 30157, 30158, 30159, 30160, 30161 , 31584, 31585, 31586, 31587, 31588, 31589, 3 3012, 33013, 33014, 33015, 33016, 33017, 3301 0, 34441, 34442, 34443, 34444, 34445, 34446, 35869, 35870, 35871, 35872, 35873, 35874, 358 97, 37298, 37299, 37300, 37301, 37302, 37303, 38726, 38727, 38728, 38729, 38730, 38731, 38 154, 40155, 40156, 40157, 40158, 40159, 40160 , 41583, 41584, 41585, 41586, 41587, 41588, 4 3011, 43012, 43013, 43014, 43015, 43016, 4301 9, 44440, 44441, 44442, 44443, 44444, 44445, 45868, 45869, 45870, 45871, 45872, 45873, 458 96, 47297, 47298, 47299, 47300, 47301, 47302, 48725, 48726, 48727, 48728, 48729, 48730, 48 153, 50154, 50155, 50156, 50157, 50158, 50159 , 51582, 51583, 51584, 51585, 51586, 51587, 5 3010, 53011, 53012, 53013, 53014, 53015, 5301 8, 54439, 54440, 54441, 54442, 54443, 54444, 55867, 55868, 55869, 55870, 55871, 55872, 558 95, 57296, 57297, 57298, 57299, 57300, 57301, 58724, 58725, 58726, 58727, 58728, 58729, 58 152, 60153, 60154, 60155, 60156, 60157, 60158 , 61581, 61582, 61583, 61584, 61585, 61586, 6 3009, 63010, 63011, 63012, 63013, 63014, 6301 7, 64438, 64439, 64440, 64441, 64442, 64443, 65866, 65867, 65868, 65869, 65870, 65871, 658 94, 67295, 67296, 67297, 67298, 67299, 67300, 68723, 68724, 68725, 68726, 68727, 68728, 68 151, 70152, 70153, 70154, 70155, 70156, 70157 , 71580, 71581, 71582, 71583, 71584, 71585, 7 3008, 73009, 73010, 73011, 73012, 73013, 7301 6, 74437, 74438, 74439, 74440, 74441, 74442, 75865, 75866, 75867, 75868, 75869, 75870, 758 93, 77294, 77295, 77296, 77297, 77298, 77299, 78722, 78723, 78724, 78725, 78726, 78727, 78 150, 80151, 80152, 80153, 80154, 80155, 80156 , 81579, 81580, 81581, 81582, 81583, 81584, 8 3007, 83008, 83009, 83010, 83011, 83012, 8301 5, 84436, 84437, 84438, 84439, 84440, 84441, 85864, 85865, 85866, 85867, 85868, 85869, 858 92, 87293, 87294, 87295, 87296, 87297, 87298, 88721, 88722, 88723, 88724, 88725, 88726, 88 149, 90150, 90151, 90152, 90153, 90154, 90155 , 91578, 91579, 91580, 91581, 91582, 91583, 9 3006, 93007, 93008, 93009, 93010, 93011, 9301 4, 94435, 94436, 94437, 94438, 94439, 94440, 95863, 95864, 95865, 95866, 95867, 95868, 958 91, 97292, 97293, 97294, 97295, 97296, 97297, 98720, 98721, 98722, 98723, 98724, 98725, 98 (09:42)>## *** last result computed in 125 ms. (09:42)>natural1(N)={ my(v=[]); for(n=1,N,v=concat(v,n)); v }; (09:42)>natural1(100000) *** user interrupt after 3mn, 39,875 ms.[/CODE] if 65536 was the limit for Vec ( which I thought it would be ) then the faster script also gets around that lol. |
those test are with minimal memory with my maximum I was able to do natural(10000000) in under 17 seconds according to the timing it gave. I wonder how much isprime would slow something like this down lol. looks like me adding a isprime check and trying up to 10000000 actually sped it up, listed all primes under 10 million in under 10 seconds according to the timer.That's without turning it into a forstep loop to only check 6n+1 of 6n-1.
|
[QUOTE=science_man_88;242171]those test are with minimal memory with my maximum I was able to do natural(10000000) in under 17 seconds according to the timing it gave.[/QUOTE]
Yes -- natural1 would take a long, long time to do that. It took 4:59.8 to do 10^5; based on curve-fitting with smaller numbers I estimate it would take about a month for it to do 10^7. |
| All times are UTC. The time now is 23:05. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.