# Ruby to solve a physics question

**URL:** <https://www.ruby-forum.com/t/ruby-to-solve-a-physics-question/211802>\
**Category:** wxRuby\
**Created:** [September 14, 2011, 2:34am UTC](https://www.ruby-forum.com/t/ruby-to-solve-a-physics-question/211802 "2011-09-14T02:34:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [September 14, 2011, 2:34am UTC](https://www.ruby-forum.com/t/ruby-to-solve-a-physics-question/211802/1 "2011-09-14T02:34:58Z")

</div>

I am trying to solve one of my graduate level physics problems with  
ruby…  
Here is what I have so far…

a6=0.0  
for n1 in -10…10  
for n2 in -10…10  
for n3 in -10…10  
if n1!=0 and n2!=0 and n3!=0  
p=Math.sqrt(n1 **2+n2** 2+n3\*\*2+n1_n2/1.414+n1_n3/1.41+n2\*n3/1.414)  
a6+=(1/p)\*\*6  
end  
end  
end  
end  
puts a6

What I’ve got here is a 10x10x10 face-centered cubic lattice of atoms.  
What I want to do is find this constant a6= sum(1/Pij)\*\*6. From a  
starting point I want the distance to every other atom location point in  
the structure, meanwhile inversing and power to the sixth each distance.  
Then I want to add all those up. This should return 14.4???

---

<div class="post-metadata">

**Author:** ![tc1185](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@tc1185](https://www.ruby-forum.com/u/tc1185)\
**Post date:** [September 14, 2011, 7:32pm UTC](https://www.ruby-forum.com/t/ruby-to-solve-a-physics-question/211802/2 "2011-09-14T19:32:31Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Am 14.09.2011 02:34, schrieb thiess cunningham:

> I am trying to solve one of my graduate level physics problems  
> with ruby…

Hi thiess,

this is the wxRuby mailinglist, related to the wxRuby GUI toolkit.  
Your question should better be served on ruby-talk. Go to  
[Ruby - Ruby-Forum](http://www.ruby-forum.com/forum/ruby) for posting there.

> location point in the structure, meanwhile inversing and power to  
> the sixth each distance. Then I want to add all those up. This  
> should return 14.4???

I’m by no means someone who has any knowledge on how atom physics  
work, but a few notes on your code:

- Don’t use “for” in Ruby. There’s the #each method that is the  
correct idiom for this. And four your specific case you should use:  
-10.upto(10) do |nx| … end

- The test against zero can be made much more readable with the  
Methods Integer#zero? and Integer#nonzero?

- I’m not willing to dig into the giant term you pass to Math.sqrt.

- From a first glance, you might want to have a look at the  
Enumerable#inject method, which you can use to easily sum up things  
that are computed the same way.

- Be aware that doing math with floating-point numbers is always  
dangerous as they’re not exact. The further you continue to compute  
with floating-point results you got, the more unprecise your final  
result will be. If I remember correctly, this is the industry standard  
to blame for this: [IEEE 754-2008 revision - Wikipedia](http://en.wikipedia.org/wiki/IEEE_754-2008)

As I noted before, post your question to ruby-talk. It’s not related  
to wxRuby in any way.

Vale,  
Marvin  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.11 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org/](http://enigmail.mozdev.org/)

iQEcBAEBAgAGBQJOcORYAAoJELh1XLHFkqha3YgH/jv4i6v+5tLR/xdhN/LCMBeD  
iHyDj6/Qsrq7r7Xg8J1GeiuXpdZzk3GlkjlJTCJ1+4mgP+zsHW+G4XwlMwvQCX69  
88tfeeOAV1KxyOcAbU6CQkD/1O6HTV5/CP0EwJOPN+kZEDnQSVXoax0chykstNLS  
EBvwbkjLaopfz5Hc5kXpsWFIeuMRO4OhW9eftmWm15zTjoscUOUcrAB+r8QUCeMq  
jzqod7vbxrk9lw6PE5e2TqQmPuD+haAOdsM3R9/FqKA5I4tLxXaxymlUme/aOAL2  
kMB8t8huHOWEW0OdzhdCCAHT7UY13IGgiNXDpXF1WOhFo6re00if13oxaQ9kNsU=  
=mrX6  
-----END PGP SIGNATURE-----
