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

  • Home
  • SEARCH
  • 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 772797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:51:37+00:00 2026-05-14T18:51:37+00:00

I’m currently working on a project Euler problem (www.projecteuler.net) for fun but have hit

  • 0

I’m currently working on a project Euler problem (www.projecteuler.net) for fun but have hit a stumbling block. One of the problem provides a 20×20 grid of numbers and asks for the greatest product of 4 numbers on a straight line. This line can be either horizontal, vertical, or diagonal.

Using a procedural language I’d have no problem solving this, but part of my motivation for doing these problems in the first place is to gain more experience and learn more Haskell.
As of right now I’m reading in the grid and converting it to a list of list of ints, eg — [[Int]]. This makes the horizontal multiplication trivial, and by transposing this grid the vertical also becomes trivial.

The diagonal is what is giving me trouble. I’ve thought of a few ways where I could use explicit array slicing or indexing, to get a solution, but it seems overly complicated and hackey. I believe there is probably an elegant, functional solution here, and I’d love to hear what others can come up with.

  • 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-14T18:51:37+00:00Added an answer on May 14, 2026 at 6:51 pm

    I disagree with the estimable Don Stewart. Given the combinatorial nature of the problem and the fact that the problem size is only 20×20, lists of lists are going to be plenty fast enough. And the last thing you want is to futz around with array indexing. Instead I suggest that you extend the techniques developed by Richard Bird in his justly famous sudoku solver. To be more specific, I’d suggest the following:

    • Write a function that given a sequence, returns all contiguous subsequences of length 4.

    • Write a function that given a grid, returns all rows.

    • Write a function that given a grid, returns all columns.

    • Write a function that given a grid, returns all diagonals.

    With these functions in hand, your solution will be easy. But as you mention the diagonal is not so obvious. What is a diagonal anyway?
    Let’s look at an example:

    X . . . . .
    . X . . . .
    . . X . . . 
    . . . X . .
    . . . . X .
    . . . . . X
    

    Suppose for a moment that you use the drop function and you drop 0 elements from row 0, 1 element from row 1, and so on. Here’s what you wind up with:

    X . . . . .
    X . . . .
    X . . . 
    X . .
    X .
    X
    

    The elements of the diagonal now form the first column of the triangular thing you have left. Even better, every column of the thing you have left is a diagonal of the original matrix. Throw in a few symmetry transformations and you’ll easily be able to enumerate all the diagonals of a square matrix of any size. Whack each one with your “contiguous subsequences of length 4” function, and Bob’s your uncle!


    A little more detail for those who may be stuck:

    The key to this problem is composition. Diagonals come in four groups. My example gives one group. To get the other three, apply the same function to the mirror image, the transpose, and the mirror image of the transpose.

    • Transpose is a one-line function, and you need it anyway to recover columns cleanly.

    • Mirror image is even simpler than transpose—think about what functions you can use from the Prelude.

    The symmetry method will give each major diagonal twice; luckily for the problem stated it’s OK to repeat a diagonal.

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

Sidebar

Related Questions

No related questions found

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.