Modulinos: Difference between revisions

1,451 bytes added ,  4 years ago
(→‎{{header|REXX}}: updated the program's boxed comment to reflect that any program utilization the "command interface" can also seem like the program was invoked via the command line (interface).)
Line 715:
 
console.log("Test: The meaning of life is " + sm.meaningOfLife());</lang>
 
=={{header|Julia}}==
Julia does not use scripted main by default, but can be set to run as such. Modules generally use a /test unit test subdirectory instead.
<br />
In module file Divisors.jl:
<lang julia>module Divisors
 
using Primes
 
export properdivisors
 
function properdivisors(n::T) where T <: Integer
0 < n || throw(ArgumentError("number to be factored must be ≥ 0, got $n"))
1 < n || return T[]
!isprime(n) || return T[one(T), n]
f = factor(n)
d = T[one(T)]
for (k, v) in f
c = T[k^i for i in 0:v]
d = d*c'
d = reshape(d, length(d))
end
sort!(d)
return d[1:end-1]
end
 
function interactiveDivisors()
println("\nFind proper divisors between two numbers.\nFirst number: ")
lo = (x = tryparse(Int64, readline())) == nothing ? 0 : x
println("\nSecond number: ")
hi = (x = tryparse(Int64, readline())) == nothing ? 10 : x
lo, hi = lo > hi ? (hi, lo) : (lo, hi)
 
println("Listing the proper divisors for $lo through $hi.")
for i in lo:hi
println(lpad(i, 7), " => ", rpad(properdivisors(i), 10))
end
end
 
end
 
# some testing code
if occursin(r"divisors.jl"i, Base.PROGRAM_FILE)
println("This module is running as main.\n")
Divisors.interactiveDivisors()
end
</lang>
In a user file getdivisors.jl:
<lang julia>include("divisors.jl")
 
using .Divisors
 
n = 708245926330
println("The proper divisors of $n are ", properdivisors(n))
</lang>
 
=={{header|LLVM}}==
4,102

edits