Multiplying large numbers

C

Cogito

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?
 
B

brunascle

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.
 
D

Dr J R Stockton

In comp.lang.javascript message <jo5m13hmpej1nb3gbpvf9kt5b9o31rj5ta@4ax.
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

Cogito

In comp.lang.javascript message <jo5m13hmpej1nb3gbpvf9kt5b9o31rj5ta@4ax.


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.
 
L

Lee

Cogito said:
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?


--
 
W

wolfgang zeidler

C

Cogito

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?
 
L

Lee

Cogito said:
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>




--
 
C

Cogito

<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.
 
L

Lee

Cogito said:
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/


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top