gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
countPrimes in Haskell with user input
- User
countPrimes in Haskell with user input
- Assistant
Here's an example of how you can implement the
countPrimesfunction in Haskell that takes user input:isPrime :: Int -> Bool isPrime n | n <= 1 = False | otherwise = all (\x -> n `mod` x /= 0) [2..(floor . sqrt . fromIntegral) n] countPrimes :: Int -> Int countPrimes n = length $ filter isPrime [1..n] main :: IO () main = do putStrLn "Enter a number:" input <- getLine let num = read input :: Int result = countPrimes num putStrLn $ "Number of primes up to " ++ show num ++ " is: " ++ show resultIn this example, the
isPrimefunction checks if a number is prime using trial division. ThecountPrimesfunction usesfilterto generate a list of prime numbers up to the input number, and then calculates its length.The
mainfunction prompts the user to enter a number, reads the input, and callscountPrimeswith the user-provided number. Finally, it prints the result to the console.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.