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 8974299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:39:06+00:00 2026-06-15T18:39:06+00:00

I am toying with project euler problem 37. The problem is stated as follows:

  • 0

I am toying with project euler problem 37. The problem is stated as follows:

The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

This is my code:

import Data.Char

prime n
    | n < 2                                                                       = False
    | n == 2                                                                      = True
    | length [x | x <- [2..(floor . sqrt $ fromIntegral n)], n `mod` x == 0] == 0 = True
    | otherwise                                                                   = False

truncateList xs = take (length xs) $ iterate init xs

truncateSteps n = truncateList nn ++ truncateList rn
            where
                nn = map digitToInt $ show n
                rn = reverse nn

truncatablePrime n = and $ map (\ns -> prime $ foldl (\x y -> 10 * x + y) 0 ns) $ truncateSteps n

main = print $ sum $ take 11 $ [n | n <- [9,11..], notElem 5 $ map digitToInt $ show n, truncatablePrime n]

I believe that my code will yield the correct result if it would finish. It is simply all too slow. I have optimized a few things, like not counting numbers that contain the digit ‘5’ and only checking for ‘primeness’ up to the square root of the number, but it is not enough at all.

I would like some hints to other optimizations I could look into. Now, keep in mind that I am a new acquantance of haskell, but do suggest anything you think is worth a mention.

UPDATE
This is the finished solution which runs in just about 1 second on my machine: https://gist.github.com/4250615

Thanks for all the optimization-pointers!

  • 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-06-15T18:39:07+00:00Added an answer on June 15, 2026 at 6:39 pm

    You have two errors in your code, first

    Prelude Data.Char Main> truncatablePrime 3797
    False
    

    and second, your list comprehension conditions are not correct. (Hope that isn’t too much of a spoiler.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve the Project Euler problem 3 in Python: The prime factors
Possible Duplicate: Project Euler Problem 12 - C++ I am trying to get the
Some of you may recognize this as Project Euler's problem number 11. The one
I am trying to solve Project Euler problem in Clojure using recursion. The following
So I'm trying out Problem 7 of Project Euler. By listing the first six
I'm trying to solve Problem 13 of project euler, which involves the sum of
I'm trying to solve Project Euler Problem 14 in a lazy way. Unfortunately, I
I'm trying to complete the Project Euler problem found here. For some reason my
I have been trying to solve Project Euler's problem #59 for a while, and
I'm trying to solve problem 50 on Project Euler . Don't give me the

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.