Skip to main content
Post Undeleted by danmcardle
fixed submission to follow the rules (must account for maximum difference seen so far)
Source Link
danmcardle
  • 745
  • 4
  • 8

Haskell, 95C134C

Golfed:

c n=null[x|x<-[2..n-1],n`mod`x==0]&&n>1
p=filter c[1..]
g l(m:n:o)=do;print
 |(n-m)>l=do print(m,n,n-m);g(n-m)(n:o)
 |True=g l(n:o)
main=g 0 p

Ungolfed:

-- determinesc function checks if n is prime
isPrime na =prime nullnumber
c [x|x<n=null[x|x<-[2..n-1],n`mod`x==0] && n > 1n`mod`x==0]&&n>1

-- constructsp is an infinite list of primes
primes = filter isPrimep=filter [1c[1..]

-- printsg outfunction theprints differencea betweenlist firstof twoprimes elements,and differences.
-- recurses onto listl createdis bythe skippingmaximum firstdifference elementseen so far
gaps--   (m:n:pso) =is do
the list of unprocessed printprimes
g l(n-m:n:o)
  |(n-m)>l=do print(m,n,n-m);g(n-m)(n:o)
 gaps|True=g l(n:pso)

-- runmain starts the gapsball functionrolling with thea infinitedefault listmax-seen value of primes0
main =main=g gaps0 primesp

Haskell, 95C

Golfed:

c n=null[x|x<-[2..n-1],n`mod`x==0]&&n>1
p=filter c[1..]
g(m:n:o)=do;print(n-m);g(n:o)
main=g p

Ungolfed:

-- determines if n is prime
isPrime n = null [x|x<-[2..n-1],n`mod`x==0] && n > 1

-- constructs an infinite list of primes
primes = filter isPrime [1..]

-- prints out the difference between first two elements,
-- recurses onto list created by skipping first element
gaps (m:n:ps) = do
    print (n-m)
    gaps (n:ps)

-- run the gaps function with the infinite list of primes
main = gaps primes

Haskell, 134C

Golfed:

c n=null[x|x<-[2..n-1],n`mod`x==0]&&n>1
p=filter c[1..]
g l(m:n:o)
 |(n-m)>l=do print(m,n,n-m);g(n-m)(n:o)
 |True=g l(n:o)
main=g 0 p

Ungolfed:

-- c function checks if n is a prime number
c n=null[x|x<-[2..n-1],n`mod`x==0]&&n>1

-- p is an infinite list of primes
p=filter c[1..]

-- g function prints a list of primes and differences.
--   l is the maximum difference seen so far
--   (m:n:o) is the list of unprocessed primes
g l(m:n:o)
 |(n-m)>l=do print(m,n,n-m);g(n-m)(n:o)
 |True=g l(n:o)

-- main starts the ball rolling with a default max-seen value of 0
main=g 0 p
Post Deleted by danmcardle
Source Link
danmcardle
  • 745
  • 4
  • 8

Haskell, 95C

Golfed:

c n=null[x|x<-[2..n-1],n`mod`x==0]&&n>1
p=filter c[1..]
g(m:n:o)=do;print(n-m);g(n:o)
main=g p

Ungolfed:

-- determines if n is prime
isPrime n = null [x|x<-[2..n-1],n`mod`x==0] && n > 1

-- constructs an infinite list of primes
primes = filter isPrime [1..]

-- prints out the difference between first two elements,
-- recurses onto list created by skipping first element
gaps (m:n:ps) = do
    print (n-m)
    gaps (n:ps)

-- run the gaps function with the infinite list of primes
main = gaps primes