If the problem were more computationally intensive, [http://en.wikipedia.org/wiki/Modular_exponentiation modular exponentiation] might be appropriate. With this problem size the naive approach is sufficient.

If the problem were more computationally intensive, [http://en.wikipedia.org/wiki/Modular_exponentiation modular exponentiation] might be appropriate. With this problem size the naive approach is sufficient.

Find arithmetic sequences, made of prime terms, whose four digits are permutations of each other.

Solution:

I'm new to haskell, improve here :-)

I tidied up your solution a bit, mostly by using and composing library functions where possible...makes it faster on my system. Jim Burton 10:02, 9 July 2007 (UTC)

importData.Listisprime::(Integrala)=>a->Boolisprimen=isprime22whereisprime2x|x<n=ifn`mod`x==0thenFalseelseisprime2(x+1)|otherwise=True-- 'each' works like this: each (4,1234) => [1,2,3,4]each::(Int,Int)->[Int]each=unfoldr(\(o,y)->letx=10^(o-1)(d,m)=y`divMod`xinifo==0thenNothingelseJust(d,(o-1,m)))ispermut::Int->Int->Boolispermut=letf=(sort.each.(,)4)in(.f).(==).fisin::(Eqa)=>a->[[a]]->Boolisin=any.elemproblem_49_1::[Int]->[[Int]]->[[Int]]problem_49_1[]res=resproblem_49_1(pr:prims)res=problem_49_1primsres'whereres'=ifpr`isin`resthenreselseres++[pr:(filter(ispermutpr)(pr:prims))]p49a::[[Int]]p49a=problem_49_1[n|n<-[1000..9999],isprimen][]unAdd[]=[]unAdd(x:xs)=[x-y|y<-xs]++(unAddxs)takeEqv[]=[]takeEqv(x:xs)=[x|y<-xs,x-y==0]++(takeEqvxs)div2un[]=[]div2un(x:xs)=[div(x-y)2|y<-xs]++(div2unxs)eqvListxy=[a|a<-x,b<-y,a==b]problem_49=[y|x<-p49a,lety=sort$nubx,length(y)>=4,letz=unAddy,length(z)/=length(nubz),(eqvList(div2uny)(takeEqvz))/=[]]