Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Find numbers given sum and product

What I have tried:

VB
Sub test()
 Dim sum, prod, outcounter, incounter
     prod = 1
For outcounter = 1 To Sheet1.Cells.Range("A1").Value
    For incounter = 1 To Sheet1.Cells.Range("A1").Value
        sum = outcounter + incounter
        prod = outcounter * incounter
        
        
        If sum = Sheet1.Cells.Range("A1").Value Then
            If prod = Sheet1.Cells.Range("A2").Value Then
                Sheet1.Cells.Range("B1").Value = outcounter
                Sheet1.Cells.Range("B2").Value = incounter
            GoTo found
            End If
        End If
        
    Next
Next
found:
End Sub
Posted
Updated 20-May-16 16:36pm
v2
Comments
CHill60 20-May-16 5:08am    
What is your actual question?
Patrice T 20-May-16 6:07am    
Looks like HomeWork.
OP just want a full blowup solution, since stated no problem.

1 solution

Here is a fully optimized version of your macro. :-O
VB
Sub test()
End Sub

If only you had used your brain before programming, you should gave discovered that the problem is just about solving a quadratic equation.

I let you do the work of finding how to get the equation and the formula for solution.

Message to downvoter that don't give a solution:
Do the mathematical analyze and you will see that there is no need of a macro.
the solution is just 4 formulas (2 solutions and 2 variables) that fit in cells formulas.
And since this is Homework, just giving the full solution would earn me even more downvotes.
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900