![]() |
Stack needs:
vector to act as placeholders; concat() that acts like push and throws errors if something like concat(data,c) in the mix; pop instruction to take the last in as the first out. In other words a variable like the stack pointer in asm to point at the data to be popped off. |
okay a little trouble:
[CODE] stack= [0] push(n)=concat(stack,n) pop(n)=stack[#stack]=n;[/CODE] push only does it's job with concat() the first time I use it. pop I coded wrong and even correctly coded it doesn't work. |
Found around the push error.
Now if only I can get pop to work. |
I wrote it up here for you:
[url]http://rosettacode.org/wiki/Stack#PARI.2FGP[/url] |
[QUOTE=CRGreathouse;239039]I wrote it up here for you:
[url]http://rosettacode.org/wiki/Stack#PARI.2FGP[/url][/QUOTE] it should be noted in [url]http://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_PARI/GP[/url] as done I think. |
[url]http://rosettacode.org/wiki/Go_Fish[/url]
shouldn't be too hard either: The hard part is making the string question eliminate things like numbers. 2 arrays: both randomly filled then a question bot like I tried in ms-dos before i think. |
oh and I've built something near this before:
[url]http://rosettacode.org/wiki/Rate_counter[/url] just need a loop a timer variable to collect gettime() data and a counter variable to count the repetitions of something every x milliseconds. |
[QUOTE=science_man_88;239040]it should be noted in [url]http://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_PARI/GP[/url]
as done I think.[/QUOTE] That list automatically updates, so it should not list Stack anymore. |
[QUOTE=science_man_88;239042]oh and I've built something near this before:
[url]http://rosettacode.org/wiki/Rate_counter[/url] just need a loop a timer variable to collect gettime() data and a counter variable to count the repetitions of something every x milliseconds.[/QUOTE] You write up that version and I'll add a 2.4.3 version using alarm(). |
[QUOTE=CRGreathouse;239045]You write up that version and I'll add a 2.4.3 version using alarm().[/QUOTE][url]http://www.mersenneforum.org/clear.gif[/url]
[CODE][COLOR="Red"]a=0;b=0;[/COLOR]c=0;for(n=1,20000000,[COLOR="red"]a=a+gettime();if(a>60000,print(b);a=0;b=0)[/COLOR];c=c+n;[COLOR="red"]b=b+1;a=a+gettime();if(a>60000,print(b);a=0;b=0)[/COLOR])[/CODE] is the more complicated version, the less complicated: [CODE]for(1,number_of_test_to_perform,gettime();c=0;for(n=1,20000000,c=c+n);return(20000000/gettime())); [/CODE] might be simpler, but acts more of a code tester. |
Why do you use the constant 20000000 here?
|
[QUOTE=CRGreathouse;239047]Why do you use the constant 20000000 here?[/QUOTE]
just picked a number I thought would take a while to test with. |
the list hasn't been updated in a while lol:
[url]http://rosettacode.org/wiki/Create_a_file[/url] |
funny I brought up testing a code:
[url]http://rosettacode.org/wiki/Test_a_function[/url] this fits this one as well. Though why not just type the function lol. |
here's one that works both for the thread and has something else in common with Mersenne forums:
[url]http://rosettacode.org/wiki/Towers_of_Hanoi[/url] |
[QUOTE=science_man_88;239048]just picked a number I thought would take a while to test with.[/QUOTE]
Yes, but the task is supposed to work with an arbitrary function. First, yours doesn't; second, it should really avoid such 'magic numbers'. What if, for example, the function took an hour to complete each run? |
[QUOTE=science_man_88;239049]the list hasn't been updated in a while lol:
[url]http://rosettacode.org/wiki/Create_a_file[/url][/QUOTE] If you can code it, go right ahead. No one has so far in Pari/GP. |
[QUOTE=CRGreathouse;239055]If you can code it, go right ahead. No one has so far in Pari/GP.[/QUOTE]
It's the directory part that has stumped me because all of ms-dos make directory commands apparently are internal only use grrrr. |
[CODE]longmultiply(a,b)= a=eval(a);b=eval(b);c=Vec(a);d=Vec(b);carry=0;answer=[];forstep(x=#d,1,[-1],if(d[x]==Vec("2"),print(yes)))[/CODE]
is my attempt at long multiplication. |
why is [URL="http://rosettacode.org/wiki/Guess_the_number/With_Feedback"]this[/URL] not complete lol, it only took [B][U][I][SIZE="4"]ME[/SIZE][/I][/U][/B] less than 10 minutes to work out lol.
[CODE]guess_the_number(N=10)= a=random(N);print("guess the number between 0 and "N);for(x=1,N,if(x>1,print("guess again"));b=input();if(b==a,break()));print("You gueesed it correctly")[/CODE] |
[QUOTE=science_man_88;239081]It's the directory part that has stumped me because all of ms-dos make directory commands apparently are internal only use grrrr.[/QUOTE]
Right. Also making it work cross-platform if possible: not using ~ in Windows, for example. |
[QUOTE=science_man_88;239086][CODE]longmultiply(a,b)= a=eval(a);b=eval(b);c=Vec(a);d=Vec(b);carry=0;answer=[];forstep(x=#d,1,[-1],if(d[x]==Vec("2"),print(yes)))[/CODE]
is my attempt at long multiplication.[/QUOTE] I don't understand the program; it seems to never use c, carry, or answer. Is this just incomplete? I certainly wouldn't use eval() unless I absolutely had to. |
[QUOTE=CRGreathouse;239098]I don't understand the program; it seems to never use c, carry, or answer. Is this just incomplete?
I certainly wouldn't use eval() unless I absolutely had to.[/QUOTE] I used eval so people could use powers not the full written number., and yes it's incomplete. |
[QUOTE=science_man_88;239087]why is [URL="http://rosettacode.org/wiki/Guess_the_number/With_Feedback"]this[/URL] not complete lol, it only took [B][U][I][SIZE="4"]ME[/SIZE][/I][/U][/B] less than 10 minutes to work out lol.
[CODE]guess_the_number(N=10)= a=random(N);print("guess the number between 0 and "N);for(x=1,N,if(x>1,print("guess again"));b=input();if(b==a,break()));print("You gueesed it correctly")[/CODE][/QUOTE] So post it to the site! (For copyright reasons, I probably shouldn't post it there myself.) You should break it onto multiple lines if possible first; that way it will look nice. |
[QUOTE=CRGreathouse;239100]So post it to the site! (For copyright reasons, I probably shouldn't post it there myself.)
You should break it onto multiple lines if possible first; that way it will look nice.[/QUOTE] the site isn't updating I added the guessing script, and the rate counter lol. |
[QUOTE=science_man_88;239099]I used eval so people could use powers not the full written number., and yes it's incomplete.[/QUOTE]
You can certainly write [code]f(x,y)=x+y; f(2^10, 3^4)[/code] and have it work properly without eval. |
[QUOTE=science_man_88;239101]the site isn't updating I added the guessing script, and the rate counter lol.[/QUOTE]
Good stuff. I changed the name in the header to PARI/GP, because that's how it needs to be (with caps) to show up properly in the categories and all that. |
[QUOTE=CRGreathouse;239102]You can certainly write
[code]f(x,y)=x+y; f(2^10, 3^4)[/code] and have it work properly without eval.[/QUOTE] Yeah well long multiplication uses each digit last I heard. |
[QUOTE=CRGreathouse;239103]Good stuff. I changed the name in the header to PARI/GP, because that's how it needs to be (with caps) to show up properly in the categories and all that.[/QUOTE]
I should make a note lol. |
FWIW I'm working on the task Amb at the moment.
[QUOTE=science_man_88;239104]Yeah well long multiplication uses each digit last I heard.[/QUOTE] I don't know what that has to do with my point (that you don't need eval). |
[QUOTE=CRGreathouse;239107]FWIW I'm working on the task Amb at the moment.
I don't know what that has to do with my point (that you don't need eval).[/QUOTE] thought it wasn't self evaluating. |
[QUOTE=CRGreathouse;239098]I don't understand the program; it seems to never use c, carry, or answer. Is this just incomplete?
I certainly wouldn't use eval() unless I absolutely had to.[/QUOTE] The reason I never use the others is I can't get the d[x]==2 stuff to work. If I can't get this to work I can't work on digit by digit in string form. |
[QUOTE=science_man_88;239111]The reason I never use the others is I can't get the d[x]==2 stuff to work. If I can't get this to work I can't work on digit by digit in string form.[/QUOTE]
I don't understand what part you're stuck on, but eval() won't help you get there. |
[QUOTE=CRGreathouse;239114]I don't understand what part you're stuck on, but eval() won't help you get there.[/QUOTE]
What is stopping me is I made strings out of the digits and I want to check the digits but I haven't found a way that works to check them. If I could I'd complete->[url]http://rosettacode.org/wiki/Determine_if_a_string_is_numeric[/url] as well. |
[QUOTE=science_man_88;239115]What is stopping me is I made strings out of the digits and I want to check the digits but I haven't found a way that works to check them. If I could I'd complete->[url]http://rosettacode.org/wiki/Determine_if_a_string_is_numeric[/url] as well.[/QUOTE]
What does "check the digits" mean? Making a string out of a number is easy: Str(n). Making this into a vector is similarly easy: Vec(Str(n)). If you want numbers rather than characters, you need eval(Vec(Str(n))). Possibly a better way is Vecsmall(Str(n)) which gives numbers that are 48 to 57 rather than 0 to 9. (You can subtract, of course, as desired.) |
[QUOTE=CRGreathouse;239116]What does "check the digits" mean?
Making a string out of a number is easy: Str(n). Making this into a vector is similarly easy: Vec(Str(n)). If you want numbers rather than characters, you need eval(Vec(Str(n))). Possibly a better way is Vecsmall(Str(n)) which gives numbers that are 48 to 57 rather than 0 to 9. (You can subtract, of course, as desired.)[/QUOTE] okay thanks now we can use your brains to finish both long multiply and determine if numerical. doh! What am I doing checking if it's 2? Now I can just add them now and use carry to take care of everything thanks again. |
[QUOTE=science_man_88;239117]okay thanks now we can use your brains to finish both long multiply and determine if numerical.[/QUOTE]
I'd rather expand yours. :smile: |
[QUOTE=CRGreathouse;239118]I'd rather expand yours. :smile:[/QUOTE]
Good luck with that lol. [CODE]longmultiply(a,b)= c=eval(Vec(Str(a)));d=eval(Vec(Str(b)));carry=0;answer=[];forstep(x=#d,1,[-1],forstep(y=#c,1,[-1],answer=concat(((c[y]*d[x])+carry)%10,answer);carry=floor((c[y]*d{x])/10)) *** sorry, embedded braces (in parser) is not yet implemented.[/CODE] I can't figure out what's messing up and, Yes I've checked the braces just enough that it should work. Oh never mind stupid concat lol. didn't work for calculating it before either. |
I have a hard time believing [url=http://rosettacode.org/wiki/Pascal's_triangle]this[/url] hasn't been done.
|
[QUOTE=science_man_88;239122]Good luck with that lol.[/QUOTE]
It's already worked. Look at how much you can do with Pari now -- you hadn't even heard of it a year ago. [QUOTE=science_man_88;239122]I can't figure out what's messing up and, Yes I've checked the braces just enough that it should work. Oh never mind stupid concat lol.[/QUOTE] You wrote "{x]" instead of "[x]". |
[QUOTE=science_man_88;239123]I have a hard time believing [url=http://rosettacode.org/wiki/Pascal's_triangle]this[/url] hasn't been done.[/QUOTE]
Essentially the only tasks that are done are the ones I did over the last four weeks. Sometimes I didn't do a task because it was easy and I thought anyone could do it; sometimes I left a task because I couldn't think of any nice way to do it; sometimes I couldn't think of any way to do it; sometimes I just didn't get around to it. You may very well become the #2 Pari contributor on RC if you continue this way. (I don't know who presently has that title, but they couldn't have very many.) |
[QUOTE=CRGreathouse;239126]Essentially the only tasks that are done are the ones I did over the last four weeks. Sometimes I didn't do a task because it was easy and I thought anyone could do it; sometimes I left a task because I couldn't think of any nice way to do it; sometimes I couldn't think of any way to do it; sometimes I just didn't get around to it.
You may very well become the #2 Pari contributor on RC if you continue this way. (I don't know who presently has that title, but they couldn't have very many.)[/QUOTE] Yeah something missing in my code I know because answer became [CODE][6, 9, 4, 6][/CODE] instead of [CODE][7,3,6][/CODE] |
Well, good luck debugging. Sometimes splitting the program into multiple functions helps, as does adding diagnostic messages with print(). (You remove these after debugging, of course.)
I finished the Amb task, that was a pain to debug. I hate string processing in Pari. :smile: |
[QUOTE=CRGreathouse;239129]
I finished the Amb task, that was a pain to debug. I hate string processing in Pari. :smile:[/QUOTE] Really? I thought it was only me that hated it. I'm heading to bed I'm up 30 minutes past when I usually go to bed. I've got Pari still open and I'm semi working on a pascals triangle script. |
[QUOTE=science_man_88;239130]Really? I thought it was only me that hated it. I'm heading to bed I'm up 30 minutes past when I usually go to bed. I've got Pari still open and I'm semi working on a pascals triangle script.[/QUOTE]
The problem I had was related to the power of concat: I wanted it to join together two vectors and it was obligingly smushing them into strings. I managed to solve my problem, thankfully. |
[QUOTE=CRGreathouse;239131]The problem I had was related to the power of concat: I wanted it to join together two vectors and it was obligingly smushing them into strings. I managed to solve my problem, thankfully.[/QUOTE]
the problem for me is I don't use multiple answer vectors: 023 x32 ___ 0046 +690 not : 23*32 =[6,9,4,6] |
[QUOTE=science_man_88;239157]the problem for me is I don't use multiple answer vectors:
023 x32 ___ 0046 +690 not : 23*32 =[6,9,4,6][/QUOTE] Ah. You should only use one, but pre-set it to the appropriate size, filled with 0s, then *add* rather than concatenate. That way you can go from [0, 0, 0, 0, 0] to [0, 0, 0, 0, 6] to [0, 0, 0, 4, 6] to [0, 0, 0, 13, 6] to [0, 0, 6, 13, 6] which can be simplified to [0, 0, 7, 3, 6] and then [7, 3, 6] if desired. |
[QUOTE=CRGreathouse;239167]Ah. You should only use one, but pre-set it to the appropriate size, filled with 0s, then *add* rather than concatenate. That way you can go from
[0, 0, 0, 0, 0] to [0, 0, 0, 0, 6] to [0, 0, 0, 4, 6] to [0, 0, 0, 13, 6] to [0, 0, 6, 13, 6] which can be simplified to [0, 0, 7, 3, 6] and then [7, 3, 6] if desired.[/QUOTE] I was thinking of using while loops and another vector, but your way works I think. The problem is if I get 9*9 then it makes it can make it even longer still. |
[QUOTE=science_man_88;239206]I was thinking of using while loops and another vector, but your way works I think. The problem is if I get 9*9 then it makes it can make it even longer still.[/QUOTE]
If you multiply an n digit number by an m digit number, n + m digits will suffice (though the most significant digit may be 0). |
[QUOTE=CRGreathouse;239208]If you multiply an n digit number by an m digit number, n + m digits will suffice (though the most significant digit may be 0).[/QUOTE]
Okay I think I get the preset stuff, if I just add I get 4+9+6+6 if I have it as is. |
[CODE]answer[#answer-(x*y-1)]=(answer[#answer-(x*y-1)]+n);carry=floor((c[y]*d[x])/10)[/CODE]
getting closer I got to: [CODE][6, 0, 13, 6][/CODE] I know why it's happened, going to have to work out how to fix it. and it all has to do with: (x*y-1) because: -(1*1-1) gives -0, -(2*1-1) and -(1*2-1) give -1,But -(2*2-1) gives -(3) so it skips -2 the place it should go in. |
I wouldn't worry about carries; just fix them in the end.
|
[QUOTE=CRGreathouse;239221]I wouldn't worry about carries; just fix them in the end.[/QUOTE]
It's not a carry problem it's a placement formula error problem because I should of had a formula to place the 6 in the 2nd from right not the very right. |
[QUOTE=science_man_88;239223]It's not a carry problem it's a placement formula error problem because I should of had a formula to place the 6 in the 2nd from right not the very right.[/QUOTE]
Every time you move one digit to the left on the multiplier you need to move the result one digit to the left. :smile: |
[QUOTE=CRGreathouse;239229]Every time you move one digit to the left on the multiplier you need to move the result one digit to the left. :smile:[/QUOTE]
Instead of multiplying I should be adding lol and subtracting 1 I think. I'll try it. |
Got it working for 2 digits I'll check for 3. If it works I'll just need to chop off a 0 on the end.
|
[QUOTE=science_man_88;239238]Got it working for 2 digits I'll check for 3. If it works I'll just need to chop off a 0 on the end.[/QUOTE]
oh and then the carry has to be done in the additions. |
[CODE]longmultiply(a,b)= c=eval(Vec(Str(a)));d=eval(Vec(Str(b)));carry=0;answer=vector(length(d)+length(c));forstep(x=#d,1,[-1],forstep(y=#c,1,[-1],n=((c[y]*d[x])+carry)%10;answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);carry=floor((c[y]*d[x])/10)));forstep(w=#answer,2,-1,answer[w-1]=answer[w-1]+floor(answer[w]/10);answer[w]=answer[w]%10)[/CODE]
One thing I can't seem to do is shift it maybe I'll have to add it in as another loop, this code is making me loopy. |
[CODE]longmultiply(a,b)= c=eval(Vec(Str(a)));d=eval(Vec(Str(b)));carry=0;answer=vector(length(d)+length(c));forstep(x=#d,1,[-1],forstepn=((c[y]*d[x])+carry)%10;answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);carry=floor((c[y]*d[x])/10)));forstep(w=#answer,1,-1,if(w>1,answer[w-1]=answer[w-1]+floor(answer[w]/10));answer[w]=answer[w]%10;if(w<#answer,answer[w+1]=answer[w]))[/CODE] There we are lol, only 398 characters long! Anyone want to better this ? sorry even more for a good copy apparently lol, well I've lost a part I'll be back.
|
[CODE]longmultiply(a,b)= c=eval(Vec(Str(a)));d=eval(Vec(Str(b)));carry=0;answer=vector(length(d)+length(c));forstep(x=#d,1,[-1],forstep(y=#c,1,[-1],n=((c[y]*d[x])+carry)%10;answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);carry=floor((c[y]*d[x])/10)));forstep(w=#answer,1,-1,if(w>1,answer[w-1]=answer[w-1]+floor(answer[w]/10));answer[w]=answer[w]%10;if(w<#answer,answer[w+1]=answer[w]))[/CODE]
Found my best answer in Pari still only works if the answer is n+m digits. 399 characters. not sure how to compensate for the last part credit should go to CRG in fact I have to give him credit for the others as well I'll correct that. |
add[CODE];answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer[/CODE] onto it I think it then works.
|
[QUOTE=science_man_88;239257]add[CODE];answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer[/CODE] onto it I think it then works.[/QUOTE]
[CODE]longmultiply(a,b)= [COLOR="Red"]c=eval(Vec(Str(a)));d=eval(Vec(Str(b)))[/COLOR];carry=0;[COLOR="red"]answer=vector(length(d)+length(c))[/COLOR];forstep(x=#d,1,[-1],forstep(y=#c,1,[-1],n=((c[y]*d[x])+carry)%10;answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);carry=floor((c[y]*d[x])/10)));forstep(w=#answer,1,-1,if(w>1,answer[w-1]=answer[w-1]+floor(answer[w]/10));answer[w]=answer[w]%10;if(w<#answer,answer[w+1]=answer[w]));answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer[/CODE] I believe. Now it's 471 characters, That's a long code in my books lol. found a flaw in my add on. without the addon lower ones work with it low ones don't. But for higher ones it messes it up the other way round I think. 4now I've got 33*33 not working man I suck at this. |
[CODE]longmultiply(a,b)= c=eval(Vec(Str(a)));d=eval(Vec(Str(b)));carry=0;answer=vector(length(d)+length(c));forstep(x=#d,1,[-1],forstep(y=#c,1,[-1],n=((c[y]*d[x])+carry)%10;answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);carry=floor((c[y]*d[x])/10)));forstep(w=#answer,2,-1,answer[w-1]=answer[w-1]+floor(answer[w]/10);answer[w]=answer[w]%10;if(w<#answer,answer[w+1]=answer[w]));answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer[/CODE]
I'm quiting this for now if this isn't it lol. It took me multiple post on here and 255 + answers in Pari. |
[CODE]*** bug in PARI/GP (Segmentation Fault), please report[/CODE]
not good I may close Pari lol just to restart. |
[QUOTE=science_man_88;239269][CODE]longmultiply(a,b)= c=eval(Vec(Str(a)));d=eval(Vec(Str(b)));carry=0;answer=vector(length(d)+length(c));forstep(x=#d,1,[-1],forstep(y=#c,1,[-1],n=((c[y]*d[x])+carry)%10;answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n);carry=floor((c[y]*d[x])/10)));forstep(w=#answer,2,-1,answer[w-1]=answer[w-1]+floor(answer[w]/10);answer[w]=answer[w]%10;if(w<#answer,answer[w+1]=answer[w]));answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer[/CODE]
I'm quiting this for now if this isn't it lol. It took me multiple post on here and 255 + answers in Pari.[/QUOTE] and adding: [CODE]if(answer==eval(Vec(Str(a*b))),print(answer))[/CODE] I get: [CODE][1, 0, 0, 1] [1, 0, 1, 2] [1, 0, 2, 3] [1, 0, 3, 4] [1, 0, 4, 5] [1, 0, 5, 6] [1, 0, 6, 7] [1, 0, 7, 8] [1, 0, 8, 9] [1, 0, 0, 8] [1, 0, 2, 0] [1, 0, 3, 2] [1, 0, 4, 4] [1, 0, 5, 6] [1, 0, 0, 1] [1, 0, 0, 8] [1, 0, 2, 2] [1, 0, 3, 6] [1, 0, 5, 0] [1, 0, 6, 4] [1, 0, 7, 8] [1, 0, 0, 8] [1, 0, 2, 4] [1, 0, 4, 0] [1, 0, 5, 6] [1, 0, 0, 7] [1, 0, 2, 6] [1, 0, 4, 5] [1, 0, 1, 2] [1, 0, 0, 8] [1, 0, 3, 2] [1, 0, 5, 6] [1, 0, 2, 3] [1, 0, 2, 4] [1, 0, 5, 6] [1, 0, 2, 3] [1, 0, 5, 6] [1, 0, 8, 9] [1, 0, 1, 2] [1, 0, 3, 4] [1, 0, 0, 8] [1, 0, 5, 6] [1, 0, 2, 9] [1, 0, 7, 8] [1, 0, 0, 1] [1, 0, 1, 2] [1, 0, 2, 3] [1, 0, 3, 4] [1, 0, 4, 5] [1, 0, 5, 6] [1, 0, 6, 7] [1, 0, 7, 8] [1, 0, 8, 9][/CODE] far from what is expected for: [CODE]for(x=1,100,for(y=1,100,longmultiply(x,y)))[/CODE] |
First of all, your code seems to be nearly there -- it works in many cases. Some of the cases where it doesn't work are as simple as having an initial 0 -- in those cases you can just replace the final
[code];answer[/code] with [code];if(answer[1],answer,vecextract(answer,2^#answer-2))[/code] to drop the zero. (I suppose you should also test to see if one number is 0 since in that case you'll only need one digit.) I'm not sure what the problem is in the other case; I've avoided looking at your code since it's not formatted. But keep looking at the cases that it fails at -- why those and not others? What's going wrong there? |
[QUOTE=CRGreathouse;239288]First of all, your code seems to be nearly there -- it works in many cases. Some of the cases where it doesn't work are as simple as having an initial 0 -- in those cases you can just replace the final
[code];answer[/code] with [code];if(answer[1],answer,vecextract(answer,2^#answer-2))[/code] to drop the zero. (I suppose you should also test to see if one number is 0 since in that case you'll only need one digit.) I'm not sure what the problem is in the other case; I've avoided looking at your code since it's not formatted. But keep looking at the cases that it fails at -- why those and not others? What's going wrong there?[/QUOTE] [CODE]longmultiply(a,b)= { [COLOR="Red"]c=eval(Vec(Str(a))); d=eval(Vec(Str(b)));[/COLOR] carry=0; [COLOR="red"]answer=vector(length(d)+length(c));[/COLOR] forstep(x=#d,1,[-1], forstep(y=#c,1,[-1], n=((c[y]*d[x])+carry)%10; answer[#answer-((#d-x+1)+(#c-y+1)-1)]=(answer[#answer-((#d-x+1)+(#c-y+1)-1)]+n); carry=floor((c[y]*d[x])/10))); forstep(w=#answer,2,-1, answer[w-1]=answer[w-1]+floor(answer[w]/10); answer[w]=answer[w]%10; if(w<#answer,answer[w+1]=answer[w])); answer[2]=answer[1]%10; answer[1]=floor(answer[1]/10); answer }[/CODE] is this formatted better ? |
[QUOTE=science_man_88;239292]is this formatted better ?[/QUOTE]
A little. Here's how I'd format it: [code]longmultiply(a,b)= { my(c=eval(Vec(Str(a))),d=eval(Vec(Str(b))),carry=0,answer=vector(#c+#d)); forstep(x=#d,1,[-1], forstep(y=#c,1,[-1], n=((c[y]*d[x])+carry)%10; answer[#answer-((#d-x+1)+(#c-y+1)-1)] += n; carry=(c[y]*d[x])\10 ) ); forstep(w=#answer,2,-1, answer[w-1] += answer[w]\10; answer[w]=answer[w]%10; if(w<#answer,answer[w+1]=answer[w]) ); answer[2]=answer[1]%10; answer[1]=answer[1]\10; answer };[/code] Also changed: length(foo) to #foo, floor(foo/10) to foo\10, foo = foo + n to foo += n, local variables put into a my block. (This is important if you want to avoid clobbering variables! Always do this!) |
[QUOTE=CRGreathouse;239295]A little. Here's how I'd format it:
[code]longmultiply(a,b)= { my(c=eval(Vec(Str(a))),d=eval(Vec(Str(b))),carry=0,answer=vector(#c+#d)); forstep(x=#d,1,[-1], forstep(y=#c,1,[-1], n=((c[y]*d[x])+carry)%10; answer[#answer-((#d-x+1)+(#c-y+1)-1)] += n; carry=(c[y]*d[x])\10 ) ); forstep(w=#answer,2,-1, answer[w-1] += answer[w]\10; answer[w]=answer[w]%10; if(w<#answer,answer[w+1]=answer[w]) ); answer[2]=answer[1]%10; [COLOR="Red"]answer[1]=answer[1]\10;[/COLOR] answer };[/code] Also changed: length(foo) to #foo, floor(foo/10) to foo\10, foo = foo + n to foo += n, local variables put into a my block. (This is important if you want to avoid clobbering variables! Always do this!)[/QUOTE] the red is the starts with 0 error, I should add an if around it checking if answer[1]>10 first if so i doubt after the previous step that answer[1] is useful. |
As for getting a correct answer, I'd rather write my own than correct yours:
[code]longmultiply(a,b)= { a=eval(Vec(Str(a))); b=eval(Vec(Str(b))); my(res=vector(#a+#b),carry=0); for(i=1,#a, for(j=1,#b, res[i+j] += a[i]*b[j] ) ); forstep(i=#res,2,-1, res[i] += carry; carry = res[i]\10; res[i] -= 10 * carry ); res[1] += carry; if(res[1], res , vecextract(res,2^#res-2) ) };[/code] |
[QUOTE=science_man_88;239298]the red is the starts with 0 error, I should add an if around it checking if answer[1]>10 first if so i doubt after the previous step that answer[1] is useful.[/QUOTE]
wrong again that caused more errors to fix. Now I see a logic flaw on my part and using n+m as the length of answer. |
[QUOTE=science_man_88;239298]the red is the starts with 0 error, I should add an if around it checking if answer[1]>10 first if so i doubt after the previous step that answer[1] is useful.[/QUOTE]
I don't know what the purpose of that line is; answer[1] will never be more than 9 (if the program is correct up to that point), but even if it was you wouldn't want to divide by 10. [QUOTE=science_man_88;239301]wrong again that caused more errors to fix. Now I see a logic flaw on my part and using n+m as the length of answer.[/QUOTE] The length is good. Look at my answer (post #1816). |
[QUOTE=CRGreathouse;239300]As for getting a correct answer, I'd rather write my own than correct yours:
[code]longmultiply(a,b)= { a=eval(Vec(Str(a))); b=eval(Vec(Str(b))); my(res=vector(#a+#b),carry=0); for(i=1,#a, for(j=1,#b, res[i+j] += a[i]*b[j] ) ); forstep(i=#res,2,-1, res[i] += carry; carry = res[i]\10; res[i] -= 10 * carry ); res[1] += carry; if(res[1], res , vecextract(res,2^#res-2) ) };[/code][/QUOTE] Oh,Really ? [CODE][0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0] [0, 1, 0, 0][/CODE] Is what I got when I added print(answer) on the end. |
[QUOTE=science_man_88;239303]Is what I got when I added print(answer) on the end.[/QUOTE]
Why would you print the result of the global variable answer? 1. My function already returns a result; no need to modify it to see the answer. 2. My function doesn't use "answer" in any way, so it's no surprise that it still has the last value it had when your function used it. 3. If your function had encapsulated its variables with local() or my() then you wouldn't have changed the global "answer" in the first place so you wouldn't get this strange result. This is one of the benefits of using local() and my()! Remember: Amateurs print, professionals return. Try something like this: [code]for(a=1,1e3,for(b=1,1e3,if(longmultiply(a,b)!=eval(Vec(Str(a*b))),print(a"*"b))))[/code] Of course if you're really confident, you can even do [code]for(a=1,1e3,for(b=1,1e3,if(longmultiply(a,b)!=eval(Vec(Str(a*b))),return([a,b]))))[/code] |
[QUOTE=CRGreathouse;239304]Why would you print the result of the global variable answer?
1. My function already returns a result; no need to modify it to see the answer. 2. My function doesn't use "answer" in any way, so it's no surprise that it still has the last value it had when your function used it. 3. If your function had encapsulated its variables with local() or my() then you wouldn't have changed the global "answer" in the first place so you wouldn't get this strange result. This is one of the benefits of using local() and my()! Remember: Amateurs print, professionals return.[/QUOTE] I'm not a professional, I never intend to be branded professional. |
[QUOTE=CRGreathouse;239295]A little. Here's how I'd format it:
[code]longmultiply(a,b)= { my(c=eval(Vec(Str(a))),d=eval(Vec(Str(b))),carry=0,answer=vector(#c+#d)); forstep(x=#d,1,[-1], forstep(y=#c,1,[-1], n=((c[y]*d[x])+carry)%10; answer[#answer-((#d-x+1)+(#c-y+1)-1)] += n; carry=(c[y]*d[x])\10 ) ); forstep(w=#answer,2,-1, answer[w-1] += answer[w]\10; answer[w]=answer[w]%10; if(w<#answer,answer[w+1]=answer[w]) ); answer[2]=answer[1]%10; answer[1]=answer[1]\10; answer };[/code] Also changed: length(foo) to #foo, floor(foo/10) to foo\10, foo = foo + n to foo += n, local variables put into a my block. (This is important if you want to avoid clobbering variables! Always do this!)[/QUOTE] my() to me seems like an easy way to admit something like .[B][U]I[/U][/B] can't find the capitals at the beginning of a sentence. |
[QUOTE=science_man_88;239305]I'm not a professional, I never intend to be branded professional.[/QUOTE]
I'm just trying to convince you to do things the 'right' way since they will make things easier for you. If you did [code]longmultiply(a,b)={ \\ blah blah blah... print(answer) };[/code] then you couldn't test your results in a loop like I did above (and you did yourself in a similar loop). If you did [code]times10(n)={ a = 5; b = times2(n); a*b }; times2(n)={ a = 2; a * n }; times10(1)[/code] then it doesn't work properly (why?)... but [code]times10(n)={ my(a = 5, b = times2(n)); a*b }; times2(n)={ my(a = 2); a * n }; times10(1)[/code] does. |
[QUOTE=CRGreathouse;239302]I don't know what the purpose of that line is; answer[1] will never be more than 9 (if the program is correct up to that point), but even if it was you wouldn't want to divide by 10.
The length is good. Look at my answer (post #1816).[/QUOTE] 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. |
[QUOTE=CRGreathouse;239307]
If you did [code]times10(n)={ a = 5; b = times2(n); a*b }; times2(n)={ a = 2; a * n }; times10(1)[/code] then it doesn't work properly (why?)... but [/QUOTE] lame excuse. and it's simple 2 variables named a. |
[QUOTE=science_man_88;239310]lame excuse. and it's simple 2 variables named a.[/QUOTE]
So let's step back and think about that. You can't call times2() from a function unless you check it to make sure that none of its variables are the same as the ones in the calling function. But what if times2() called another function? [code]times2(n)={ a = minus1(n); if(a, times2(a) + 2, 2) }; minus1(n)={ n - 1 };[/code] Then you need to check it as well. If it's programmed the way I have it above, you see that the two variables named a collide, but you can make times10 work by changing the variable: [code]times10(n)={ b = times2(n); theVariableFormerlyKnownAsA = 5; theVariableFormerlyKnownAsA*b };[/code] But what if I had instead written minus1() as [code]minus1(n)={ b = n; b-- }[/code] Now suddenly you can't call times2() without checking for collisions in that function, or any function that it calls. Of course this goes on forever: what if minus1() called another function? You'd have to check that too. This becomes surprisingly common with complex functions calling each other, with how few major variable names we use. But the whole issue can be solved in one step -- no looking up functions, and functions in functions, and functions in functions in functions, ... -- by using my(). |
[QUOTE=science_man_88;239308]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.[/QUOTE]
If you look at my code, the formula is embarrassingly simple. (I could hardly believe it when I saw that pretty much everything cancels...) |
[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); ); }[/CODE] good enough code ? I added my() in just to satisfy you even though i see no point of it. |
[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) ); }[/CODE] 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. |
[QUOTE=science_man_88;239343]good enough code ?[/QUOTE]
I don't understand where the constants 5 and 6 come from. [QUOTE=science_man_88;239343]I added my() in just to satisfy you even though i see no point of it.[/QUOTE] 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. |
[QUOTE=CRGreathouse;239374]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.[/QUOTE] 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. |
[QUOTE=science_man_88;239348][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) ); }[/CODE] 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.[/QUOTE] Very nice code formatting!:smile: 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. |
[QUOTE=CRGreathouse;239377]Very nice code formatting!:smile:
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.[/QUOTE] 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. |
[QUOTE=science_man_88;239376]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.[/QUOTE]
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=[]) [COLOR="Red"]for(x=1,N, if(x>5,break(1)); row=eval(Vec(Str(11^(x-1)))); print(row));[/COLOR] prevrow=row; [COLOR="Blue"]for(y=6,N, for(p=2,#prevrow, row[p]=prevrow[p-1]+prevrow[p]); row=concat(row,1); prevrow=row; print(row); );[/COLOR] }[/code] I suspect that the indentation reflects your intent and the parentheses need to be moved to match. |
[QUOTE=science_man_88;239378]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.[/QUOTE]
Actually... :blush: it looks like that part of your code is OK. |
[QUOTE=CRGreathouse;239379]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=[]) [COLOR="Red"]for(x=1,N, if(x>5,break(1)); row=eval(Vec(Str(11^(x-1)))); print(row));[/COLOR] prevrow=row; [COLOR="Blue"]for(y=6,N, for(p=2,#prevrow, row[p]=prevrow[p-1]+prevrow[p]); row=concat(row,1); prevrow=row; print(row); );[/COLOR] }[/code] I suspect that the indentation reflects your intent and the parentheses need to be moved to match.[/QUOTE] copying your format I found another missed semi colon |
[QUOTE=CRGreathouse;239380]Actually... :blush: it looks like that part of your code is OK.[/QUOTE]
[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) ); }[/CODE] RLE() takes the [COLOR="Red"]string[/COLOR] puts the first letter in[COLOR="Lime"] Letter[/COLOR] and puts an array of characters in [COLOR="Red"]string[/COLOR] and start [COLOR="DarkOrange"]number[/COLOR] 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 [COLOR="Lime"]Letter[/COLOR] if it matches number increases.Otherwise it prints ([COLOR="rgb(255, 140, 0)"]number[/COLOR])[COLOR="Lime"]Letter[/COLOR] then sets [COLOR="Lime"]Letter [/COLOR]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. |
[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=1) );print1("("number")"Letter) }[/CODE] so it now becomes this and should work. [QUOTE](1)m(1)i(2)s(1)i(2)s(1)i(2)p(1)i[/QUOTE] |
[QUOTE=science_man_88;239381]copying your format I found another missed semi colon[/QUOTE]
no the semi coon is the only thing I think lol. |
[QUOTE=science_man_88;239384][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=1) );print1("("number")"Letter) }[/CODE] so it now becomes this and should work.[/QUOTE] now I need a decoding part this semi falls under finding if a string is numerical, I'll start to try to decode it again. |
[url]http://rosettacode.org/wiki/Substring[/url]
too hard ? wasn't this done on this forum for FaR lol |
[QUOTE=science_man_88;239343]I added my() in just to satisfy you even though i see no point of it.[/QUOTE]
I thought you'd like this (maybe it'll make you feel better?) -- see #4: [url]http://www.techradar.com/news/software/applications/10-mistakes-every-programmer-makes-909424[/url] |
[QUOTE=CRGreathouse;239468]I thought you'd like this (maybe it'll make you feel better?) -- see #4:
[url]http://www.techradar.com/news/software/applications/10-mistakes-every-programmer-makes-909424[/url][/QUOTE] how does my help that ? nevermind I see what you mean lol. |
| All times are UTC. The time now is 23:20. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.