On Dec 5, 2011, at 11:38 AM, Robert Klemme wrote:

> On Mon, Dec 5, 2011 at 10:03 AM, James Gallagher
> <lollyproductions / mac.com> wrote:
>> Thank you everyone for Helping me out.  :-)
>=20
> If I am not mistaken, everybody in this thread resorted to using a
> string conversion.  Why does nobody want to calculate this
> numerically?

I posted a similar version to yours which uses an iterator.

n =3D 2011
n.size.pred.downto(0).reduce(0) { |sum, a| sum +=3D (n.abs / 10 ** a) % =
10 }

The while loop is more straightforward though. Perhaps we could combine =
the two?

>=20
>>> x =3D 2011
> =3D> 2011
>>> q =3D 0
> =3D> 0
>>> a =3D x
> =3D> 2011
>>> while a>0;a,b=3Da.divmod 10;q+=3Db;end
> =3D> nil
>>> q
> =3D> 4
>=20
> Granted, this is not too short - but it works without creating a
> String instance.
>=20
> Kind regards
>=20
> robert
>=20
> --=20
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/
>=20