473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Multiplying large numbers

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?
Apr 10 '07 #1
9 3875
On Apr 10, 12:50 am, Cogito <nos...@nospam.nospamwrote:
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.

Apr 10 '07 #2
In comp.lang.javascript message <jo5m13hmpej1nb3gbpvf9kt5b9o31rj5ta@4ax.
com>, Tue, 10 Apr 2007 04:50:44, Cogito <no****@nospam.nospamposted:
>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.
Apr 10 '07 #3
On Tue, 10 Apr 2007 22:54:37 +0100, Dr J R Stockton
<jr*@merlyn.demon.co.ukwrote:
>In comp.lang.javascript message <jo5m13hmpej1nb3gbpvf9kt5b9o31rj5ta@4ax.
com>, Tue, 10 Apr 2007 04:50:44, Cogito <no****@nospam.nospamposted:
>>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.
Apr 11 '07 #4
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?
--

Apr 11 '07 #5
Cogito wrote:
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
Apr 11 '07 #6
On 11 Apr 2007 08:02:26 -0700, Lee <RE**************@cox.netwrote:
>
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?
Apr 12 '07 #7
Lee
Cogito said:
>
On 11 Apr 2007 08:02:26 -0700, Lee <RE**************@cox.netwrote:
>>
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>


--

Apr 12 '07 #8
On 11 Apr 2007 22:14:04 -0700, Lee <RE**************@cox.netwrote:
>>
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.
Apr 12 '07 #9
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/
--

Apr 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied...
5
by: Edith Gross | last post by:
I'd like to add to unsigned integers and find out whether a carry occurs. Can I do that in a simple and fast way? A similar problem is subtracting with borrow and the multiplication of two...
10
by: Tuvas | last post by:
I've been thinking about writing a program to generate the world's largest prime numbers, just for the fun of it. This would require being able to hold an 8000000 digit number into memory (25...
18
by: Brad | last post by:
I have a problem in that I need to change a large decimal value into its hex equivalent. The largest decimal value I need to represent as hex is 25516777215. The problem here is that this number...
18
by: Zero | last post by:
Hi, I am calculating an integer to the pwer of a large integer, e.g. 2^5000. It turns out that the result I always get is zero. I am sure that the result is too large to store in such type...
2
by: k | last post by:
I have aproblem when multiplying 2 float 638.9 * 382.8 should = 244570.92 results giving 244570.922 both numbers are float variables , tried using double to...
22
by: Frinton | last post by:
Hi, I am trying to do some calculations on large numbers (ie 7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it doesn't get it quite right. Its always somewhere between 10...
4
by: Tenacious | last post by:
Why is it that when I multiply two double values like this: double Multi = 100.0; double Force = 0.29; double Result = Multi * Force; I get Result = 28.999999999996 instead of 29.0? I'm not...
1
by: xvader | last post by:
Hi, I'm new here, and I'm having a problem implementing a multiplication method for a class called VeryLongInt. It's for a homework problem. As far as I know, it should work just like the Bignum...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.