Hey /prog/, I know I'm a little late to the party, but I was wondering how many problems you've solved thus far.
Other discussion regarding Project Euler is also welcome!
Name:
Anonymous2010-09-24 23:35
Impromptu challenge proposal:
Solve a P.U. problem of your choice.
If the chosen problem has been solved previously in the thread, your solution must exceed all previous solutions in one or more aspects of: correctness, clarity, conciseness, elimination of redundant calculation.
>>2,12 Problem 1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000. In[1]:= 3 Sum[d, {d, 1, 333}] + 5 Sum[i, {i, 1, 200}] - 15 Sum[x, {x, 1, 66}]
Out[1]= 234168
``faggot'' way
>Problem 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-valued terms in the sequence which do not exceed four million.[/o] In[2]:= n = 1; While[Fibonacci[3 n] < 4000000, n++]
Sum[Fibonacci[3 m], {m, n}]
Out[2]= 19544084
Problem 3
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ? In[3]:= FactorInteger[600851475143][[-1, 1]]
Out[3]= 6857
``faggot'' way
Name:
Anonymous2010-09-25 18:21
Problem 4: 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. ghci> let palindrome x = (show x) == (reverse $ show x)
ghci> last $ filter palindrome [x*y | x <- [900..999], y <- [900..999]
906609
Name:
Anonymous2010-09-25 18:24
Problem 5: What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? *./>:i.20
232792560
Name:
Anonymous2010-09-25 18:29
Problem 6: Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. nums = range(1, 101)
square_sums = sum(nums)**2
sum_squares = sum(x**2 for x in nums)
print abs(square_sums - sum_squares)
>>21
My prediction is that we're picking off the easy ones until we are left with ones that have no insanely simple and short solutions in any language. Then hopefully we'll stumble on something interesting that can be iterated various times.
Name:
Anonymous2010-09-25 18:57
>>21
I guess so. I was making no. 7 in Scheme but can't figure out how to program the Sieve of Eratosthenes for the life of me and clearly have to go read some more SICP.
Here's some very ugly Python. longnum = "731671765313306249192251196744265747423\
55349194934969835203127745063262395783180169848018\
<snip>
55935729725716362695618826704282524836008232575304\
20752963450"
biggest = 0
for i in range(len(longnum) - 5):
biggest = max((biggest, reduce(int.__mul__, map(int, list(longnum[i:i+5])))))
print biggest
http://pastebin.com/gbv94fsc
I can't get the second problem correct, please insult me /prague/. If you would help me that would be nice too.
Name:
Anonymous2010-09-25 19:10
Problem 9 Prelude> let pythagoras a b = sqrt (a^2 + b^2)
Prelude> floor $ head $ [a * b * pythagoras a b | a <- [1..1000], b <- [a..1000], a + b + pythagoras a b == 1000]
31875000
Name:
Anonymous2010-09-25 19:14
51
Name:
Anonymous2010-09-25 19:15
>>25
Line 5 should be int answer = 0, first = 0, current = 0;, else you're just initializing answer and first without a value.
Oups, that doesn't work. Well here is my solution from when I was learning C doing these.
/* Find the sum of all the primes
below two million. */
#include <stdio.h>
void primes(int *arr, int len){
int i, k;
for (i = 2; i*i < len; i++)
if (arr[i])
for (k = i*i; k < len; k += i)
arr[k] = 0;
arr[0] = 0;
arr[1] = 0;
}
int main(int argc, char **argv){
int i, n = 2000000, p[n+1];
long long int s = 0;
for (i = 0; i < n; i++)
p[i] = 1;
primes(p, n);
for (i = 0; i < n; i++)
if (p[i])
s += i;
printf("%lld\n", s);
return 0;
}
Problem 12
What is the value of the first triangle number to have over five hundred divisors? In[12]:= For[i = 1, DivisorSigma[0, i*(i + 1)/2] < 500, i++]; i*(i + 1)/2
Out[12]= 76576500
double combination(double n,double r);
double factorial(double number);
int main()
{
int total = 0;
for(int n = 23;n <= 100;n++)
{
int r = 1;
while(combination(n,r) <= 1000000 && n > r)
{
r++;
}
total += n-r*2 + 1;
>>39 >>40
Please include code tags using brackets in the same manner as other tags in BBCODE by inserting "code" in the brackets.
Name:
Anonymous2010-09-25 23:10
>>41
I understand C++ well, but BBcode will never make sense to me.
Heres my last one
#include <iostream>
#include <gmp.h>
using namespace std;
int sumdigs(mpz_t * num);
int main()
{
mpz_t result;
mpz_init(result);
int best = 0;
for(int a = 1; a < 101;a++)
{
for(int b = 1; b <101;b++)
{
mpz_ui_pow_ui(result,a,b);
int temp = sumdigs(&result);
if( temp > best)
{
best = temp;
}