mersenneforum.org  

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

Reply
 
Thread Tools
Old 2010-12-15, 16:45   #2080
axn
 
axn's Avatar
 
Jun 2003

5,087 Posts
Default

Code:
c=vector(10, n, n)
vecextract(c, concat("1..", #c-1))
vecextract(c, concat("2..", #c))
axn is offline   Reply With Quote
Old 2010-12-15, 16:46   #2081
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Code:
c=concat(c,c[1]);c=vector(#c-1,n,c[n+1])
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.

Last fiddled with by science_man_88 on 2010-12-15 at 16:52
science_man_88 is offline   Reply With Quote
Old 2010-12-15, 16:47   #2082
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

100000110000002 Posts
Default

Quote:
Originally Posted by axn View Post
Code:
c=vector(10, n, n)
vecextract(c, concat("1..", #c-1))
vecextract(c, concat("2..", #c))
didn't know I could do that syntax.
science_man_88 is offline   Reply With Quote
Old 2010-12-15, 23:06   #2083
3.14159
 
3.14159's Avatar
 
May 2010
Prime hunting commission.

24×3×5×7 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
How about
Code:
primesUPTO(x)=primes(primepi(x));
?

Also: I'm proud of you for making the function return a vector instead of just printing values. You're learning!
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.
3.14159 is offline   Reply With Quote
Old 2010-12-16, 02:44   #2084
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Quote:
Originally Posted by 3.14159 View Post
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.
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:
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.
science_man_88 is offline   Reply With Quote
Old 2010-12-16, 04:16   #2085
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by 3.14159 View Post
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.
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)
};
is faster than
Code:
natural1(N)={
  my(v=[]);
  for(n=1,N,v=concat(v,n));
  v
};
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".

Last fiddled with by CRGreathouse on 2010-12-16 at 05:13 Reason: fixed dumb mistake; thanks, axn
CRGreathouse is offline   Reply With Quote
Old 2010-12-16, 05:03   #2086
axn
 
axn's Avatar
 
Jun 2003

5,087 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
So
Code:
natural(N)={
  my(l=List());
  for(n=1,N,listput(l,n));
  Vec(l)
};
is faster than
Code:
natural1(N)={
  my(l=List());
  for(n=1,N,listput(l,n));
  Vec(l)
};
I seriously doubt if the first one is faster than the second
axn is offline   Reply With Quote
Old 2010-12-16, 05:13   #2087
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

597910 Posts
Default

Hah! Fixed it.
CRGreathouse is offline   Reply With Quote
Old 2010-12-16, 13:50   #2088
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by axn View Post
I seriously doubt if the first one is faster than the second
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.
if 65536 was the limit for Vec ( which I thought it would be ) then the faster script also gets around that lol.

Last fiddled with by science_man_88 on 2010-12-16 at 13:51
science_man_88 is offline   Reply With Quote
Old 2010-12-16, 14:02   #2089
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

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.

Last fiddled with by science_man_88 on 2010-12-16 at 14:19
science_man_88 is offline   Reply With Quote
Old 2010-12-16, 15:28   #2090
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
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.
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.
CRGreathouse is offline   Reply With Quote
Reply

Thread Tools


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 22:31.


Fri Aug 6 22:31:29 UTC 2021 up 14 days, 17 hrs, 1 user, load averages: 3.46, 3.33, 3.24

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.