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)

ismillo 2013-07-24 21:12

Sure.

I don't know if you ever heard about Project Euler, It's a website about Math problems which is highly recommend use programming languages.

So, there is this problem, you have an array of words and you sum each letter (A=1;B=2;C=3;...Z=26;(For example "CAN" = 18; 3+1+14=18)). If the sum of the letters is in the possibles results from the equation "tn = ½n(n+1)", then the word is a triangle word. The problem is asking for how many triangle words the array has.

My issue is, there's some words with two equal letter, such as "ADD" and "ACCESS", however the function only detects one and only sums as one.

I'm not asking for the answer, I'm just asking for help.
----------------------------------------------------

INPUT

a="ABCA";
find(a,"A");

OUTPUT

1

Only returns the first word that match with.

CRGreathouse 2013-07-25 03:00

So you don't want a find function at all, but just (1) a function that loops over the letters, and (2) a function that converts a letter to its numeric value. Function (1) calls function (2) each time through the loop and returns the sum.

I'd suggest something like
[code]getLetterValue(letter)={
\\ something here
};

getWordValue(word)={
my(letters=Vec(word));
sum(i=1,#letters, getLetterValue(letters[i]))
};[/code]

ismillo 2013-07-25 10:14

Well, while I was sleeping I have found the solution.

I knew the "Vec(x)" exists, however, I didn't have the slightest idea it could split words into its characters.

Thanks, CRG.

Here's the code.

[CODE]

counterWords(w)={
l=Vec(w);
vp=vector(26,i,Strchr(i+64));
c=0;
for(a=1,#l,c+=vecsearch(vp,l[a]));
c;
}

w=read("words.txt");
t=0;
vt=vector(18,n,(1/2*n*(n+1)));
for(x=1,#w,c=counterWords(w[x]);if(vecsearch(vt,c),t++));t

[/CODE]

CRGreathouse 2013-07-25 22:38

Good, you got it working.

You might find that using Vecsmall("ABC") is more convenient... you can just subtract 64 and be done, no need for a lookup table.

ismillo 2013-07-25 23:07

Great, this new function is about 50% faster than the old one. :D

[CODE]

cw(w)={
l=Vec(w);
c=0;
for(a=1,#l,c+=Vecsmall(l[a])[1]-64);c;
}

w=read("words.txt");
t=0;
vt=vector(18,n,(1/2*n*(n+1)));
for(x=1,#w,c=cw(w[x]);if(vecsearch(vt,c),t++));t

[/CODE]

CRGreathouse 2013-07-25 23:44

[CODE]cw(w)={
my(v=Vecsmall(w));
sum(i=1,#v,v[i]-64)
};[/CODE]
should be much faster. You could improve this to
[CODE]cw(w)={
my(v=Vecsmall(w));
sum(i=1,#v,v[i])-64*#v
};[/CODE]
at a slight cost to readability.

Also
[code]sum(i=1,#w,ispolygonal(cw(w[i]),3))[/code]
if you're using a recent version of gp.

ismillo 2013-07-25 23:57

I see, now, the 1st function solve the problem in 40 ms, the 2nd in 20 ms.
The two new one solve in 14 ms the 1st, and 13 the 2nd.

"sum(i=1,#w,ispolygonal(cw(w[i]),3))" solves in 3 ms. However its specific for this situation.

So, this is a great improvement.

PS.: "Sum" is better than "for" in every situation which requires repetitions?

CRGreathouse 2013-07-26 00:02

[QUOTE=ismillo;347388]"Sum" is better then "for" in every situation?[/QUOTE]

When you're doing a sum, use sum. When you're doing a product, use prod. that way you don't need temporary variables so it looks neater (and is shorter to type). Plus, as an added benefit, prod uses binary splitting to keep subproducts small which can greatly improve speed.

ismillo 2013-07-26 00:08

Hm... understood. Soon or later I'll probably keep reading PARI docs. I was just using what I knew. It's difficult to find good tutorials around the web, you guys and this thread is helping a lot.

Thank you, Sir.

CRGreathouse 2013-07-26 01:10

Glad to help. You may also find
[url]http://rosettacode.org/wiki/Category:PARI/GP[/url]
useful. It contains a large number of tasks and their solutions in GP (as well as many other languages). I actually wrote most of the PARI/GP code myself, in hopes that it would help people like you.

Also, you'll find some tutorials linked there -- possibly useful as well. (I didn't write any of them.)

ismillo 2013-07-26 12:37

Rosetta Code is very helpful. Couple weeks ago (in the same week the website was down), I was listing all possible useful functions and put them all in a file. :D


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

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