Forum Moderators: open
~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.
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!
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. ;)
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.
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
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]
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
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?