Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Egg 'n bacon

HS Thomas
Ranch Hand
Posts: 3404
Jessica raises pot-bellied pigs and ostriches.
Among her animals she has 17 heads and 56 legs.
How many pigs and ostriches does she have ?
regards

Jason Menard
Sheriff
Posts: 6450
6 ostriches and 11 pigs.
The Math:
x = # of ostrich legs, x/2 = # of ostriches
y = # of pig legs, y/4 = # of pigs
x + y = 56
x/2 + y/4 = 17
y = 56 - x
x/2 + (56 - x)/4 = 17
4(x/2 + (56 - x)/4) = 4 * 17
2x + 56 - x = 68
x = 12
y = 56 - 12 = 44
x/2 = 6
y/4 = 11
[ September 10, 2003: Message edited by: Jason Menard ]

HS Thomas
Ranch Hand
Posts: 3404
Congrats.
I did it the usual (longer) way using math.
Here is a solution that uses pure logic.
Each animal has two hind legs, so 17 heads means 34 hind legs total.
The remaining 22 legs must be the front legs of 11 four-legged pigs.
Therefore 11 heads belong to the pigs and the remaining 6 heads belong to ostriches.
regards

Bert Bates
author
Sheriff
Posts: 8945
17
How do you count cows in a pasture?
.
.
.
.
.
.
.
.
.
Count their legs and divide by four.

 It is sorta covered in the JavaRanch Style Guide.