project-euler

Euler Project No. 2 with Python

Can somebody tell me why this should be wrong? #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. sum=2 list...

generating a binary tree from given data in python

i wanted to know how to read values from a list into a binary tree. i have a triangle like this: 0 1 2 3 4 5 6 7 8 9 i have written a class node like this class node: def __init__(self,data,left=None,right=None): self.data=data self.left=left self.right=right basically...

Tips to solve problem #41 of project euler

I'm trying to solve Problem 41of project Euler in Java, by counting the number from 99888888 to 80000000(which took very long time :( ), I got 98765431 as an answer, but I'm getting that answer not correct. Could anyone please tell me the reason of not getting the correct answer and how can I speed my program? ...

Python function returning None after recursion

I can't figure out why this python function returns None if it calls itself recursively. It was part of my solution to a Project Euler problem. I have solved the problem in a better way anyhow, but this is still annoying me as the function seems to work OK - and it seems to know the value of the variable I wanted to return. def next_p...

Smallest number that is evenly divisible by all of the numbers from 1 to 20?

I did this problem [Project Euler problem 5], but very bad manner of programming, see the code in c++, #include<iostream> using namespace std; // to find lowest divisble number till 20 int main() { int num = 20, flag = 0; while(flag == 0) { if ((num%2) == 0 && (num%3) == 0 && (num%4) == 0 && (num%5) == 0 && (num%6) == 0 &...

PROJECT EULER #29

Well, after solving this problem by naive STL set,I was reading the forum entries,there I find this entry : #include <iostream> #include <cmath> #define MAX 100 using namespace std; int main(){ int res=(MAX-1)*(MAX-1); for(int i=2;i<MAX;i++) for(int j=i*i;j<=MAX;j=j*i) res = res-int(MAX*(log(i)/log(...

Project Euler - Problem 8 - Help with understanding requirement

Hi All, I am working through the problems on project Euler and am not to certain if my understanding of the question is correct. Problem 8 is as follows: Find the greatest product of five consecutive digits in the 1000-digit number. I have taken this to mean the following: I need to find any five numbers that run consecutively i...

Project Euler #15

Hey everyone, Last night I was trying to solve challenge #15 from Project Euler: Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner. How many routes are there through a 20×20 grid? I figured this shouldn't be so hard, so I wrote a basic recursive f...

java array problem?

i was finding out highest prime factor which divides num, as shown in program, there's a issue with array and arr[j] = i; j++; Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at primenum.main(primenum.java:13) //to find highest prime factor public class primenum { public static void main(String[] args)...

Project Euler Problem# 276 - Primitive Triangles

Hi, I tried to solve problem# 276 from Project Euler, but without success so far. Consider the triangles with integer sides a, b and c with a ≤ b ≤ c. An integer sided triangle (a,b,c) is called primitive if gcd(a,b,c)=1. How many primitive integer sided triangles exist with a perimeter not exceeding 10 000 000? The bo...

How can determine dice sum probabilities?

In trying to solve a particular Project Euler question, I ran into difficulties with a particular mathematical formula. According to this web page (http://www.mathpages.com/home/kmath093.htm), the formula for determining the probability for rolling a sum, T, on a number of dice, n, each with number of sides, s, each numbered 1 to s, can...

Transforming nested Python loops into list comprehensions

I've started working on some Project Euler problems, and have solved number 4 with a simple brute force solution: def mprods(a,b): c = range(a,b) f = [] for d in c: for e in c: f.append(d*e) return f max([z for z in mprods(100,1000) if str(z)==(''.join([str(z)[-i] for i in range(1,len(str(z))+1)]))]) After solving, I tried t...

MATLAB runs out of memory during program execution

I have been happily using MATLAB to solve some project Euler problems. Yesterday, I wrote some code to solve one of these problems (14). When I write code containing long loops I always test the code by running it with short loops. If it runs fine and it does what it's supposed to do I assume this will also be the case when the length of...

Is any solution the correct solution?

I always think to myself after solving a programming challenge that I have been tied up with for some time, "It works, thats good enough". I don't think this is really the correct mindset, in my opinion and I think I should always be trying to code with the greatest performance. Anyway, with this said, I just tried a ProjectEuler ques...

Python recursive function error: "maximum recursion depth exceeded"

I solved Problem 10 of Project Euler with the following code, which works through brute force: def isPrime(n): for x in range(2, int(n**0.5)+1): if n % x == 0: return False return True def primeList(n): primes = [] for i in range(2,n): if isPrime(i): primes.append(i) retu...

Project Euler 7 Scala Problem

I was trying to solve Project Euler problem number 7 using scala 2.8 First solution implemented by me takes ~8 seconds def problem_7:Int = { var num = 17; var primes = new ArrayBuffer[Int](); primes += 2 primes += 3 primes += 5 primes += 7 primes += 11 primes += 13 while (primes.size < 10001){ ...

C++ Perfect Number. Need some help revising.

I need some help revising this. It keeps only displaying 0s as the temp. Thank you. // A program to determine whether the input number is a perfect number // A perfect number is defined by the sum of all its positive divisors excluding itself // 28: 1+2+3+7+14 = 28. int perfect, limit, divisor; cout << "Please enter a positive intege...

Fastest algorithm to check if a number is pandigital?

Pandigital number is a number that contains the digits 1..number length. For example 123, 4312 and 967412385. I have solved many Project Euler problems, but the Pandigital problems always exceed the one minute rule. This is my pandigital function: private boolean isPandigital(int n){ Set<Character> set= new TreeSet<Character>();...

Trying to calculate the 10001st prime number in Java.

Greetings. I am doing Problem 7 from Project Euler. What I am supposed to do is calculate the 10001st prime number. (A prime number being a number that is only divisible by itself and one.) Here is my current program: public class Problem7 { public static void main(String args []){ long numberOfPrimes = 0; long number = 2; ...

Sum of even fibonacci numbers

This is a Project Euler problem. If you don't want to see candidate solutions don't look here. Hello you all! im developping an application that will find the sum of all even terms of the fibonacci sequence. The last term of this sequence is 4,000,000 . There is something wrong in my code but I cannot find the problem since it makes se...