Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help me find the problem with my solution to Project Euler #12 in Haskell

I'm a total newbie to Haskell, and programming in general, but I'm trying to work through some Project Euler problems because I do like problem solving. However, I have a problem with problem #12.

I devised a solution I thought would work, but alas, it does not.

Can you help me by opening my eyes to the problem with my code, and maybe push me in the right direction towards fixing it? Thank you.

Here is the code:

triangleNumber = scanl1 (+) [1..]

factors n = [x | x <- [1..n], n `mod` x == 0]

numFactors = length . factors

eulerTwelve = find ((>500) . numFactors) triangleNumber 

Thank you very much! :)

like image 657
Deven.W Avatar asked Jan 22 '11 22:01

Deven.W


People also ask

Where can I solve Project Euler problems?

You can now solve the classic Project Euler programming problems using the Rust language. Each of these problems comes with a user-friendly test suite. Here's the full Project Euler Rust GitHub repository. If you do not know Rust, and want to learn it, you can start with freeCodeCamp's interactive Rust course.

Why is Project Euler so difficult?

What makes a Project Euler problem? The typical problem on Project Euler usually involves number theory. This usually means searching a large number range for numbers that meet certain criteria. The size of the search space usually makes brute force solutions very time consuming or difficult, but not impossible.

Does Project Euler have answers?

Problems are of varying difficulty, but each is solvable in less than a minute of CPU time using an efficient algorithm on a modestly powered computer. As of 27 April 2021, Project Euler has more than 1,000,000 users who have solved at least one problem, in over 100 different programming languages.

What language is used in Project Euler?

I think Python is popular among Project Euler solvers because of its clean syntax. If you are a registered member of Project Euler, you should go to the Statistics page and take a look at the most used languages. You'll find Python, C/C++, Java and C# to be the most popular.


2 Answers

The Project Euler questions are designed so that it's a bad idea to try solve them by "brute force", that is, by programming the obvious search and just leaving it to run. (Some of the earlier questions can be solved like that, but it's a good idea not to exploit that, but to use them as practice.) Instead, you have to think about the mathematical content of the question so as to make the computer search tractable.

I don't want to give away too much, but here are some hints:

  • There's a formula for the nth triangular number. Find it, so you don't have to compute it by summation.

  • Given this formula for the nth triangular number, what numbers can possibly be its divisors?

  • Given what you know about these divisors, what's an easy way to figure out how many of them are there? (Without having to enumerate them.)

like image 200
Gareth Rees Avatar answered Oct 24 '22 04:10

Gareth Rees


I copied it and what it threw me was an error that find couldn't be found. That is because you need to import the module List where find is in:

import Data.List

By the way, you should optimize your factors function.

like image 23
Santiago Alessandri Avatar answered Oct 24 '22 03:10

Santiago Alessandri