Connecting Tech Pros Worldwide Forums | Help | Site Map

Multiplying large numbers

Cogito
Guest
 
Posts: n/a
#1: Apr 10 '07
I would like to calculate factorial numbers that produce results of
say 100 digits. What is the best way of doing it in Javascript?
Can I define a variable and somehow have control on each of its digits
individually?
Can I multiply two relatively small numbers and determine that there
was a carry?

brunascle
Guest
 
Posts: n/a
#2: Apr 10 '07

re: Multiplying large numbers


On Apr 10, 12:50 am, Cogito <nos...@nospam.nospamwrote:
Quote:
I would like to calculate factorial numbers that produce results of
say 100 digits. What is the best way of doing it in Javascript?
Can I define a variable and somehow have control on each of its digits
individually?
Can I multiply two relatively small numbers and determine that there
was a carry?
this guy's code might be of help: http://www-cs-students.stanford.edu/~tjw/jsbn/

he has released some big-int and RSA cryptography javascript code
under a BSD license.

Dr J R Stockton
Guest
 
Posts: n/a
#3: Apr 10 '07

re: Multiplying large numbers


In comp.lang.javascript message <jo5m13hmpej1nb3gbpvf9kt5b9o31rj5ta@4ax.
com>, Tue, 10 Apr 2007 04:50:44, Cogito <nospam@nospam.nospamposted:
Quote:
>I would like to calculate factorial numbers that produce results of
>say 100 digits. What is the best way of doing it in Javascript?
>Can I define a variable and somehow have control on each of its digits
>individually?
>Can I multiply two relatively small numbers and determine that there
>was a carry?
Perhaps you learned to multiply with pencil and paper at school?

Just represent each number as an array of digits, and implement the
method that you learned. I did that, in Pascal, in the program which
eventually became LONGCALC.PAS, via sig line 3. It's also in <URL:http:
//www.merlyn.demon.co.uk/js-misc0.htm#CDC, functions Add, Twice, Halve,
adequate for a limited situation.

If approximate results will do, see <URL:http://www.merlyn.demon.co.uk/
js-maths.htm#BF=1000000! = 8.263929417903883e5565708, 10000000! =
1.202421895523417e65657059 .

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm- also batprogs.htm.
Cogito
Guest
 
Posts: n/a
#4: Apr 11 '07

re: Multiplying large numbers


On Tue, 10 Apr 2007 22:54:37 +0100, Dr J R Stockton
<jrs@merlyn.demon.co.ukwrote:
Quote:
>In comp.lang.javascript message <jo5m13hmpej1nb3gbpvf9kt5b9o31rj5ta@4ax.
>com>, Tue, 10 Apr 2007 04:50:44, Cogito <nospam@nospam.nospamposted:
Quote:
>>I would like to calculate factorial numbers that produce results of
>>say 100 digits. What is the best way of doing it in Javascript?
>>Can I define a variable and somehow have control on each of its digits
>>individually?
>>Can I multiply two relatively small numbers and determine that there
>>was a carry?
>
>Perhaps you learned to multiply with pencil and paper at school?
>
>Just represent each number as an array of digits, and implement the
>method that you learned. I did that, in Pascal, in the program which
>eventually became LONGCALC.PAS, via sig line 3. It's also in <URL:http:
>//www.merlyn.demon.co.uk/js-misc0.htm#CDC, functions Add, Twice, Halve,
>adequate for a limited situation.
How does this help me?
Yes, I know how to multiply. And I even wrote the very same program
I'm attempting now in Javascrip, some 30 years ago in PL/1. That too
does not help me one bit with the Javascript challenge.
Lee
Guest
 
Posts: n/a
#5: Apr 11 '07

re: Multiplying large numbers


Cogito said:
Quote:
>How does this help me?
>Yes, I know how to multiply. And I even wrote the very same program
>I'm attempting now in Javascrip, some 30 years ago in PL/1. That too
>does not help me one bit with the Javascript challenge.
If you know how to write the code in another language, what remaining
questions do you have about writing it in Javascript?


--

wolfgang zeidler
Guest
 
Posts: n/a
#6: Apr 11 '07

re: Multiplying large numbers


Cogito wrote:
Quote:
I would like to calculate factorial numbers that produce results of
say 100 digits. What is the best way of doing it in Javascript?
Can I define a variable and somehow have control on each of its digits
individually?
Can I multiply two relatively small numbers and determine that there
was a carry?
If you need only addition and multiplikation you may use:
http://home.arcor.de/wzwz.de/wiki/ebs/en.htm
http://home.arcor.de/wzwz.de/wiki/ebs/ebs.zip
(public domain)

--
http:\wzwz.de\mail
Cogito
Guest
 
Posts: n/a
#7: Apr 12 '07

re: Multiplying large numbers


On 11 Apr 2007 08:02:26 -0700, Lee <REM0VElbspamtrap@cox.netwrote:
Quote:
>
>If you know how to write the code in another language, what remaining
>questions do you have about writing it in Javascript?
Let's say that I use the random function to select 2 random numbers, I
multiply them and want to determine if the fifth digit of the result
is a 7. How would I do it?
Lee
Guest
 
Posts: n/a
#8: Apr 12 '07

re: Multiplying large numbers


Cogito said:
Quote:
>
>On 11 Apr 2007 08:02:26 -0700, Lee <REM0VElbspamtrap@cox.netwrote:
>
Quote:
>>
>>If you know how to write the code in another language, what remaining
>>questions do you have about writing it in Javascript?
>
>Let's say that I use the random function to select 2 random numbers, I
>multiply them and want to determine if the fifth digit of the result
>is a 7. How would I do it?
<html>
<body>
<script type="text/javascript">
var count=0;
for ( var i=0;i<1000;i++ ) {
// select two randomish numbers
var a=Math.floor(Math.random()*100+100);
var b=Math.floor(Math.random()*100+100);
// multiply them
var p=a*b;
// check to see if the 5th digit is a 7
if ( !p.toString().search(/^\d\d\d\d7/) ) {
document.write("<b>"+p+"</b");
count++;
} else {
document.write(p+" ");
}
}
document.write("<hr>The fifth digit was &quot;7&quot; "
+count+" times out of 1000.");
</script>
</body>
</html>




--

Cogito
Guest
 
Posts: n/a
#9: Apr 12 '07

re: Multiplying large numbers


On 11 Apr 2007 22:14:04 -0700, Lee <REM0VElbspamtrap@cox.netwrote:
Quote:
Quote:
>>
>>Let's say that I use the random function to select 2 random numbers, I
>>multiply them and want to determine if the fifth digit of the result
>>is a 7. How would I do it?
>
><html>
><body>
><script type="text/javascript">
var count=0;
for ( var i=0;i<1000;i++ ) {
// select two randomish numbers
var a=Math.floor(Math.random()*100+100);
var b=Math.floor(Math.random()*100+100);
// multiply them
var p=a*b;
// check to see if the 5th digit is a 7
if ( !p.toString().search(/^\d\d\d\d7/) ) {
document.write("<b>"+p+"</b");
count++;
} else {
document.write(p+" ");
}
}
document.write("<hr>The fifth digit was &quot;7&quot; "
+count+" times out of 1000.");
></script>
></body>
></html>
>
>
Thanks for the code. Looks like there are lots of javascript functions
I never heard of. Where can I find a description of toString and what
all the parameters mean?
While on this note, can you recommend a good book to learn Javascript?
Perhaps I need a better understanding of the language and learning
from sample code may not be enough for what I want to do now.
Lee
Guest
 
Posts: n/a
#10: Apr 12 '07

re: Multiplying large numbers


Cogito said:
Quote:
>Thanks for the code. Looks like there are lots of javascript functions
>I never heard of. Where can I find a description of toString and what
>all the parameters mean?
>While on this note, can you recommend a good book to learn Javascript?
>Perhaps I need a better understanding of the language and learning
>from sample code may not be enough for what I want to do now.
A good place to start is:
http://www.jibbering.com/faq/

One good resource for Javascript functions is:
http://docs.sun.com/source/816-6408-10/


--

Closed Thread