/* Prime code in Picat. Via a retweet by Card Colm Mulcahy Via From Martin Garner tweet https://twitter.com/WWMGT/status/1192429565055913985 From Chris Smith https://twitter.com/aap03102/status/1189528339914149888 https://twitter.com/aap03102/status/1189528339914149888/photo/1 """ Take the first 26 primes and relate each to letter of the alphabet (A=2, B=3, C=5, ... Y=97, Z=101) Use the code that a word's numeric is given by the product of the letters. So the 'Prime Code' for the word CAB would be the product 5*2*3 = 30 a) Decode this message... 913 1511191 7411406 b) What (legitimate) English word comes closest to a million? """ Solution: go/0: a) Decode this message 913 1511191 7411406 913 = ew we 1511191 = levo love velo vole 7411406 = maths b) What (legitimate) English word comes closest to a million? [p = 999925,word = [ciclo,colic],diff = 75] go2/0: Nearest words to 10**I: target = 10 [p = 10,word = [ac,ca],diff = 0] target = 100 [p = 100,word = [acca,caca],diff = 0] target = 1000 [p = 1001,word = [def,dfe,edf,efd,fed],diff = 1] target = 10000 [p = 9996,word = [bagdad],diff = 4] target = 100000 [p = 100040,word = [acamar,camara,maraca],diff = 40] target = 1000000 [p = 999925,word = [ciclo,colic],diff = 75] target = 10000000 [p = 10001365,word = [coupe,pouce],diff = 1365] target = 100000000 [p = 100003156,word = [laharpe,phalera,raphael],diff = 3156] target = 1000000000 [p = 999907129,word = [filths],diff = 92871] target = 10000000000 [p = 10001123902,word = [glissade],diff = 1123902] This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. go => garbage_collect(400_000_000), WordList = "words_lower.txt", ProductWords = make_product_words(WordList), println("a) Decode this message 913 1511191 7411406"), foreach(P in [913,1511191,7411406]) println(P=ProductWords.get(P).join(' ')) end, println("\nb) What (legitimate) English word comes closest to a million?"), minof(target_word(1_000_000,ProductWords,Word,P,Diff),Diff), println([p=P,word=Word,diff=Diff]), nl. go2 => garbage_collect(400_000_000), WordList = "words_lower.txt", ProductWords = make_product_words(WordList), println("\nNearest words to 10**I:"), foreach(I in 1..10) Target = 10**I, println(target=Target), time(minof(target_word(Target,ProductWords,Word2,P2,Diff2),Diff2)), println([p=P2,word=Word2,diff=Diff2]), nl end, nl. % Note: We assume that the words are in lowercase! make_product_words(WordList) = ProductWords => S = 97, AlphaPrime = new_map([C=P : {C,P} in zip([chr(C) : C in S..S+26-1],primes(101))]), ProductWords = new_map(), foreach(Word in read_file_lines(WordList)) word_product(Word,AlphaPrime) = Product, ProductWords.put(Product, ProductWords.get(Product,[]) ++ [Word]) end. target_word(Target,ProductWords,Word,P,Diff) ?=> T = Target div 1000, TT = [S : S in Target-T..Target+T], member(P,TT), ProductWords.has_key(P), Word = ProductWords.get(P), Diff=abs(Target-P). word_product(Word,AlphaPrime) = prod([AlphaPrime.get(C) : C in Word]).