The Ultimate Computer (Star Trek)

On stardate 4729.4, the Federation starship USS Enterprise NCC-1701, commanded by Captain James T. Kirk, is summoned to a space station without explanation. Commodore Robert Wesley, commanding a group of starships from the USS Lexington, explains that the Enterprise will be a test vessel for a revolutionary tactical and control computer called the "M-5 Multitronic System", designed by the brilliant Dr. Richard Daystrom (who had also invented the TOS era computer systems after developing duotronics when he was 24 years old). The M-5 will handle all ship functions without human assistance. While Captain Kirk and Chief Medical Officer Dr. McCoy are unhappy about the test, Science Officer Spock is impressed with M-5. However, Kirk learns that four earlier prototypes were unsuccessful, giving him further doubts.
At first M-5 works well, performing ship functions more quickly and efficiently than a living crew. Later, M-5 exhibits quirks such as turning off power and life support to unoccupied parts of the ship. It draws increased power for unknown reasons. But Daystrom maintains M-5 is working properly.
In a war games drill, M-5 defends the Enterprise against mock attacks from starships Excalibur and Lexington. The Enterprise is declared the victor, prompting Commodore Wesley to call Kirk "Captain Dunsail" (pronounced "dunsel"). Spock explains the term is used by midshipmen at Starfleet Academy to describe a part serving no useful purpose. Kirk is visibly shaken by this.
Soon after, M-5 detects the Woden, an unmanned freighter that is not part of the test, and attacks with real weapons, destroying it. Kirk orders M-5 taken offline, but Daystrom continues to believe it is working correctly, and refuses. Kirk tries to disconnect M-5, but discovers it is protecting itself with a force field. Chief Engineer Scott assigns Ensign Harper, a technician, to unplug the main connection, but the crewman is killed in the process. Spock and Scott desperately attempt a manual override, but they discover M-5 has bypassed its power source and now draws energy directly from the ship's warp engines. Daystrom persistently defends M-5 and refuses to disconnect it.
Spock questions Daystrom on his computer design. Daystrom reveals he has programmed human engrams into M-5. Pressed further, Daystrom admits the engrams he used were his own, meaning M-5 thinks similarly to Daystrom himself. With increased stress and anger, Daystrom appears unstable. M-5 now shows similar instability. An attempt by the Enterprise crew to isolate M-5 from the ship fails, as they are duped by a decoy.
Meanwhile the other ships in the test continue unaware of the problems with the Enterprise. Next follows a war game against Federation starships Lexington, Potemkin, Excalibur, and Hood. M-5 detects the ships, but does not treat them as part of the trial, instead firing on them with full-strength weapons. Daystrom states the M-5 is programmed to preserve itself by any means. Although surprised by M-5's actions, Daystrom simply views them as mistakes made by a learning "child." An angry Kirk asserts these "mistakes" are costing lives, and the computer must be shut down.
The crew watches as M-5 pounds the other ships relentlessly. The Enterprise fires on the Lexington, killing 53, then completely cripples the Excalibur - killing all aboard her and leaving her to drift in space. From the Lexington, Commodore Wesley contacts Starfleet Command for permission to destroy the Enterprise. Since M-5 has disabled communications, Kirk is unable to contact Commodore Wesley and explain what is happening. Kirk demands that Daystrom act, but the scientist will not accept M-5 as another failure. He rambles about proving his worth and curses colleagues taking credit for his work. McCoy sees a psychotic episode coming, and warns Kirk the scientist is becoming delusional.
Kirk has Daystrom taken to sickbay after Spock fells him with a Vulcan nerve pinch. Kirk then talks to M-5 to see whether he can persuade it to stop the attacks. The M-5 acknowledges Kirk, who asks M-5 what its purpose is. M-5 responds "To save men from the dangerous activities of space exploration". Kirk rejoins that it just acted contrary to its purpose by killing people. M-5 recognizes the penalty for murder is death, so it shuts itself down. In so doing, it cripples the Enterprise, setting the ship adrift.
Having permission from Starfleet, the other Federation ships now close on the Enterprise to destroy it. While Scotty frantically attempts to regain control of the ship, Kirk decides to let the ship drift with shields down, hoping that Commodore Wesley will realize what the situation is aboard the Enterprise. The gamble pays off as the Commodore orders his ships to stand down at the last moment.
McCoy says that Daystrom will be committed to a rehabilitation center. Kirk explains that he knew that Bob Wesley would not fire, because he gambled on his humanity. McCoy pointedly comments that compassion is something computers lack. Spock responds that machines are more efficient than human beings: not better. He then dryly remarks that if McCoy's engrams were impressed in a computer, the resulting torrential flood of illogic would be most entertaining.