mersenneforum.org  

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

Reply
 
Thread Tools
Old 2010-11-30, 02:31   #1827
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

597910 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
I couldn't get the digits placed properly as i didn't have a formula I found one that worked except a shift I made a shift of all but answer[1] then I shifted answer[1] separately, and yes you would as it determines what stays in position 1.
If you look at my code, the formula is embarrassingly simple. (I could hardly believe it when I saw that pretty much everything cancels...)
CRGreathouse is offline   Reply With Quote
Old 2010-11-30, 13:22   #1828
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

100000110000002 Posts
Default

Code:
pascals_triangle(N)= {
my(row=[],prevrow=[])
for(x=1,N,
    if(x>5,break(1));
         row=eval(Vec(Str(11^(x-1))));
         print(row));
         prevrow=row;
for(y=6,N,
   for(p=2,#prevrow,
         row[p]=prevrow[p-1]+prevrow[p]);
         row=concat(row,1);
         prevrow=row;
         print(row);
     );
}

good enough code ? I added my() in just to satisfy you even though i see no point of it.
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 14:05   #1829
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Code:
RLE(string)={
my(number=1,string=eval(Vec(Str(string))),Letter=string[1]);
      for(x=2,#string,
           if(string[x]==Letter,
              number+=1,
              print1("("number")"Letter);
              Letter=string[x];
              number=0)
         );
}
This is my best try at RLE I have a book that has it and LZW. I tried it with RLE(wwwbbbbwwww) it failed to code the last w's.

Last fiddled with by science_man_88 on 2010-11-30 at 14:16
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 18:43   #1830
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
good enough code ?
I don't understand where the constants 5 and 6 come from.

Quote:
Originally Posted by science_man_88 View Post
I added my() in just to satisfy you even though i see no point of it.
It is *perfectly* acceptable to not understand why my() is used. In fact, I would generally avoid telling people I'm teaching why they use my() at all -- just have them use it whenever they define their own variables.
CRGreathouse is offline   Reply With Quote
Old 2010-11-30, 18:50   #1831
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

100000110000002 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
I don't understand where the constants 5 and 6 come from.



It is *perfectly* acceptable to not understand why my() is used. In fact, I would generally avoid telling people I'm teaching why they use my() at all -- just have them use it whenever they define their own variables.
I did the 5 and 6 because up until row 5 of the triangle they easily conform to the digits of 11^row number. and I used a slower form of adding them together from there until N if it's above 5.
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 18:51   #1832
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

175B16 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
Code:
RLE(string)={
my(number=1,string=eval(Vec(Str(string))),Letter=string[1]);
      for(x=2,#string,
           if(string[x]==Letter,
              number+=1,
              print1("("number")"Letter);
              Letter=string[x];
              number=0)
         );
}
This is my best try at RLE I have a book that has it and LZW. I tried it with RLE(wwwbbbbwwww) it failed to code the last w's.
Very nice code formatting!

First, you should take input as a string: trust me, this is the right way. So call it with RLE("wwwbbbbwwww") and just call string=Vec(string).

The code should (presumably, didn't read the task description) *either* add 1 *or* print the current total for the last letter, reset the letter to the current letter, and reset the count.
CRGreathouse is offline   Reply With Quote
Old 2010-11-30, 18:53   #1833
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Very nice code formatting!

First, you should take input as a string: trust me, this is the right way. So call it with RLE("wwwbbbbwwww") and just call string=Vec(string).

The code should (presumably, didn't read the task description) *either* add 1 *or* print the current total for the last letter, reset the letter to the current letter, and reset the count.
yeah I figured that was coming...

yeah that's what the if is trying to do and it works until it hits the second set of w's but then fails.

this may be something to keep my mind busy while my mom checks through emergency to fix a portacast that seems to be coming out! and a lower body temperature.

Last fiddled with by science_man_88 on 2010-11-30 at 18:56
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 18:56   #1834
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
I did the 5 and 6 because up until row 5 of the triangle they easily conform to the digits of 11^row number. and I used a slower form of adding them together from there until N if it's above 5.
I just noticed that you formatting is deceptive. I've highlighted the two main loops in red and blue:
Code:
pascals_triangle(N)= {
my(row=[],prevrow=[])
for(x=1,N,
    if(x>5,break(1));
         row=eval(Vec(Str(11^(x-1))));
         print(row));
         prevrow=row;
for(y=6,N,
   for(p=2,#prevrow,
         row[p]=prevrow[p-1]+prevrow[p]);
         row=concat(row,1);
         prevrow=row;
         print(row);
     );
}
I suspect that the indentation reflects your intent and the parentheses need to be moved to match.
CRGreathouse is offline   Reply With Quote
Old 2010-11-30, 18:58   #1835
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
yeah that's what the if is trying to do and it works until it hits the second set of w's but then fails.
Actually... it looks like that part of your code is OK.
CRGreathouse is offline   Reply With Quote
Old 2010-11-30, 19:00   #1836
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
I just noticed that you formatting is deceptive. I've highlighted the two main loops in red and blue:
Code:
pascals_triangle(N)= {
my(row=[],prevrow=[])
for(x=1,N,
    if(x>5,break(1));
         row=eval(Vec(Str(11^(x-1))));
         print(row));
         prevrow=row;
for(y=6,N,
   for(p=2,#prevrow,
         row[p]=prevrow[p-1]+prevrow[p]);
         row=concat(row,1);
         prevrow=row;
         print(row);
     );
}
I suspect that the indentation reflects your intent and the parentheses need to be moved to match.
copying your format I found another missed semi colon

Last fiddled with by science_man_88 on 2010-11-30 at 19:00
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 19:14   #1837
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Actually... it looks like that part of your code is OK.
Code:
RLE(string)={
my(number=1,string=eval(Vec(Str(string))),Letter=string[1]);
      for(x=2,#string,
           if(string[x]==Letter,
              number+=1,
              print1("("number")"Letter);
              Letter=string[x];
              number=0)
         );
}
RLE() takes the string puts the first letter in Letter and puts an array of characters in string and start number off at 1 to represent the first letter in the count.

if then goes from string[2] to string[#string] checking if the next letter matches Letter if it matches number increases.Otherwise it prints ([COLOR="rgb(255, 140, 0)"]number[/COLOR])Letter then sets Letter to the letter that doesn't match. Then it resets [COLOR="rgb(255, 140, 0)"]number[/COLOR] to be 0 which actually should be 1.

fixed it I forgot a final print after the loop.

Last fiddled with by science_man_88 on 2010-11-30 at 19:21
science_man_88 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:03.


Fri Aug 6 23:03:17 UTC 2021 up 14 days, 17:32, 1 user, load averages: 3.70, 3.95, 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.