mersenneforum.org  

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

Reply
 
Thread Tools
Old 2013-07-24, 21:12   #2410
ismillo
 
Jul 2013
Brazil

1316 Posts
Default

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
ismillo is offline   Reply With Quote
Old 2013-07-25, 03:00   #2411
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

175B16 Posts
Default

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]))
};
CRGreathouse is offline   Reply With Quote
Old 2013-07-25, 10:14   #2412
ismillo
 
Jul 2013
Brazil

1316 Posts
Default

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
ismillo is offline   Reply With Quote
Old 2013-07-25, 22:38   #2413
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

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
CRGreathouse is offline   Reply With Quote
Old 2013-07-25, 23:07   #2414
ismillo
 
Jul 2013
Brazil

1316 Posts
Default

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
ismillo is offline   Reply With Quote
Old 2013-07-25, 23:44   #2415
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

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

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

Last fiddled with by CRGreathouse on 2013-07-25 at 23:49
CRGreathouse is offline   Reply With Quote
Old 2013-07-25, 23:57   #2416
ismillo
 
Jul 2013
Brazil

19 Posts
Default

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
ismillo is offline   Reply With Quote
Old 2013-07-26, 00:02   #2417
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

175B16 Posts
Default

Quote:
Originally Posted by ismillo View Post
"Sum" is better then "for" in every situation?
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.
CRGreathouse is offline   Reply With Quote
Old 2013-07-26, 00:08   #2418
ismillo
 
Jul 2013
Brazil

19 Posts
Default

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
ismillo is offline   Reply With Quote
Old 2013-07-26, 01:10   #2419
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

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
CRGreathouse is offline   Reply With Quote
Old 2013-07-26, 12:37   #2420
ismillo
 
Jul 2013
Brazil

19 Posts
Default

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
ismillo 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 23:31.


Fri Aug 6 23:31:55 UTC 2021 up 14 days, 18 hrs, 1 user, load averages: 4.26, 3.93, 3.96

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.