mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   PARI/GP (https://www.mersenneforum.org/forumdisplay.php?f=155)
-   -   PARI's commands (https://www.mersenneforum.org/showthread.php?t=13636)

science_man_88 2013-07-22 13:29

[QUOTE=science_man_88;346974] [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])
[/CODE]
[/QUOTE]

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])[/CODE]

ismillo 2013-07-22 14:15

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])
};
[/CODE]

Once again, thank you very much, for solving my problem and for quick reply as well.

axn 2013-07-23 03:12

[CODE]vec2int(a)={my(s=""); for(i=1,#a,s=Str(s,a[i])); eval(s)}[/CODE]

ismillo 2013-07-23 11:01

1 Attachment(s)
Another great help, axn, thank you.

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


[ATTACH]10041[/ATTACH]

CRGreathouse 2013-07-23 22:56

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

[code]subst(Pol(a), 'x, 10)[/code]

ismillo 2013-07-23 23:25

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.

CRGreathouse 2013-07-24 02:41

[QUOTE=ismillo;347172]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.[/QUOTE]

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
};[/code]

CRGreathouse 2013-07-24 02:43

[QUOTE=ismillo;347172]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.[/QUOTE]

I should also mention the commands Vecrev and Polrev, then.

[code]subst(Polrev(digits(123)), 'x, 10)[/code]

gives 321.

ismillo 2013-07-24 11:43

[QUOTE=CRGreathouse;347187]I should also mention the commands Vecrev and Polrev, then.

[code]subst(Polrev(digits(123)), 'x, 10)[/code]

gives 321.[/QUOTE]

Nice, I didn't knew I could reverse vector with function from PARI, so I made my own. :D

[QUOTE=CRGreathouse;347185]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
};[/code][/QUOTE]

This is perfect, I guess. Later I will give it a shot, if I succeed then I notify you guys.

ismillo 2013-07-24 20:46

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.
:/

CRGreathouse 2013-07-24 21:00

[QUOTE=ismillo;347250]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.
:/[/QUOTE]

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.


All times are UTC. The time now is 22:37.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.