mersenneforum.org  

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

Reply
 
Thread Tools
Old 2010-11-29, 21:17   #1805
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

838410 Posts
Default

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]))
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.

Last fiddled with by science_man_88 on 2010-11-29 at 21:41
science_man_88 is offline   Reply With Quote
Old 2010-11-29, 22:04   #1806
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

838410 Posts
Default

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]))
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.
science_man_88 is offline   Reply With Quote
Old 2010-11-29, 22:37   #1807
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

add
Code:
;answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer
onto it I think it then works.
science_man_88 is offline   Reply With Quote
Old 2010-11-29, 22:39   #1808
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
add
Code:
;answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer
onto it I think it then works.
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]));answer[2]=answer[1]%10;answer[1]=floor(answer[1]/10);answer
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.

Last fiddled with by science_man_88 on 2010-11-29 at 23:10
science_man_88 is offline   Reply With Quote
Old 2010-11-29, 23:19   #1809
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

20C016 Posts
Default

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
I'm quiting this for now if this isn't it lol. It took me multiple post on here and 255 + answers in Pari.

Last fiddled with by science_man_88 on 2010-11-29 at 23:29
science_man_88 is offline   Reply With Quote
Old 2010-11-29, 23:41   #1810
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

838410 Posts
Default

Code:
***   bug in PARI/GP (Segmentation Fault), please report
not good I may close Pari lol just to restart.
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 00:08   #1811
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
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
I'm quiting this for now if this isn't it lol. It took me multiple post on here and 255 + answers in Pari.
and adding:

Code:
if(answer==eval(Vec(Str(a*b))),print(answer))
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]
far from what is expected for:

Code:
for(x=1,100,for(y=1,100,longmultiply(x,y)))
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 01:22   #1812
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

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
with
Code:
;if(answer[1],answer,vecextract(answer,2^#answer-2))
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?
CRGreathouse is offline   Reply With Quote
Old 2010-11-30, 01:27   #1813
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
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
with
Code:
;if(answer[1],answer,vecextract(answer,2^#answer-2))
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?
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
}
is this formatted better ?

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

175B16 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
is this formatted better ?
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
};
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!)

Last fiddled with by CRGreathouse on 2010-11-30 at 01:44
CRGreathouse is offline   Reply With Quote
Old 2010-11-30, 01:49   #1815
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

838410 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
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
};
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!)
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.

Last fiddled with by science_man_88 on 2010-11-30 at 01:51
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:18 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.