We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 23505e8 commit b8dc474Copy full SHA for b8dc474
archive/t/tcl/prime-number.tcl
@@ -0,0 +1,33 @@
1
+package require Tcl 8.6
2
+package require math::numtheory
3
+
4
+proc usage {} {
5
+ puts stderr {Usage: please input a non-negative integer}
6
+ exit 1
7
+}
8
9
+proc isNonNegativeInteger {s} {
10
+ if {[catch {expr {int($s)}} n]} {
11
+ return 0
12
+ }
13
+ if {$s eq ""} { return 0 }
14
+ if {![string is integer -strict $s]} {
15
16
17
+ return [expr {$n >= 0}]
18
19
20
+if {$argc != 1} { usage }
21
22
+set input [string trim [lindex $argv 0]]
23
+if {![isNonNegativeInteger $input]} { usage }
24
25
+set n $input
26
+if {$n < 2} {
27
+ puts "composite"
28
+} elseif {[math::numtheory::isprime $n]} {
29
+ puts "prime"
30
+} else {
31
32
33
0 commit comments