mersenneforum.org  

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

Reply
 
Thread Tools
Old 2010-11-30, 01:55   #1816
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

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)
	)
};
CRGreathouse is offline   Reply With Quote
Old 2010-11-30, 01:55   #1817
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

838410 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
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.
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.
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 01:57   #1818
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

597910 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
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.
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:
Originally Posted by science_man_88 View Post
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.
The length is good. Look at my answer (post #1816).

Last fiddled with by CRGreathouse on 2010-11-30 at 01:58
CRGreathouse is offline   Reply With Quote
Old 2010-11-30, 01:58   #1819
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
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)
	)
};
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]
Is what I got when I added print(answer) on the end.
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 02:02   #1820
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
Is what I got when I added print(answer) on the end.
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))))
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]))))

Last fiddled with by CRGreathouse on 2010-11-30 at 02:05
CRGreathouse is offline   Reply With Quote
Old 2010-11-30, 02:07   #1821
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
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.
I'm not a professional, I never intend to be branded professional.
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 02:11   #1822
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
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!)
my() to me seems like an easy way to admit something like .I can't find the capitals at the beginning of a sentence.
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 02:15   #1823
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
I'm not a professional, I never intend to be branded professional.
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)
};
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)
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)
does.
CRGreathouse is offline   Reply With Quote
Old 2010-11-30, 02:16   #1824
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
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).
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.
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 02:21   #1825
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
If you did
Code:
times10(n)={
  a = 5;
  b = times2(n);
  a*b
};
times2(n)={
  a = 2;
  a * n
};
times10(1)
then it doesn't work properly (why?)... but
lame excuse. and it's simple 2 variables named a.
science_man_88 is offline   Reply With Quote
Old 2010-11-30, 02:30   #1826
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
lame excuse. and it's simple 2 variables named a.
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
};
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
};
But what if I had instead written minus1() as
Code:
minus1(n)={
  b = n;
  b--
}
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().
CRGreathouse is offline   Reply With Quote
Reply



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:01.


Fri Aug 6 23:01:47 UTC 2021 up 14 days, 17:30, 1 user, load averages: 3.99, 4.10, 4.01

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.