project-euler

List comprehension won't give correct result in Haskell

Hi I am doing project euler question 136, and came up with the following to test the example given: module Main where import Data.List unsum x y z n = (y > 0) && (z > 0) && (((x*x) - (y*y)- (z*z)) == n) && ((x - y) == (y - z)) answer = snub $ takeWhile (<100) [n|x<-[1..],d<-[1..x`div`2],n<-[x..100],y<-[x-d],z<-[y-d], unsum x y z n ] ...

Can't quite get Project Euler problem #2 figured out.

I'm trying to learn the basics of C++ by going through some Project Euler problems. I've made it to...#2. 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-valu...

Reliable cube root in Haskell

Hi I am doing question 62 at project euler and came up with the following to test whether a number is cubic: isInt x = x == fromInteger (round x) isCube x= isInt $ x**(1/3) But due to floating point error, it returns incorrect results: *Main> isCube (384^3) False Is there a way to implement a more reliable cube test? On a side-no...

List construction in Haskell

Hi I have the following recursive function for project euler question no. 74: chain n | n `elem` xs = length xs | otherwise = (chain (sumFac n)) : xs fac n = foldl (*) 1 $ enumFromTo 1 n sumFac n = sum $ map fac $ decToList n Except I don't know the correct syntax to construct a list on chain n so that it builds up a list of xs a...

Project Euler: please help me understand #106

I have solved #103 and #105, but I have a hard time understanding #106, specifically where does the number 25 come from? If we are talking about two disjoint subsets with equal number of elements, then 1-elem vs. 1-elem: there are 4 x 3 = 12 comparisons 2 vs. 2: C(4, 2) = 6 comparisons If we include disjoint subsets with non-equal nu...

Handling numbers larger than Long in VBA

I am Currently trying to write some code in VBA to solve a problem from Project Euler. I have been trying to answer a question that requires you to find primes that can be divided into a number that will not fit in a long. Any suggestions as how to handle this problem? I know I can split the number between two variables and I have ...

Implementing a factorisation method in Haskell

Hi I am doing question 266 at project euler and after a bit of searching, found this method of quickly finding the factors of a number. What you do is find all the permutations of the prime factors of a number, these are its factors. I already have a module to find the prime power factors of a number, eg: Main> primePowerFactors 196 [(...

A List processing problem in F#

I am trying to do problem 12 in Project Euler. numDivisor64 is to calculate number of divisors. I wrote this F# code: let problem12 = {1L..300000L} |> Seq.map (fun x->x*(x+1L)/2L) |> Seq.map numDivisor64 |> Seq.filter (fun x->x>500L) The problem asks to find the number rather than its # of divisors. Besides writing this in a l...

Lagged Fibonacci Rng For Project Euler #149

Hey guys, this is very likely a total brain fart on my part but I was hoping someone could have a look at the following statement which describes how to set up the lagged fibonacci rng: First, generate four million pseudo-random numbers using a specific form of what is known as a "Lagged Fibonacci Generator": For 1 ≤ k ≤ 55, s(k...

Fibonacci Sequence Algorithm

I am attempting to find the first number in the fibbonacci sequence to contain N digits (N being somewhere in the range of 500 and 2000). I attempt to do this with the following code: BigInteger num = BigInteger.valueOf(2); BigInteger num1 = BigInteger.ONE; BigInteger num2 = BigInteger.ONE; int record = 0; BigInteger TEN = BigInteger.va...

Returning a decyphered string as part of tuple in Haskell

Hi For project euler 59, I came up with this to return a list of tuples containing the decyphered string and the key used (and yes I know about Data.Bits): module XOR where import Data.List import Data.Char decToBin :: Integer -> [Integer] decToBin x = reverse $ decToBin' x where decToBin' 0 = [] decToBin' y = let (a...

Project Euler #75: ways to optimize the algorithm

I'm looking at ways to optimize my algorithm for solving Project Euler #75, two things I have done so far are, Only check L with even values as this can be easily proved. Store L values that have been verified to have only one way to form an integer sided right angle triangle. Later on, when checking a new L value, I look for L's divis...

Project Euler 126: could someone explain?

Project Euler 126 says: "If we then add a second layer to this solid it would require forty-six cubes to cover every visible face." How come? I thought lay another 3x2x1 over a 3x2x1 makes it 3x2x2, and you need 6 to cover the top, 6 to cover the bottom, 3+2+3+2 to cover each layer, so the total is 32, every white color face is covered,...

Why do I fail Project Euler #10?

Question is: Find the sum of all the primes below 2 million. I pretty much did the Sieve of Erastothenes thing, and the program below seems to work for small number i.e. define LIMIT as 10L produces 17 as answer. I submitted 1179908154 as the answer, as produced by the following program, and it was incorrect. Please help pointing out ...

How to Code a Solution To Deal With Large Numbers?

I'm doing some Project Euler problems and most of the time, the computations involve large numbers beyond int, float, double etc. Firstly, I know that I should be looking for more efficient ways of calculation so as to avoid the large number problem. I've heard of the Bignum libraries. But, for academics interests, I'd like to know how...

Project Euler #101 - how to work around numpy polynomial overflow?

Project Euler #101 I just started learning Numpy and it so far looks pretty straightforward to me. One thing I ran into is that when I evaluate the polynomial, the result is a int32, so an overflow would occur. u = numpy.poly1d([1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1]) for i in xrange(1, 11): print(i, u(i)) The results are: (1, 1...

What's the style for immutable set and map in F#

I have just solved problem23 in Project Euler, in which I need a set to store all abundant numbers. F# has a immutable set, I can use Set.empty.Add(i) to create a new set containing number i. But I don't know how to use immutable set to do more complicated things. For example, in the following code, I need to see if a number 'x' could ...

Project Euler - Problem 272 in C#

I am have difficulties solving problem 272 of Project Euler, which has the following statement. For a positive number n, define C(n) as the number of the integers x, for which 1 < x < n and x^3 = 1 mod n. When n=91, there are 8 possible values for x, namely : 9, 16, 22, 29, 53, 74, 79, 81. Thus, C(91)=8. Find the s...

No overflow exception for int in C#?

Hi, I had this weird experience with problem number 10 on Project Euler (great site by the way). The assignment was to calculate the sum of all the prime numbers below two million. I used an int for the sum, and my algorith produced an answer, but when i pasted it to verify the answer, it was wrong. It turned out that the result was t...

Fastest way to list all primes below N in python

This is the best algorithm I could come up with after struggling with a couple of Project Euler's questions. def get_primes(n): numbers = set(range(n, 1, -1)) primes = [] while numbers: p = numbers.pop() primes.append(p) numbers.difference_update(set(range(p*2, n+1, p))) return primes >>> timeit....