Forum Moderators: open

Message Too Old, No Replies

i need help with my javascript project...can someone help me?

number crunching page

         

urgirljavabeginner

6:51 am on Apr 15, 2004 (gmt 0)

10+ Year Member



This project is designed to demonstrate that you have mastered iteration. (Naturally, you will still be exercising all your previously learned skills.)
Your task for this project is to create a number crunching page, that is, a page that provides the user with two computations that show the power of computer number crunching. The page you create will have

~a labeled area for entering a number,
~a button labeled Show Factors,
~a button labeled Show Triples,
~a textarea of several lines to contain the results of the computations,
~as before, a copyright statement at the bottom of your page and a last modified date, both in smaller-than-normal font.

Naturally, you are welcome (but not required) to make your page look nice, dressing it up with colors, or a picture or two, as you see fit.

urgirljavabeginner

6:59 am on Apr 15, 2004 (gmt 0)

10+ Year Member



Factors
When the user clicks the Show Factors button, your page must verify that the user has supplied a positive integer in the input area, and then compute and show all the prime factors of the given integer. (Math Refresher: multiplying together all the prime factors of a number gives you that number.) For instance, if the user supplied the number 18, your page displays:
Prime factors of 18:
2
3
3
(Hint: you may find the remainder operator ("%") useful here. This operator is like division ("/"), except that it computes the remainder rather than the quotient. For instance, 10 % 3 equals 1, and 12 % 3 equals 0.)

urgirljavabeginner

7:00 am on Apr 15, 2004 (gmt 0)

10+ Year Member



Pythagorean triples
When the user clicks the Show Triples button your page must verify that the user has supplied a positive integer in the input area, and then compute and show all the Pythagorean triples whose individual values are less than the given integer. Pythagorean triples are any three positive integers for which the square of the largest equals the sum of the squares of the other two. For example, 3, 4 and 5 are a Pythagorean triple because 32 + 42 = 52. Hence, if the user supplied the number 18, your page displays:
Pythagorean triples less than 18:
3, 4, 5
5, 12, 13
6, 8, 10
8, 15, 17
9, 12, 15
(Note that, although technically "4, 5, 3" is also a Pythagorean triple, it is not listed.)

i posted these other 2 so u know wat the prof. exactly wants. i hope you all can help me out. i need this project done by friday. please help me i'm deperate!

Purple Martin

7:22 am on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello urgirljavabeginner, welcome to Webmaster World.

I would rather not do your project work for you, as this would not help you learn.

If there is a specific issue that you are having trouble understanding, please ask about it and I'm sure we'll all be happy to explain it as best we can.

urgirljavabeginner

7:26 am on Apr 15, 2004 (gmt 0)

10+ Year Member



well..actually i need help understanding which functions to use for what teh prof is asking for. like for the factors. wat function do i use?

Wertigon

7:50 am on Apr 15, 2004 (gmt 0)

10+ Year Member



For the factors:

In programming, the number 0 is always the equivalent of the value "False". Hence you could make a for-loop that iterates through the code, checking to see if the modulus operator (%) returns false and if it does, break out that prime number. Something along the lines of

x = 0;
for (i = 2; i<num; i++)
{
if!(num % i)
{
prime[x] = i;
x++;
num = num/i;
i = 1;
}
}

What the above iteration does is to go through num, check if it is dividable with a prime, put the prime number in the array if it does exist, then divide the number with the prime.

Of course, this is mostly pseudo-code, you'll have to do the actual debugging yourself. ;)

urgirljavabeginner

7:54 am on Apr 15, 2004 (gmt 0)

10+ Year Member



wat is Pythagorean triples and how do i go about doin this?

Wertigon

8:04 am on Apr 15, 2004 (gmt 0)

10+ Year Member



Well... You know Pythagoras' formula right?

a2 + b2 = c2

So that a squared + b squared = c squared. Right?

A pythagoran triple is one where three integers can be put into the above formula. Example:

32 + 42 = 52

Gives the triple 3,4,5. Hope that helps some...

How to go at it? No idea, sorry. :/

urgirljavabeginner

8:56 am on Apr 15, 2004 (gmt 0)

10+ Year Member



to do my project. how do i go about so that i can factor any number the user types in?

Wertigon

9:27 am on Apr 15, 2004 (gmt 0)

10+ Year Member



I assume you know that the definition of a prime number is "A number that cannot be divided into integers by other means than itself and 1", but just for the record I'll mention it.

What you do is you simply break the factors out independently, then store them in an array.

E.g. the number 25 has the factors 5 and 5 (5*5 = 25, 5 is a prime number). What you do is to divide the number 25 with the lowest possible prime (in this case 5), and then divide the new quota with the lowest possible prime again.

So to pick out the first factor:

Start at two, since that is the lowest possible prime. Can you divide...

... 25 with 2? -> No
... 25 with 3? -> No
... 25 with 4? -> No
... 25 with 5? -> Yes

The first factor is in other words five.

Now for the second factor, divide the original number with the factor you picked out (25 / 5 in this case). Take the result of that division and divide again. Can you divide...

... 5 with 2? -> No
... 5 with 3? -> No
... 5 with 4? -> No
... 5 with 5? -> Yes

The second factor is also five. Now repeat this step as many times as neccessary. And if you *REALLY* want to impress your teach, make it tell you that it's a prime number if it is one... ;)

Hope that helps.

urgirljavabeginner

10:13 am on Apr 15, 2004 (gmt 0)

10+ Year Member



ok..that helps.

urgirljavabeginner

10:43 am on Apr 15, 2004 (gmt 0)

10+ Year Member



so..i'm assuming if i did this right..from all of your help this is it?

function factor(numm)
{
var newnum = numm;
var newtext = "";
var checker = 2;
while (checker*checker <= newnum)
{
if (newnum % checker == 0)
{
newtext = newtext + checker;
newnum = newnum/checker;
if (newnum!= 1)
{
newtext = newtext + ".";
}
}
else
{
checker++;
}
}
if (newnum!= 1)
{
newtext = newtext + newnum;
}
if (newtext == "" + numm)
{
newtext = "Prime: " + newtext;
}
return newtext;
}

tell me if that is correctly done

Wertigon

11:26 am on Apr 15, 2004 (gmt 0)

10+ Year Member



Well, I haven't actually been running it, but from a brief look of it you forgot one little detail; As it stands right now it'll only return the *LAST* factor.

My suggestion is that you use an array to store the factors in and iterate through them to post them.

urgirljavabeginner

12:37 pm on Apr 15, 2004 (gmt 0)

10+ Year Member



hmm ok

Bernard Marx

9:08 pm on Apr 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Interesting. I ran your code and it produced 2.2.2.2 for 16. 'It must be a dud' I thought, so I started my own version. Now, before you call me stupid, I'll do it instead.

2.2.2.2 is the prime decomposition of 16!

It just wasn't until I'll done mine that the penny dropped.

Here's mine anyway. It's still a code version of Wertigon's idea (others too). The code is slightly different to yours - a little shorter, and uses the Array::join method to put in whatever delimiter you want. If you don't put in a delimiter string, you get the result back as an array.

[pre]
// call
factor(35,'.')
// or: factor(35)
function factor(dividend,delimiter)
{
var primes = [1]
var check = 2

while(dividend!= 1)
{
while(dividend%check==0)
{
primes[primes.length] = check
dividend /= check
}
check++
}

return (delimiter)? primes.join(delimiter):primes

}
[/pre]

Wertigon

8:19 am on Apr 16, 2004 (gmt 0)

10+ Year Member



Doh, sorry, I was wrong... That script does indeed work, it was just me being kinda tired. =)

I also came up with an idea that could solve your other problem... You simply loop through it all checking to see if anything works. E.g.

x=0; 
a=0;
while (a <= num)
{
b=0;
while (b <= num)
{
c=0;
while (c <= num)
{
if ( a*a + b*b ) == (c*c)
{
triples[x] = a + "," + b + "," + c;
x++;
}
c++;
}
b++;
}
a++;
}

The disadvantage is that if num is ridiculously large, say 65535 or something, you're in for a treat... ;) It's the easy way out though. :p

urgirljavabeginner

1:15 pm on Apr 16, 2004 (gmt 0)

10+ Year Member



what i now would like to know is how do i put this triple that i made so it would come out from same textarea box that i made for the prime factors?

for(int a = 1; a < MAX; a++)
{
for(int b = a + 1; b < MAX; b++)
{
for(int c = b + 1; b < MAX; c++)
{
if(a*a + b*b = c*c)
{
//a, b, c is a solution
}
}
}
}

i would like to know how i would go about doin this?