module anagram ' dogagram.bas -- Find anagrams ' Written August 8, 2019 by Eric Olson ' Based on anagram-sb.bas written by John Spikowski ' ' This program demonstrates using associative arrays and dogarithms ' to find anagrams in the insane British dictionary. dim filename, wordlist() as string dim prime() as integer = { 0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 } dim dog(26) as int64 function isword(s as string) as boolean dim i,c as integer for i=1 to len(s) c=asc(mid(s,i,1)) if (casc("z")) then return false end if next i return true end function function strkey(s as string) as int64 dim key as int64 dim i as integer key=0 for i=1 to len(s) dim c as integer c=asc(mid(s,i,1))-asc("a")+1 key=key+dog(c) next i return key end function dim anagram as new system.collections.generic.dictionary(of int64,string) dim index as new system.collections.generic.list(of int64) function main() as integer dim i as integer for i=1 to 26 dog(i)=math.log(prime(i))*4503599627370496.0+0.5 next i dim word as string dim key as int64 filename="/usr/share/dict/british-english-insane" wordlist=system.io.file.readalllines(filename) for each word in wordlist if isword(word) then key=strkey(word) if anagram.containskey(key) then dim temp as string anagram.trygetvalue(key,temp) anagram.remove(key) anagram.add(key,temp+" "+word+",") else anagram.add(key,word+":") index.add(key) end if end if next word dim anaidx as int64 for each anaidx in index dim temp as string anagram.trygetvalue(anaidx,temp) if right(temp,1)="," then console.writeline(left(temp,len(temp)-1)) end if next anaidx end function end module