<div dir="ltr"><div>Stavo guardando il problema delle 100 porte e le soluzioni nei vari linguaggi. <br><br></div>Ad esempio ecco la versione ottimizzata in Go<br><br>package main<br> <br>import "fmt"<br> <br>func main() {<br> var door int = 1<br> var incrementer = 0<br> <br> for current := 1; current <= 100; current++ {<br> fmt.Printf("Door %d ", current)<br> <br> if current == door {<br> fmt.Printf("Open\n")<br> incrementer++<br> door += 2*incrementer + 1<br> } else {<br> fmt.Printf("Closed\n")<br> }<br> }<br>}<br><div><br clear="all"><div>quella Java<br><br>public class Doors{<br> public static void main(String[] args){<br> int i; <br> for(i = 1; i < 101; i++){<br> double sqrt = Math.sqrt(i);<br> if(sqrt != (int)sqrt){<br> System.out.println("Door " + i + " is closed");<br> }else{<br> System.out.println("Door " + i + " is open");<br> }<br> }<br> } <br>}<br><br></div><div>Gia meglio quello Javascript (che sto comunque riscoprendo ed apprezzando)<br><br>for (var door = 1; door <= 100; door++) {<br> var sqrt = Math.sqrt(door);<br> if (sqrt === (sqrt | 0)) {<br> console.log("Door %d is open", door);<br> }<br>}<br><br></div><div>Anche quella in C non mi spiace<br><br>#include <stdio.h><br> <br>int main()<br>{<br> int i;<br> for (i = 1; i * i <= 100; i++)<br> printf("door %d open\n", i * i);<br> <br> return 0;<br>}<br><br></div><div>ora tocca a C++<br><br>#include <iostream> //compiled with "Dev-C++" , from RaptorOne<br> <br>int main()<br>{<br> for(int i=1; i*i<=100; i++)<br> std::cout<<"Door "<<i*i<<" is open!"<<std::endl;<br>}<br><br></div><div>Il mio primo linguaggio, il Forth<br><br>: squared ( n -- n' ) dup * ;<br>: doors ( n -- )<br> 1 begin 2dup squared >= while<br> dup squared .<br> 1+ repeat 2drop ;<br>100 doors<br><br></div><div>Il solito Perl<br><br>while( ++$i <= 100 )<br>{<br> $root = sqrt($i);<br> if ( int( $root ) == $root )<br> {<br> print "Door $i is open\n";<br> }<br> else<br> {<br> print "Door $i is closed\n";<br> }<br>}<br><br></div><div>tocca al php<br><br><?php<br>for ($i = 1; $i <= 100; $i++) {<br> $root = sqrt($i);<br> $state = ($root == ceil($root)) ? 'open' : 'closed';<br> echo "Door {$i}: {$state}\n";<br>}<br>?><br><br></div><div>Il Prolog (che come leggibilita' si batte per il trofeo al contrario, ovvero chi ci capisce nulla<br><br>doors_optimized(N) :-<br> Max is floor(sqrt(N)),<br> forall(between(1, Max, I),<br> ( J is I*I,format('Door ~w is open.~n',[J]))).<br> <br><br></div><div>L'ultimo "gioiello" di Cupertino<br><br>/* declare enum to identify the state of a door */<br>enum DoorState : String {<br> case Opened = "Opened"<br> case Closed = "Closed"<br>}<br> <br>/* declare list of doors state and initialize them */<br>var doorsStateList = [DoorState](count: 100, repeatedValue: DoorState.Closed)<br> <br>/* set i^2 doors to opened */<br>var i = 1<br>do {<br> doorsStateList[(i*i)-1] = DoorState.Opened<br> ++i<br>} while (i*i) <= doorsStateList.count<br> <br>/* print the results */<br>for (index, item) in enumerate(doorsStateList) {<br> println("Door \(index+1) is \(item.rawValue)")<br>}<br><br></div><div>Restando in tema di gemme, Ruby<br><br>doors = [false] * 100<br>100.times do |i|<br> (i ... doors.length).step(i + 1) do |j|<br> doors[j] = !doors[j]<br> end<br>end<br>puts doors.map.with_index(1){|d,i| "Door #{i} is #{d ? 'open' : 'closed'}."}<br></div><div><br></div><div>e per finire il nostro amato Python(e) <br><br>print '\n'.join(['Door %s is %s' % (i, ('closed', 'open')[(i**0.5).is_integer()]) for i in xrange(1, 101)])<br><br></div><div>Io mi astengo dal commentare (sono di parte dite?) ma mi piacerrebbe sentire commenti sui vari modi di risoluzione. Perche' preferite una data soluzione?<br></div><div><br></div><div>Carlos<br></div><div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr">EZLN ... Para Todos Todo ... Nada para nosotros<br></div></div></div></div>
</div></div></div>