/* Godel anagrams in Picat. https://twitter.com/fermatslibrary/status/1275066521450975234/photo/1 """ Clever algorithm to find out whether or not 2 words are anagrams. Map each of the 26 English characters a,b,c,... to a [distinct] prime number. The multiply the characters of each word. Since every integer is a prime is a prime or a _unique_ product of primes (fundamental theorem or arithmetics), two words are anagrams if their products are the same. E.g. (a) = 2, f(e) = 5, f(r) = 7 f(are) = 2*7*5 = 70 f(ear) = 5*2*7 = 70 """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. % % Test "ear" and "are". % % ear = 1342 % ear = 1342 % % Wow, it works. :-) % go ?=> Map = godel_map(), println(ear=godel("ear",Map)), println(ear=godel("are",Map)), nl. go => true. % % Checking a word list. % % Using /usr/dict/words: % The max word is % 90310594166542993211837839521595 = [immunoelectrophoresis] % % % Using /usr/share/dict/words: % Max word: % 1523127317154863235957273808099490 = [counterrevolutionaries] % % Max with > 1 words % 8056607950592924764679 = [impressiveness,permissiveness] % % The numbers with most words: % maxWords = [5049946 = [pares,parse,pears,rapes,reaps,spare,spear], % 31919470 = [carets,caster,caters,crates,reacts,recast,traces]] % go2 ?=> % WordList = "unixdict.txt", % a small version of /usr/dict/words WordList = "/usr/share/dict/words", Words = read_file_lines(WordList), Godel = godel_map(), Map = new_map(), foreach(W in Words) OK = true, % check for bad words, i.e. not a..z foreach(C in W, not ascii_lowercase(C)) OK := false end, if OK then N = godel(W,Godel), Map.put(N,Map.get(N,[])++[W]) end end, MaxNumWords = 0, MaxWords = [], foreach(K in Map.keys.sort) L = Map.get(K), Len = L.len, if Len > 1 then println(K=Map.get(K)) end, if Len >= MaxNumWords then if Len == MaxNumWords then MaxWords := MaxWords ++ [K=L] else MaxWords := [K=L], MaxNumWords := Len end end end, println(maxWords=MaxWords), nl. go2 => true. % Create the Godel map of a..z and the primes godel_map() = new_map([A=P : {A,P} in zip("abcdefghijklmnopqrtsuvwxyz",primes(101))]). % Godelize a word godel(Word,Map) = prod([Map.get(C) : C in Word]).