I'm trying to learn Ruby, and am going through some of the Project Euler problems. I solved problem number two as such:
def fib(n)
return n if n < 2
vals = [0, 1]
n.times do
vals.push(vals[-1]+vals[-2])
end
return vals.last
end
i = 1
s = 0
while((v = fib(i)) < 4_000_000)
s+=v if v%2==0
i+=1
end
puts s
While that work...
This is the problem:
How many integers 0 n < 1018 have
the property that the sum of the
digits of n equals the sum of digits
of 137n?
This solution is grossly inefficient. What am I missing?
#!/usr/bin/env python
#coding: utf-8
import time
from timestrings import *
start = time.clock()
maxpower = 18
count = 0
for i in r...
I'm trying to write a function to return the word string of any number less than 1000.
Everytime I run my code at the interactive prompt it appears to work without issue but when I try to import wordify and run it with a test number higher than 20 it fails as "TypeError: 'function' object is unsubscriptable".
Based on the error message...
Source: http://projecteuler.net/index.php?section=problems&id=11
Quick overview: Take a 20x20 grid of numbers and compute the largest product of 4 pairs of numbers in either horizontal, vertical, or diagonal.
My current approach is to divide the 20x20 grid up into single rows and single columns and go from there with a much more m...
I've solved 84 of the Project Euler problems, mostly in Haskell. I am now going back and trying to solve in J some of those I already solved in Haskell, as an exercise in learning J.
Currently, I am trying to solve Problem 56. Let me stress that I already know what the right answer is, since I've already solved it in Haskell. It's a ver...
Hi, I'm trying to find the sum of the prime numbers < 2,000,000. This is my solution in java but I can't seem get the correct answer. Please give some input on what could be wrong and general advice on the code is appreciated.
Printing 'sum' gives: 1308111344, which is incorrect.
edit:
Thanks for all the help. Changed int to long and ...
I'm doing Project Euler to learn Clojure.
The purpose of this function is to calculate the lcm of the set of integers from 1 to m.
(lcm 10) returns 2520
This is a rather brute-force way of doing this. In theory, we go through each number from m to infinity and return the first number for which all values 1 through m divide that number...
I'm trying to solve Project Euler #9, which is http://projecteuler.net/index.php?section=problems&id=9.
I've looked through this code, and the logic seems right…but I'm not getting any output at all, not even the printfs in the loop. I'm (obviously) a C newbie, trying to learn from higher level languages…could you tell me what's goi...
I'm working on Project Euler #14 in C and have figured out the basic algorithm; however, it runs insufferably slow for large numbers, e.g. 2,000,000 as wanted; I presume because it has to generate the sequence over and over again, even though there should be a way to store known sequences (e.g., once we get to a 16, we know from previous...
Hi Guys , Well i have a probleme with some puzzles , However i want to solve this puzzle and i need your help , i have an array from ten Numbers(Or More),for example :
arr[1,2,3,4,5,6,7,8,9,10]
i want a method to check if 3 numbers from this arrays have the same sum to another 3 numbers (I want to get all the possibilities ! ) , for ...
I'm starting to go through the questions in project Euler, and I'd like to approach it with a TDD style, but I'm having trouble finding the numeric answer to the question that doesn't include the code. Is there any resource with that data so that I can make test cases that will tell me if I've solved the problem correctly?
My motivation...
I'm a student with an interest in computer science and a lot of time on my hands. I've started solving Project Euler problems in PHP and Python, and most of the early problems are pretty easy to solve with either brute-force or common-sense solutions. However, when compared to the elegant solutions I see on some of the forums, my solutio...
For any N, let f(N) be the last five
digits before the trailing zeroes in
N!. For example,
9! = 362880 so f(9)=36288
10! = 3628800 so f(10)=36288
20! = 2432902008176640000 so f(20)=17664
Find f(1,000,000,000,000)
I've successfully tackled this question for the given examples, my function can correctly find f(9), f(10),...
I'm just getting back into Project Euler and have lost my account and solutions, so I'm back on problem 7. However, my code doesn't work. It seems fairly elementary to me, can someone help me debug my (short) script?
Should find the 10001st Prime.
#!/usr/bin/env python
#encoding: utf-8
"""
P7.py
Created by Andrew Levenson on 2010-06-2...
I'm making my way through project Euler and I'm trying to write the most concise code I can. I know it's possible, so how could I simplify the following code. Preferably, I would want it to be one line and not use the int->string->int conversion.
Question: What is the sum of the digits of the number 21000?
My answer:
>>> i=0
>>> for...
I'm again working on Project Euler, this time problem #4. The point of this script is to find the largest palindromic product of two three digit numbers. I thought it was fairly straightforward to solve, but I'm getting an answer that is too low. More specifically, I am getting 580085, and the answer is 906609.
Could someone tell me wh...
Once again working on Project Euler, this time my script just hangs there. I'm pretty sure I'm letting it run for long enough, and my hand-trace (as my father calls it) yields no issues. Where am I going wrong?
I'm only including the relevant portion of the code, for once.
def main():
f, n = 0, 20
while f != 20:
f = 0
...
here is problem about sum of factorial of digit
http://projecteuler.net/index.php?section=problems&id=254
also
here is Define sf(n) as the sum of the digits of f(n). So sf(342) = 3 + 2 = 5.
here is code which prints 32 as sum of digit's factorials but it does not show me second result
import java.util.*;
public class fact_of_d...
I am working on Problem 14 on Project Euler, and my code seems to freeze at random intervals for no apparent reason.
static void Main()
{
int maxNum = 0;
int maxLength = 0;
for (int x = 2; x < 1000000; ++x)
{
int num = x;
int length = 0;
while (num != 1)
{
if (num % 2 == 0)
...
I'll try to be concise this time around! I'm still working Project Euler, this time back to #2. My real issue here is I'm terrible with Ruby. When I run the following code
x = 1
y = 2
sum = 2
while x >= 4_000_000 do |x|
sum += y if y % 2 == 0
z = x + y
x = x ^ y # xor magic
y = x ^ y # xor magic
x = x ^ y # xor magic...