project-euler

Project Euler Problem 2 in F#

I'm a bit stuck on the last step of getting the solution to problem 2 on Project Euler. This is the source I've gotten so far. #light module pe2 (* Project Euler Problem 2 solution *) open System let Phi = 1.6180339887;; let invPhi = 1.0/Phi;; let rootOfFive = 2.236067977;; let maxFib = 4000000.0; let Fib n = ...

How to make Haskell use another list depending on the previous answer in Haskell

Hi I am doing project euler question 63 where I must find the amount of numbers that exist where: x^(n) == y Where n is the length of y. It soon emerges that the results for this condition alternate between odd and even and so I came up with this in Haskell: prob63 = [ n | n <- nums n , i <-[1..10], i^(length $ show n) == n] nums n...

Finding Fibonacci sequence in C#. [Project Euler Exercise]

Hi there, I'm having some trouble with this problem in Project Euler. Here's what the question asks: 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 sequenc...

Problems with java.math.BigInteger

I have the following code at the head of a method: BigInteger foo = BigInteger.valueOf(0); BigInteger triNum = BigInteger.valueOf(0); //set min value to 1*2*3*4*5*...*199*200. BigInteger min = BigInteger.ONE; BigInteger temp = BigInteger.ZERO; for(int i=1; i<=200; i++) { temp = BigInteger.valueOf(i); min = min.multiply(temp); }...

Intermediate lists in Haskell

Hi I am doing Project Euler question 55 on Lychrel numbers where the aim is to find the number of Lychrel numbers below 10,000 within 50 iterations. I came up with this: revAdd n = (read $ reverse $ show n) + n lychrel n | length xs == 50 = error "False" | ((reverse $ show (revAdd n)) == (show (revAdd n))) = True | other...

How can I maximally partition a set?

I'm trying to solve one of the Project Euler problems. As a consequence, I need an algorithm that will help me find all possible partitions of a set, in any order. For instance, given the set 2 3 3 5: 2 | 3 3 5 2 | 3 | 3 5 2 | 3 3 | 5 2 | 3 | 3 | 5 2 5 | 3 3 and so on. Pretty much every possible combination of the members of the set....

Finding if two numbers have the same digit and then remove them from in the original number in Haskell

Hi I am doing project euler question 33 and have divised a refactor to solve it but I can't think of how to remove the digit if it is the same across both x and y. I got this far: import Ratio import List p33 = [ (x%y) | y <- [10..99] , x <- [10..y], (x `rem` 10) /= 0 , (y `rem` 10) /= 0 , x /= y , (length $ nub $ concat $ map decToLis...

Finding palindromic numbers in Ruby

So, I'm doing Project Euler to solidify my Ruby skills. I'm on problem #4, which reads: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99. Find the largest palindrome made from the product of two 3-digit numbers. First, I'm trying to ver...

What is Sum of Even Terms In Fibonacci (<4million)? [Large Value Datatype Confusion]

By starting with 1 and 2, the first 10 terms of Fibonacci Series 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 4 million. SOLVED: Actually, I managed to get the solution myself. Here's my program. It works. #include <stdio.h> int main() { int x=1,y=2...

Project Euler #3 Java Solution Problem

class eulerThree { public static void main(String[] args) { double x = 600851475143d; for (double z = 2; z*z <= x; z++) { if (x%z == 0) { System.out.println(z + "PRIME FACTOR"); } } } } and the output is: 71.0 839.0 1471.0 6857.0 59569.0 104441.0 486847.0 So, I assume 486847 is the...

Euler #26, how to convert rational number to string with better precision?

I want to get 1/7 with better precision, but it got truncated. How can I get better precision when I convert a rational number? Thanks. >>> str(1.0/7)[:50] '0.142857142857' ...

Problem in project euler #32

Hi, I'm trying to solve problem 32 of project euler in Java but I'm not getting the correct answer. I'm getting the answer of the following program as 48146. Could anyone please tell me what's wrong I'm doing here and how can I solve that problem? Here's the code through which I'm trying to solve that problem: public class Euler32 { ...

Problem detecting cyclic numbers in Haskell

Hi I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3) `div` 2 p8 n = n*(3*n -2) x n = take 2 $ show n x2 n = reverse $ take 2 $ reverse $ show n pX p = dropWhile (< 999) $ takeWhile (< ...

Euler Problem in Haskell -- Can Someone Spot My Error

Hi all, I'm trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up to the task, but I'm getting a result that looks inconsistent to say the least. Here's my palindrome detector (which was simplic...

Tips for Project Euler Problem #78

This is the problem in question: Problem #78 This is driving me crazy. I've been working on this for a few hours now and I've been able to reduce the complexity of finding the number of ways to stack n coins to O(n/2), but even with those improvements and starting from an n for which p(n) is close to one-million, I still can't reach the...

How to rearrange this function to return the extended list in Haskell

Hi I am doing problem 68 at project euler and came up with the following code in Haskell to return the list of numbers which fit the (given) solution: lists = [n|n<- permutations [1..6] , ring n ] ring [a,b,c,d,e,f] = (length $ nub $ map sum [[d,c,b],[f,b,a],[e,a,c]]) == 1 This only returns a list of lists of 6 numbers each which f...

How to recursively compare the digits in a number in Haskell

Hi I am doing problem 112 on Project Euler and came up with the following to test the example case (I'll change the number in answer to 0.99 to get the real answer): isIncre x | x == 99 = False | otherwise = isIncre' x where isIncre' x = ??? isDecre x = isIncre (read $ reverse $ show x :: Int) isBouncy x = (is...

How to left/right truncate numbers without strings (Euler #37)

As stated in problem 37 at Project Euler: The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. I already solved this problem (answer ends ...

Project Euler #10 Conundrum

So after pulling my hair out for 30 minutes, I have decided to come to SO for some help on Project Euler #10: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. Now, I don't want to know how to do the problem - that's easy - and especially not the answer. I would like to kno...

What is wrong with my "digit finder" in python?

I am learning python through the Project Euler problems. For problem 40 I wrote this code: import math i = 1 counter = 0 while counter <= 1000000: MMM = int(math.log(i, 10)) + 1 counter = counter + MMM V = math.log(i, 10) print(i, counter, MMM, V) i += 1 It is supposed to return the number containing the Nth dig...