Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell Programming Praxis: Nerds, Jocks, and Lockers

And another WTF Programing Praxis :

Nerds, Jocks, and Lockers

 function get-OpenLockers ($num){1..([math]::sqrt($num))|%{$_*$_}}

Enjoy,

Greetings /\/\o\/\/

Published Thursday, August 06, 2009 5:31 AM by MoW
Filed under:

Comments

# re: PowerShell Programming Praxis: Nerds, Jocks, and Lockers

This solution only works when the absolute difference between the number of total lockers (x) and sq' is less than the absolute difference between the number of lockers and sq''.

For example, if there are 12 lockers, then lockers 1, 4 & 9 are open.  In this case sq' = 9 and sq''=16.

sq' - x = |9-12| = 3

x - sq'' = |12-16 = 4

3 < 4, so the solution works

But if there are 13 lockers, then the script outputs 1,4,9 & 16.

sq' - x = |9-13| = 4

x - sq'' = |13-16 = 3

4 > 3, so the solution doesn't work.  Locker 16 can't be open because it doesn't exist.

I'm very new to powershell and programming in general, so I'll have to have a look to see how the output is being generated.

Monday, October 18, 2010 9:42 AM by
Anonymous comments are disabled