project-euler

Project Euler Question 14 (Collatz Problem)

The following iterative sequence is defined for the set of positive integers: n ->n/2 (n is even) n ->3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 40 20 10 5 16 8 4 2 1 It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it ha...

Project Euler: Programmatic Optimization for Problem 7?

So I would call myself a fairly novice programmer as I focused mostly on hardware in my schooling and not a lot of Computer Science courses. So I solved Problem 7 of Project Euler: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? I managed t...

Parse int to string with stringstream

Well! I feel really stupid for this question, and I wholly don't mind if I get downvoted for this, but I guess I wouldn't be posting this if I had not at least made an earnest attempt at looking for the solution. I'm currently working on Euler Problem 4, finding the largest palindromic number of two three-digit numbers [100..999]. As...

How do I break out of a loop in Scala?

For Problem 4 of Project Euler How do I break out a loop? var largest=0 for(i<-999 to 1 by -1) { for (j<-i to 1 by -1) { val product=i*j if (largest>product) // I want to break out here else if(product.toString.equals(product.toString.reverse)) largest=largest max product } } And does anyone know how to turn nested for l...

Have I checked every consecutive subset of this list?

I'm trying to solve problem 50 on Project Euler. Don't give me the answer or solve it for me, just try to answer this specific question. The goal is to find the longest sum of consecutive primes that adds to a prime below one million. I wrote a sieve to find all the primes below n, and I have confirmed that it is correct. Next, I am goi...

Project Euler 9 Understanding

This question states: A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. I'm not sure what's it trying to ask you. Are we trying to find a2 + b...

Sum of Fibonacci numbers

Hi, I'm rather new to Haskell. The problem is to find the sum of all even Fibonacci numbers not greater than 4 million. I can't use lists. If I understand correctly, the below solution is wrong, because it uses lists: my_sum = sum $ filter (odd) $ takeWhile (< 4000000) fibs Where fibs is the list of all Fibonacci numbers. Somehow,...

Multiplying numbers on horizontal, vertial, and diagonal lines

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 20x20 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 prob...

Find Pythagorean triplet for which a + b + c = 1000

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. Source: http://projecteuler.net/index.php?section=problems&amp;id=9 I tried but didn't know where my code...

Project Euler #163 understanding

I spent quite a long time searching for a solution to this problem. I drew tons of cross-hatched triangles, counted the triangles in simple cases, and searched for some sort of pattern. Unfortunately, I hit the wall. I'm pretty sure my programming/math skills did not meet the prereq for this problem. So I found a solution online in ord...

Why is Java not telling me when I can't use Integer?

For a small project (Problem 10 Project Euler) i tried to sum up all prime numbers below 2 millions. So I used a brute force method and iterated from 0 to 2'000'000 and checked if the number is a prime. If it is I added it to the sum: private int sum = 0; private void calculate() { for (int i = 0; i < 2000000; i++) { if (i.isP...

Is there any way to maximize PHP_INT_MAX?

I can't find this row in php.ini, so is there any way to do that? So ok, I understood, the answer is not, then how should I calculate the result of this task if I want it do with php? The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? ...

C++ Euler-Problem 14 Program Freezing

I'm working on Euler Problem 14: http://projecteuler.net/index.php?section=problems&amp;id=14 I figured the best way would be to create a vector of numbers that kept track of how big the series was for that number... for example from 5 there are 6 steps to 1, so if ever reach the number 5 in a series, I know I have 6 steps to go and I h...

Encrypt text using a number

Project Euler I have recently begun to solve some of the Project Euler riddles. I found the discussion forum in the site a bit frustrating (most of the discussions are closed and poorly-threaded), So I have decided to publish my Python solutions on launchpad for discussion. The problem is that it seems quite unethical to publish these ...

Project Euler 79: what am I missing?

Hi Guys, I'm not interested in the answer, but I need to be pointed in the right direction Here's problem 79 I first try to analyze the file myself. I've noticed that the number 7 only ever appears as the first digit. This immediately implies that all the numbers containing 7 never overlaps with under numbers (on the left side). Sin...

Project Euler #14 and memoization in Clojure

As a neophyte clojurian, it was recommended to me that I go through the Project Euler problems as a way to learn the language. Its definitely a great way to improve your skills and gain confidence. I just finished up my answer to problem #14. It works fine, but to get it running efficiently I had to implement some memoization. I could...

Project Euler #9 (Pythagorean triplets) in Clojure

My answer to this problem feels too much like these solutions in C. Does anyone have any advice to make this more lispy? (use 'clojure.test) (:import 'java.lang.Math) (with-test (defn find-triplet-product ([target] (find-triplet-product 1 1 target)) ([a b target] (let [c (Math/sqrt (+ (* a a) (* b b)))] (let [s...

Project Euler: Problem #5 in Ruby

I'm trying to solve Problem #5 in Project Euler. The code works for the example, when I check the numbers from 1 to 10 I get 2520 as a result, which is right. But when I check for the numbers from 1 to 20, the code doesn't stop running. Here it is: num = 0 while true num += 1 check = true for i in 1..20 break un...

Lazy Sequences that "Look Ahead" for Project Euler Problem 14

I'm trying to solve Project Euler Problem 14 in a lazy way. Unfortunately, I may be trying to do the impossible: create a lazy sequence that is both lazy, yet also somehow 'looks ahead' for values it hasn't computed yet. The non-lazy version I wrote to test correctness was: (defn chain-length [num] (loop [len 1 n num] (con...

More ruby-like solution to this problem?

I am learning ruby and practicing it by solving problems from Project Euler. This is my solution for problem 12. # Project Euler problem: 12 # What is the value of the first triangle number to have over five hundred divisors? require 'prime' triangle_number = ->(num){ (num *(num + 1)) / 2 } factor_count = ->(num) do prime_fac = Pr...