A prime number is an integer that has exactly two factors: one, and itself. The
ID: 3747673 • Letter: A
Question
A prime number is an integer that has exactly two factors: one, and itself. The number 1, however, is not considered to be prime. So the first few prime numbers are 2, 3, 5, 7, 11, 13.
Define a helper function using the symbolic operator notation slash-question-mark: (/?). This can be used as an infix operator to determine whether a number is divisible by another number (with no remainder). It returns a Boolean. For example:
ghci> 3 /? 2
False
ghci> 18 /? 3
True
ghci> 18 /? 4
False
Next, you can define isPrime n. It can check whether any number in the range [2..n-1] divides n. (It also must ensure that n > 1.)
Finally, a twin prime is a pair of neighboring odd numbers that are both primes. For example 11 and 13 are both prime so they are referred to as twins. Define a function isFirstTwinPrime n that determines whether n is the first of a pair of twin primes.
Code must be in HASKELL to answer the following.
8/?3
8/?4
count primes up to 1000
first ten twin primes
IsPrime 1, IsPrime 19, IsPrime 21, IsPrime 2
Explanation / Answer
(/?) :: Integer -> Integer -> Bool
(/?) a b = a `rem` b == 0
isPrime :: Integer -> Bool
isPrime k= k>1 && null[ x | x <- [2..k - 1], k /? x == True]
countPrimeUpto :: Integer -> [Integer]
countPrimeUpto n = [x | x <-[2..n], isPrime x == True]
areTwinPrime a b = isPrime a && isPrime b && b-a==2
getNTwins n = take n [(x,y) | x<-[2..1000],y<-[3..1000], areTwinPrime x y==True]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.