link options link factors link strings procedure main(args) local exp, line, vars, limit, c, opts, name, output local expr1, expr2, file, var, max, primes opts := options(args, "l+") limit := \opts["l"] | 100 output := open("dietzsol.icn", "w") | stop("*** cannot open file for program") exp := "" # Input may be on more than one line. while exp ||:= pretrim(read()) # Variables are guaranteed to be lowercase letters vars := cset(exp) ** &letters # Find the largest number in the expression. max := 0 exp ? { while tab(upto(&digits)) do max <:= tab(many(&digits)) \ 1 } # Get as many primes past the largest number # as there are variables. primes := [nxtprime(max)] # big enough every 2 to *vars do put(primes, nxtprime(primes[-1])) # Perform ad-hoc replacements to convert # Mathematica syntax to a valid Icon expression. exp := replacem(exp, "\\ ", " * ", "\\^", " ^ ", "\\(", "(", "\\)", ")", "\\!", "", "+", "|| \",\" ||", # concatenation and separator ) write(output, "procedure main()") every var := !vars do write(output, " ", var, " := ", get(primes)) write(output, " terms := ", exp) write(output, " result := \"\"") write(output, " terms ? {") write(output, " while term := tab(upto(',') | 0) do {") write(output, " pattern := \"\"") write(output, " every var := !", image(vars), " do {") write(output, " while term % variable(var)_ = 0 do {") write(output, " pattern ||:= var") write(output, " term /:= variable(var)") write(output, " }") write(output, " }") write(output, " result ||:= repl(pattern, term)") write(output, " move(1) | break") write(output, " }") write(output, " }") write(output, " write(result)") write(output, "end") close(output) system("icont -s dietzsol -x") write(output, " every write(!map(result, letters,_ \"123456789\"[1 +: *letters]))") remove("diosol.icn") # clean up debris remove("diosol") end