project-euler

Haskell: faster summation of primes

Disclaimer: I'm working on Euler Problem 9. I'm adding up some pretty large numbers, all the primes from 1 to 2 000 000. Summing those primes takes forever. I'm using the haskell built in function 'sum'. as in: sum listOfPrimes Are there any other faster options? --My prime generator was the slow link in my code. ...

An interesting math puzzle

Hi StackOverflow, Although it is not very programming related but I think SO could be of some assistance: A zeroless pandigital number of base 10 is a number with all the distinct digits 1,2,3,4,5,6,7,8,9. For example, the first zeroless pandigital number of base 10 is 123456789. Find a zeroless pandigital number of base 10 such that...

Iterating a function and analysing the result in haskell

Ok, referring back to my previous question, I am still working on learning haskell and solving the current problem of finding the longest chain from the following iteration: chain n | n == 0 = error "What are you on about?" | n == 1 = [1] | rem n 2 == 0 = n : chain (n `div` 2) | otherwise = n : chain (3 * n...

How do I save the original argument in a recursive function in PHP?

Hi, I'm in PHP working on an Euler problem. I have this function so far: <?php $biggest = 0; $counter = 1; function test($i){ global $biggest; global $counter; if ($i == 1) { echo "I'm done! Took me $biggest steps"; } else { if ($i%2 == 0) { $counter = $counter + 1; if ($counter>$biggest) { ...

prime generator optimization

I'm starting out my expedition into Project Euler. And as many others I've figured I need to make a prime number generator. Problem is: PHP doesn't like big numbers. If I use the standard Sieve of Eratosthenes function, and set the limit to 2 million, it will crash. It doesn't like creating arrays of that size. Understandable. So now I'...

Add all natural numbers that are multiples of 3 and 5 : What is the bug in the following code.

I know that this can be easily done by using if(i%5 == 0 OR i%3 ==0) sum+=i; But what is wrong in the following C#code: int sum = 0; for(int i = 0, j = 0; i < 1000; i+=3, j+=5) { Console.WriteLine("i = " + i); Console.WriteLine("j = " + j); sum += i; Console.WriteLine("Sum after adding i = " + sum); if(j < 995 &&...

Project Euler N2 - Fibonacci algorithm isnt working properly.

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Find the sum of all the even-valued terms in the sequence which do not exceed four million. Int64[] Numeros = new Int64[400000...

Converting vb.net code to python for educational purposes. Output of a numeric value not occuring.

My day job is mainly coding in vb.net, so I am very familiar with it. While doing my first few dozen project euler problems, I used vb.net just to get the hang of the problem styles. Now I'd like to use project euler to help me learn a new language and have been running a couple in python. However. I've hit a snag. The following code...

Ways to improve this code

I am a Scala newbie, just starting to learn the language. I solved Problem 8 from Project Euler page. The code looks like this (I removed all the code to do with reading of an input file): def max(n1: Int, n2: Int): Int = Math.max(n1, n2) def max_product(digits: List[Int], num: Int): Int = { def max_core(lst: List[Int], curr_max...

f# stackoverflow project euler #4

I'm working on problem four of Project Euler and am running into a stackoverflow exception. I'm not asking for help on solving the problem, I'd just like it explained why I am getting a stackoverflow exception. It's usually because of infinite recursion but I don't believe that is the case this time (if I'm just blind and not seeing it...

Project Euler # 255

Project euler problem #255 is quite mathematical. I figured out how it is done for given example. Since I am a newbie in Python, I am not sure how to handle long range values. Below is the solution I have. But how does it work for 10^13 and 10^14? def ceil(a, b): return (a + b - 1) / b; def func(a, b): return (b + ceil(a, b)) / 2; d...

Making a list of divisors in Haskell

Hi I am doing problem 21 in eulerproject. One part requires finding the list of proper divisors of a number. i.e. where there is remainder of n and some number of less than n. So I made this Haskell, but GHCI gets angry at me. divisors n =[ n | n <- [1..(n-1)], n `rem` [1..(n-1)] ==0 ] The problem is that I don't know how to make: n ...

Haskell function taking a long time to process

Hi I am doing question 12 of project euler where I must find the first triangle number with 501 divisors. So I whipped up this with Haskell: divS n = [ x | x <- [1..(n)], n `rem` x == 0 ] tri n = (n* (n+1)) `div` 2 divL n = length (divS (tri n)) answer = [ x | x <- [100..] , 501 == (divL x)] The first function finds the divisors of ...

Detecting cyclic behaviour in Haskell

Hi I am doing yet another projecteuler question in Haskell, where I must find if the sum of the factorials of each digit in a number is equal to the original number. If not repeat the process until the original number is reached. The next part is to find the number of starting numbers below 1 million that have 60 non-repeating units. I ...

Problem using map with a list of lists in Haskell

Hi I am using Haskell to solve problem 99 in euler project, where I must find the maximum result from a list of base exponent pairs. I came up with this: prob99 = maximum $ map ((fst)^(snd)) numbers Where the numbers are in the form: numbers = [[519432,525806],[632382,518061],[78864,613712].. Why doesn't this work? Do I need to c...

Is there a way to optimise this program in Haskell?

Hi I am doing project euler question 224. And whipped up this list comprehension in Haskell: prob39 = length [ d | d <- [1..75000000], c <- [1..37500000], b <-[1..c], a <- [1..b], a+b+c == d, a^2 + b^2 == (c^2 -1)] I compiled it with GHC and it has been running with above average kernel priority for over an hour without returning a re...

Recommended reading for solving Project Euler problems?

Can anyone suggest any mathematical or programming books that have helped you progress in your learning whilst completing the project Euler problems? Even some suggestions for study areas or appropriate websites would be much appreciated. Background So far I've done a fair few of the Project Euler problems (I'm close to level 3). For t...

Optimisations for a series of functions in Haskell

Hi I am trying to do problem 254 in project euler and arrived at this set of functions and refactor in Haskell: f n = sum $ map fac (decToList n) sf n = sum $ decToList (f n) g i = head [ n | n <- [1..], sf n == i] sg i = sum $ decToList (g i) answer = sum [ sg i | i <- [1 .. 150] ] Where: f (n) finds the sum of the factorials ...

How to refactor this in J?

My newbie solution to Project Euler #1 +/((0=3|1+i.1000-1) +. (0=5|1+i.1000-1)) * (1+i.1000-1) I know that this can be refactored, and transformed into a function, i don't know how to do it, and I would have to read all the labs to learn it. ...

How to refactor this in J?

Here is a different approach for the Project Euler #1 solution: +/~.(3*i.>.1000%3),5*i.>.1000%5 How to refactor it? ...