/    Sign up×
Community /Pin to ProfileBookmark

Factoring Script Help!

Hello,

I have been trying to work at this script for about 3 days now but can’t seem to work out what I want to do. What I want to do is put two numbers (a, and b)into two fields, and then find two numbers that multiply to get “a” and those SAME two numbers must add to get “b”.
Now, I got the part where I was able to factor ONE number, and show all of it’s factors (Look at the script below.)I cleaned it up to make it look easy to read and to test it out for yourself. I got some help from a friend to help me write it.

BUT, my problem is, is that Im not able to put in the TWO numbers, in TWO different fields, and then calculate the factors for the first number. Then take the factors and figure out which one will both MULTIPLY to give me “a” and ADD to give me “b”.

[B]An example would be like this:[/B]
[I]What two numbers multiply together to give me 12, and add together to give me 7?
Well, the factors of 12 are (12,1) (6,2) and (3,4).
And if 3 and 4 add together AND multiply together they give me 12, and 7.[/I]

Also, how can I include negatives into all of this? [B]Example:[/B]
[I]What two numbers multiply to give me (-99) and add to give me (2).
Answer would be outputted as: (-9, 11).[/I]

The answers of the final numbers that Multiply and Add to give me those two numbers would lastly be outputted to a final field.

Anways if you misunderstand me, I’ll try my best to explain! Thank you very much! And I’ll try my best to understand you!

Thanks,
AJ Phinney :p

PS – Here’s the code I’m currently using.

[CODE]
<HTML>
<HEAD>
<script language=”JavaScript”>
<!–
function SymError()
{
return true;
}
window.onerror = SymError;
//–>
</script>

<script language=”javascript”>

function doit(){

a=document.factors.num.value;
checker=Math.sqrt(a);
output=””;

for (x=1; x<=checker; x++)
{
c=a/x;
d=Math.floor(c);

if(c==d){
output = output + “(” + x + “,” + c + “)” + “n”;
}
}
document.factors.worksp.value= “Factors Are:n” + output;
}
</script>

</HEAD>
<form name=”factors”>
<strong>Factor A Number </strong><br>
<input name=”num” type=”text” size=”15″><br>
<input type=”button” name=”bt1″ value=”CALCULATE” onClick=”doit()”>
<input type=”reset” value=”CLEAR”><br>
<strong>Result</strong><br>
<textarea name=”worksp” wrap=”hard”></textarea>

</form>

</center>
</BODY>
</HTML>

[/CODE]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Daniel_TMar 03.2006 — Here's a function that will get the job done.function factor(product, sum) {
for(var i = 0; i &lt; Math.sqrt(product); i++) {
var mult = product/i;
if(i+mult == sum) {
alert('factors are '+i+' and '+mult);
factor();
}
}
}

Example usage:&lt;form onsubmit="factor(this['product'].value, this['sum'].value); return false"&gt;
&lt;fieldset&gt;
&lt;legend&gt;Enter numbers&lt;/legend&gt;
&lt;p&gt;
&lt;label&gt;Product: &lt;input type="text" name="product" /&gt;&lt;/label&gt;
&lt;label&gt;Sum: &lt;input type="text" name="sum" /&gt;&lt;/label&gt;
&lt;/p&gt;
&lt;p&gt;&lt;input type="submit" /&gt;&lt;/p&gt;
&lt;/fieldset&gt;
&lt;/form&gt;

I'll work on the negative part now.
Copy linkTweet thisAlerts:
@Daniel_TMar 03.2006 — Alright, got it working with negatives!function factor(product, sum) {
var maxfactor = Math.floor(Math.sqrt(Math.sqrt(product*product)));
for(var i = maxfactor*(-1); i &lt;= maxfactor; i++) {
var mult = product/i;
if(i+mult == sum) {
return '('+i+', '+mult+')';
}
}
return 'no integer factors';
}
It uses two Math.sqrt functions because I couldn't find another way to do 4th roots.

Here it is in action: http://tnd.servebeer.com/tests/js/factor/
Copy linkTweet thisAlerts:
@paradoxperfectauthorMar 03.2006 — Hey, thanks for the great reply! Could you give me all of that put into a page? I seem to be having trouble outputting it now. Also, how could I out put that to a field, and not alert? Thanks again!

PS - Hey your Canadian, where are you from? I'm from New Brunswick!

EDIT: Never mind about you showing me the script! lol, I just saw that link at the bottom of your post! But how could I output that to a field? Thanks!
Copy linkTweet thisAlerts:
@Daniel_TMar 03.2006 — EDIT: Never mind about you showing me the script! lol, I just saw that link at the bottom of your post! But how could I output that to a field? Thanks![/QUOTE]
It IS being output to a text field in my example. It's just set to READONLY, and styled so that it doesn't look like a text field ?

Basically, to ouput to a field, you just gotta do this:field.value = factor(12, 7); // sets field value to (3, 4)
Hey your Canadian, where are you from? I'm from New Brunswick![/QUOTE]I live in a small town in southern Manitoba ?
Copy linkTweet thisAlerts:
@paradoxperfectauthorMar 04.2006 — Awesome I finally got it to work! But how do I change it so you actually have to CLICK a button to submit it to the field? Im having trouble with that!

EDIT: Actually instead of outputting to a field, couldn't I just use the "alert" function (is it a function...? or what ever) and have it pop up? I managed to play around with it and got it to pop up if I type in the numbers, but every time I type in a number it comes up "no integer factors", how can I set the "no integer factors" to come up AFTER I have typed everything in? And then say I press "Calculate"? Thanks alot! Sorry for me not wanting this at the first! its just that when I put this onto my Palm Tungsten T5, it doesnt allow the "onkeyup", because there's no key being pressed, its just inputed via the stylus. Thanks!
Copy linkTweet thisAlerts:
@Daniel_TMar 04.2006 — Here you go! Same Javascript, just modified the HTML event handlers.

http://tnd.servebeer.com/tests/js/factor/index2.htm
Copy linkTweet thisAlerts:
@paradoxperfectauthorMar 04.2006 — Awesome thank you so much!
×

Success!

Help @paradoxperfect spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...