mersenneforum.org  

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

Reply
 
Thread Tools
Old 2013-07-22, 13:29   #2399
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

20C016 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
Code:
 
a=[1,2,3];b=#a;until(#a==1,a[1]=concat(Str(a[1]),Str(a[2]));if(#a==b,a[b-1]=a[b]);a=vector(#a-1,n,a[n]));a=eval(a[1])
the correct version is:

Code:
a=[1,3,7,15,31,63];until(#a==1,a[1]=concat(Str(a[1]),Str(a[2]));for(b=3,#a,a[b-1]=a[b]);a=vector(#a-1,n,a[n]));a=eval(a[1])
science_man_88 is offline   Reply With Quote
Old 2013-07-22, 14:15   #2400
ismillo
 
Jul 2013
Brazil

19 Posts
Default

Thank you very much, Sir. This will help a lot.
PARI is a great languague for mathematics no doubts, however let's be honest, it's some times so much complicated.

For anyone who has the same problem I had, here is the function, only if you may, science_man_88, all credits for you.

Code:
vectoint(a)={
	until(#a==1,a[1]=concat(Str(a[1]),Str(a[2]));for(b=3,#a,a[b-1]=a[b]);
	a=vector(#a-1,n,a[n]));
	a=eval(a[1])
};
Once again, thank you very much, for solving my problem and for quick reply as well.
ismillo is offline   Reply With Quote
Old 2013-07-23, 03:12   #2401
axn
 
axn's Avatar
 
Jun 2003

5,087 Posts
Default

Code:
vec2int(a)={my(s=""); for(i=1,#a,s=Str(s,a[i])); eval(s)}
axn is offline   Reply With Quote
Old 2013-07-23, 11:01   #2402
ismillo
 
Jul 2013
Brazil

19 Posts
Default

Another great help, axn, thank you.

I was doing some test here, axn is a faster, however, both are excellent and helps a lot.


Click image for larger version

Name:	res.png
Views:	70
Size:	2.6 KB
ID:	10041
ismillo is offline   Reply With Quote
Old 2013-07-23, 22:56   #2403
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

There's an even faster version available if your numbers are only a digit each:

Code:
subst(Pol(a), 'x, 10)
CRGreathouse is offline   Reply With Quote
Old 2013-07-23, 23:25   #2404
ismillo
 
Jul 2013
Brazil

19 Posts
Default

Awesome, this will help a lot with large palindromics numbers.
I usually compare palindromes making vectors and never knew how I could manipulate the numbers.

Edit:
One last question, how do I return the position of a specific character from a word?
For example 'a = "ABC"' and return "A".

PS.: I know PARI isn't the right language for strings, but in the end, it's all about Mathematics.

Last fiddled with by ismillo on 2013-07-24 at 00:23
ismillo is offline   Reply With Quote
Old 2013-07-24, 02:41   #2405
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by ismillo View Post
One last question, how do I return the position of a specific character from a word?
For example 'a = "ABC"' and return "A".

PS.: I know PARI isn't the right language for strings, but in the end, it's all about Mathematics.
PARI is bad at this. The best method it has to offer is Vec("ABC")[1]. For finding a string, you can try
Code:
find(source, target)={
  source=Vec(source);
  target=Vec(target);
  for(i=0,#source-#target,
    for(j=1,#target,
      if(target[j]!=source[i+j],next(2))
    );
    return(i+1)
  );
  -1
};
CRGreathouse is offline   Reply With Quote
Old 2013-07-24, 02:43   #2406
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

135338 Posts
Default

Quote:
Originally Posted by ismillo View Post
Awesome, this will help a lot with large palindromics numbers.
I usually compare palindromes making vectors and never knew how I could manipulate the numbers.
I should also mention the commands Vecrev and Polrev, then.

Code:
subst(Polrev(digits(123)), 'x, 10)
gives 321.
CRGreathouse is offline   Reply With Quote
Old 2013-07-24, 11:43   #2407
ismillo
 
Jul 2013
Brazil

19 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
I should also mention the commands Vecrev and Polrev, then.

Code:
subst(Polrev(digits(123)), 'x, 10)
gives 321.
Nice, I didn't knew I could reverse vector with function from PARI, so I made my own. :D

Quote:
Originally Posted by CRGreathouse View Post
PARI is bad at this. The best method it has to offer is Vec("ABC")[1]. For finding a string, you can try
Code:
find(source, target)={
  source=Vec(source);
  target=Vec(target);
  for(i=0,#source-#target,
    for(j=1,#target,
      if(target[j]!=source[i+j],next(2))
    );
    return(i+1)
  );
  -1
};
This is perfect, I guess. Later I will give it a shot, if I succeed then I notify you guys.

Last fiddled with by ismillo on 2013-07-24 at 11:43
ismillo is offline   Reply With Quote
Old 2013-07-24, 20:46   #2408
ismillo
 
Jul 2013
Brazil

100112 Posts
Default

Well, I'm having an issue with 2 equal letters, I'll keep trying, if can't do it I will try make in C.
:/

Last fiddled with by ismillo on 2013-07-24 at 20:48
ismillo is offline   Reply With Quote
Old 2013-07-24, 21:00   #2409
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

175B16 Posts
Default

Quote:
Originally Posted by ismillo View Post
Well, I'm having an issue with 2 equal letters, I'll keep trying, if can't do it I will try make in C.
:/
Can you give an example of the input, the expected output, and the actual output? I'll admit that I didn't test my script at all, just typed it up, so bugs are certainly possible.
CRGreathouse 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 21:15.


Fri Aug 6 21:15:04 UTC 2021 up 14 days, 15:44, 1 user, load averages: 2.56, 2.52, 2.52

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.