![]() |
|
|
#2410 |
|
Jul 2013
Brazil
19 Posts |
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. Last fiddled with by ismillo on 2013-07-24 at 21:31 |
|
|
|
|
|
#2411 |
|
Aug 2006
3·1,993 Posts |
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]))
};
|
|
|
|
|
|
#2412 |
|
Jul 2013
Brazil
19 Posts |
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
Last fiddled with by ismillo on 2013-07-25 at 10:23 |
|
|
|
|
|
#2413 |
|
Aug 2006
3×1,993 Posts |
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. Last fiddled with by CRGreathouse on 2013-07-25 at 22:39 |
|
|
|
|
|
#2414 |
|
Jul 2013
Brazil
19 Posts |
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
Last fiddled with by ismillo on 2013-07-25 at 23:16 |
|
|
|
|
|
#2415 |
|
Aug 2006
3×1,993 Posts |
Code:
cw(w)={
my(v=Vecsmall(w));
sum(i=1,#v,v[i]-64)
};
Code:
cw(w)={
my(v=Vecsmall(w));
sum(i=1,#v,v[i])-64*#v
};
Also Code:
sum(i=1,#w,ispolygonal(cw(w[i]),3)) Last fiddled with by CRGreathouse on 2013-07-25 at 23:49 |
|
|
|
|
|
#2416 |
|
Jul 2013
Brazil
19 Posts |
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? Last fiddled with by ismillo on 2013-07-26 at 00:00 |
|
|
|
|
|
#2417 |
|
Aug 2006
597910 Posts |
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.
|
|
|
|
|
|
#2418 |
|
Jul 2013
Brazil
19 Posts |
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. Last fiddled with by ismillo on 2013-07-26 at 00:26 |
|
|
|
|
|
#2419 |
|
Aug 2006
3×1,993 Posts |
Glad to help. You may also find
http://rosettacode.org/wiki/Category:PARI/GP 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.) Last fiddled with by CRGreathouse on 2013-07-26 at 01:12 |
|
|
|
|
|
#2420 |
|
Jul 2013
Brazil
19 Posts |
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
|
|
|
|
![]() |
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 |