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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:26:36+00:00 2026-05-30T10:26:36+00:00

During last three days I have been trying to solve Project Euler 15 in

  • 0

During last three days I have been trying to solve Project Euler 15 in Haskell.

Here is my current state:

import Data.Map as Map

data Coord = Coord Int Int deriving (Show, Ord, Eq)

corner :: Coord -> Bool 
corner (Coord x y) = (x == 0) && (y == 0)

side :: Coord -> Bool
side (Coord x y) = (x == 0) || (y == 0)

move_right :: Coord -> Coord
move_right (Coord x y) = Coord (x - 1) y

move_down :: Coord -> Coord
move_down (Coord x y) = Coord x (y - 1)

calculation :: Coord -> Integer
calculation coord 
           | corner coord = 0
           | side coord = 1 
           | otherwise = (calculation (move_right coord)) + (calculation (move_down coord)) 

problem_15 :: Int -> Integer
problem_15 size =
           calculation (Coord size size)

It works fine but it is very slow if the ‘n’ is getting bigger.

As I know I can use the dynamic programming and the hashtable (Data.Map, for example) to cache calculated values.

I was trying to use memoization, but don’t have a success. I was trying to use Data.Map, but each next error was more scary then previous. So I ask your help: how to cache values which was already calculated ?

I know about mathematical solution of this problem (Pascal triangle), but I am interested in the algorithmic solution.

  • 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-30T10:26:37+00:00Added an answer on May 30, 2026 at 10:26 am

    Instead of a Map, this problem is better suited for an two-dimensional array cache, since we have a bounded range for input values.

    import Control.Applicative
    import Data.Array
    
    data Coord = Coord Int Int deriving (Show, Ord, Eq, Ix)
    
    calculation :: Coord -> Integer
    calculation coord@(Coord maxX maxY) = cache ! coord where
        cache = listArray bounds $ map calculate coords
        calculate coord
            | corner coord = 0
            | side coord   = 1
            | otherwise    = cache ! move_right coord + cache ! move_down coord
    
        zero  = Coord 0 0
        bounds = (zero, coord)
        coords = Coord <$> [0..maxX] <*> [0..maxY]
    

    We add deriving Ix to Coord so we can use it directly as an array index and in calculation, we initialize a two-dimensional array cache with the lower bound of Coord 0 0 and upper bound of coord. Then instead of recursively calling calculation we just refer to the values in the cache.

    Now we can calculate even large values relatively quickly.


    *Main> problem_15 1000
    2048151626989489714335162502980825044396424887981397033820382637671748186202083755828932994182610206201464766319998023692415481798004524792018047549769261578563012896634320647148511523952516512277685886115395462561479073786684641544445336176137700738556738145896300713065104559595144798887462063687185145518285511731662762536637730846829322553890497438594814317550307837964443708100851637248274627914170166198837648408435414308177859470377465651884755146807496946749238030331018187232980096685674585602525499101181135253534658887941966653674904511306110096311906270342502293155911108976733963991149120

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

Sidebar

Related Questions

I have faced this problem quite often during the last couple of months, during
I have looked a bit into cryptography and related matters during the last couple
I am trying to work out a bug we've found during our last iteration
I've been trying to find a good architecture for one application for the last
I've been scratching my head over this for the last few days and neither
I have interesting thing here, I'm using xcode 4 for my project and I
I have a price of js code, which has been troubling me since last
I've been playing with redis (and add some fun with it) during the last
During the last few days I've gained some information about memory allocators other than
I have been trying different techniques while designing this application, which to me 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.