I have cows, horses and dogs, a different prime number of each. If I multiply the number of cows (c) by the total of cows and horses (c+h), the product is 120 more than the number of dogs (d), that is: c*(c+h) = 120 + d.
How many of each do I have?
As c^2 + ch  (120 + d) = 0, the quadratic formula can find c for a given h and d.
c = (h + sqrt(h^2 + 4(120 + d)))/2
Then c can be tested for primality.
The UBASIC program evaluates this allowing h and d to be up to the 10,000th prime number:
10 for Hct=1 to 10000
20 for Dct=1 to 10000
30 H=prm(Hct)
40 D=prm(Dct)
50 Disc=H*H+4*(D+120)
60 Sr=int(sqrt(Disc)+0.5)
70 if Sr*Sr=Disc and (Srh)@2=0 then
80 :C=int((SrH)/2)
90 :if prmdiv(C)=C then
100 :print C,H,D
110 next
120 next
finds two solutions, one of which is spurious as it has the same number of dogs as cows, contrary to the problem statement. The spurious one is 2 cows, 59 horses and 2 dogs.
The intended solution is 11 cows, 2 horses and 23 dogs.
Note: In UBASIC, prm(x) returns the xth prime number, and prmdiv(x) returns the smallest prime divisor of x. The prmdiv function is applicable only to numbers in integer formnot to real numbers even if they happen to have an integral value, hence line 80, converting a real that happens to be an integer to integer format.

Posted by Charlie
on 20081020 12:22:28 