Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4033102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:48:19+00:00 2026-05-20T11:48:19+00:00

Just starting with Haskell, and I put together this ugly piece to determine the

  • 0

Just starting with Haskell, and I put together this ugly piece to determine the numbers in a list divisible by a number and all numbers less than it.

divis :: (Integral a) => a -> [a] -> [a]
divis _ [] = []
divis n (x:xs)
    | x `mod` n == 0 && n == 2 = x : divis n xs
    | x `mod` n == 0 = divis (n-1) [x] ++ divis n xs
    | otherwise = divis n xs 

and I can call it like…

head (divis 10 [1..])

to get the first number in the list, in this case 2520. However, it seems that this is not good enough to efficently solve using a higher number like 20.

How can I fix this raskell of a haskell?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:48:19+00:00Added an answer on May 20, 2026 at 11:48 am

    This can be improved substantially by using a different algorithm: The smallest number which can be divided by a set of numbers (in this case, the set is [1..10]) is the least common multiple of those numbers.

    Haskell even has a least common multiple function (lcm) built-in which you can use:

    Prelude> foldl lcm 1 [1..10]
    2520
    

    If you prefer not to use the build-in lcm function (as that’s almost cheating 🙂 ), you can do it by using Euclid’s algorithm to calculate the GCD, and then using:

    lcm a b = a * b `div` gcd a b
    

    If you need to find all the numbers in a given list which are divisible by [1..n], you can use the fact that any such number will also be divisible by the least common multiple of [1..n]:

    divis n xs = filter (\x -> x `mod` mult == 0) xs
        where mult = foldl lcm 1 [1..n]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just starting out at teaching myself Haskell. This code is supposed to do
Just starting out on C# and I keep getting stuck on this particular piece
I'm just starting to look into the world of Functional Reactive Programming in Haskell,
Just starting out, this should be a simple one but I haven't been able
Just starting with php and sorry if this is a newbie question but i'm
Just starting out with C#, so this may be overly simple that I keep
I'm just starting to learn haskell. I have custom class class MyClass a where
Just starting to use Cruise Control, but can't seem to find anything on this
'm a student, just starting working with Haskell and have problems with part of
I'm just starting to take a look at Haskell (my previous FP experience is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.