Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Project Euler

Name: Anonymous 2010-09-24 23:18

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: Anonymous 2010-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.

Name: Anonymous 2010-09-25 0:18

40, but I haven't visited it in ages.

Name: Anonymous 2010-09-25 0:32

Im at 33, but im spending all my time on the google AI challenge at the moment

Name: Anonymous 2010-09-25 0:39

70, like 3 years ago

Name: Anonymous 2010-09-25 4:42

62, also like 3 years ago

Name: Anonymous 2010-09-25 7:44

12

Name: Anonymous 2010-09-25 9:03

0

Name: Anonymous 2010-09-25 9:26

By solving problems from Project Euler you slowly, gradually, step by step, become very proficient at solving problems from Project Euler.

Name: Anonymous 2010-09-25 10:52

2

Name: Anonymous 2010-09-25 11:18

>>9
By existing you slowly, gradually, step by step, become very proficient at breathing.

Name: Anonymous 2010-09-25 13:53

>>2
1st Problem


#include <stdio.h>

main()
{
    float answer = 0;
    int i = 1;
   
    while (i < 1000)
    {
        labelA:
        if ((i%15) == 0)
        {answer+=i;
        i++;
        goto labelA;}
        if ((i%5) == 0)
        {answer+=i;}
        if ((i%3) == 0)
        {answer+=i;}
        i++;
    }
    printf("%f", answer);
    return 0;
}

Name: Anonymous 2010-09-25 13:53

>>11
Manually.

Name: Anonymous 2010-09-25 16:12

>>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

Name: Anonymous 2010-09-25 16:30

>>2,12,14
Junctions ahoy!

say [+] grep { $_ %% (3|5) }, ^1000;

P.S. >>14: Your solution includes 1000 in the sum.

Name: Anonymous 2010-09-25 17:14

>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


``faggot'' way

Name: Anonymous 2010-09-25 17:22

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: Anonymous 2010-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: Anonymous 2010-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: Anonymous 2010-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)

Name: Anonymous 2010-09-25 18:33

Are we doing every Euler project problem?

Name: Anonymous 2010-09-25 18:43

Problem 7
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?

In[7]:= Prime[10001]
Out[7]= 104743


``faggot'' way

Name: Anonymous 2010-09-25 18:56

>>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: Anonymous 2010-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

Name: Anonymous 2010-09-25 19:04

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: Anonymous 2010-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: Anonymous 2010-09-25 19:14

51

Name: Anonymous 2010-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.

Name: Anonymous 2010-09-25 19:30

"""The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million."""

import numpy as np

def sieve(n):
    if n <= 1: return np.array([])
    u = int( np.sqrt(n) )
    plist = np.arange(1, n+1, 2, dtype=int)
    for k in np.arange(1, u):
        if plist[k]:
            plist[ (plist[k] * plist[k])/2 :: plist[k] ] = 0
    plist[0] = 2
    return plist[np.flatnonzero( plist )]

print np.sum( sieve(2000000).tolist() )

Name: Anonymous 2010-09-25 19:33

>>29

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;
}

Name: Anonymous 2010-09-25 19:44

>>29

Alright the error is using np.sum instead of just sum.

Name: Anonymous 2010-09-25 20:03

Problem 16: What is the sum of the digits of the number 2^1000?
Prelude Char> sum $ map digitToInt $ show $ 2^1000
1366

Name: Anonymous 2010-09-25 21:09

>>28
Thanks friend, I thought it was something menial, but I wasn't sure.

Name: Anonymous 2010-09-25 21:20

70 something, 2-3 years ago

Name: Anonymous 2010-09-25 21:35

Problem 10
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

In[10]:= n = 0; For[i = 1, Prime[i] < 2000000, i++, n += Prime[i]]; n
Out[10]= 142913828922


``faggot'' way

Name: Anonymous 2010-09-25 21:51

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


``faggot'' way

Name: Anonymous 2010-09-25 21:52

>>35

I don't see how using Mathematica is particularly ``faggot''-y.

Name: Anonymous 2010-09-25 21:54

>>36

What about #11? ;_;

Name: Anonymous 2010-09-25 22:20

#53

Some particularly elegant code

<code>
#include <iostream>


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;

    }
    std::cout << total;
}
double combination(double n,double r)
{
    return factorial(n) / ( factorial(r) * factorial(n-r));
}
double factorial(double number)
{
    double temp;
    if (number <= 1) return 1;
    temp = number * factorial(number - 1);
    return temp;
}
</code>

Name: Anonymous 2010-09-25 22:53

97

#include <iostream>

using namespace std;

int main()
{
    char number[10];
    for(int i = 0; i < 10;i++)
    {
        number[i] = 0;
    }
    number[9] = 2;
    for(int i = 0; i <7830457 - 1;i++)
    {
        for(int j = 9; j > -1;j--)
        {
            number[j] *=2;
        }
        for(int j = 9; j > -1;j--)
        {
            if(number[j] > 9)
            {
                number[j] -= 10;
                if( j != 0 )
                {
                    number[j-1]++;
                }
            }
        }
    }
    for(int i = 0; i < 10;i++)
    {
        cout << (int)number[i];
        // now use a calculator to multiply by 28433 and + 1
    }
}

Name: Anonymous 2010-09-25 23:03

>>39
>>40
Please include code tags using brackets in the same manner as other tags in BBCODE by inserting "code" in the brackets.

Name: Anonymous 2010-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;
            }


        }
    }
    cout<<best;
    return 0;
}
int sumdigs(mpz_t * num)
{

    char holder[202];
    mpz_get_str(holder,10,*num);
    int length = 0;
    int sum = 0;
    while(1)
    {
        if(holder[length] == 0)
        {
            break;
        }
        length++;
    }
    for(int i = 0; i < length;i++)
    {
        sum += holder[i] - 48;
    }
    return sum;
}

Name: Anonymous 2010-09-26 0:08

>>42
BBCODE is rather simple.
Put tags around shit to manipulate how it appears. Close tags like you would with braces in a programming language.

Name: Anonymous 2010-09-26 1:53

>>41
Please optimize your post references.

>>43
BBCODE's HTML-inspired verbosity means it doesn't work nearly as conveniently as just using }.

Name: Anonymous 2010-09-27 17:27

>>16
say [+] grep {$_ %% 2}, ( 0,1, { $^a + $^b }  ...^ * > 4_000_000);

Name: Anonymous 2010-09-28 13:50

Problem 303
For a positive integer n, define f(n) as the least positive multiple of n that, written in base 10, uses only digits ≤2.

Thus f(2)=2, f(3)=12, f(7)=21, f(42)=210, f(89)=1121222.

Also sum_n=1^100 {f(n)/n} = 11363107

Find sum_n=1^10000 {f(n)/n}.

http://projecteuler.net/index.php?section=problems&id=303

f[n_] := FromDigits@IntegerDigits[n, 3]
Timing[Sum[For[i = 1, Mod[f[i], n] != 0, i++]; f[i]/n, {n, 100}]]
{0.206891, 11363107}


Above is a brute force that is really slow and inefficient. This is ok for some of the earlier problems, but not this one.

I was thinking using a some divisibility tests and modular arithmetic such as checking ending digits and adding blocks of digits described here http://webspace.ship.edu/msrenault/divisibility and here http://webspace.ship.edu/msrenault/divisibility/StupidDivisibilityTricks.pdf

Name: Anonymous 2010-09-28 14:28

>>46
Example for finding a multiple of 999 that fits the using only 0, 1, and 2 for digits criterion.

999 factors to 33 × 37.

999 can be checked for divisibility by both 27 and 37 by summing the digits into blocks of 3 and checking their divisibility.

Because 999 is the LCM of 37 and 27, the lowest sum we are aiming for is 999.

{0, 1, 2, 10, 11, 12, 20, 21, 22, 100, 101, 102, 110, 111, 112, 120, 121, 122, 200, 201, 202, 210, 211, 212, 220, 221, 222}

So we get there using the fewest amount of digits and trying to aim for the smallest number possible using what we have above.

111 + 222 + 222 + 222 + 222 = 999

111,222,222,222,222 is the resulting number.

Name: Anonymous 2010-11-03 2:38

Name: Anonymous 2011-02-03 0:16

Name: Anonymous 2011-03-10 17:47


Don't change these.
Name: Email:
Entire Thread Thread List