11
\$\begingroup\$

 

Project Euler is another fun programming challenge site to compete (well, play) on. Early problems start off gently, but then explode in difficulty beyond the first hundred or so. The first few problems have some commonality between finding primes, multiples, and factors, so there could be some interesting code micro-reuse opportunities to toy with.

So, write a program that solves, using no a priori knowledge, any of the first 9 problems.

  1. The problem is selected by the user, ASCII '1' through '9', inclusive, via an argument when calling or standard input while running. (You can compute all the answers, but only show one.)
  2. The correct answer must be printed on a new line, in base 10, using ASCII.
  3. Programs should execute in less than a minute (PE suggestion).
  4. By "no a priori knowledge", I mean your code must derive the answer without external resources. A program like this would be considered invalid (otherwise correct though, assuming I didn't make a typo):

    print[233168,4613732,6857,906609,232792560,25164150,104743,40824,31875000][input()-1]
    

    for problem #8 (involves a 1000-digit number), you may read the number from an external file, just specify how it's stored (e.g. binary, text, header, imported module) and/or include it in your answer post (does not count towards the length of the main program).

  5. Score is by bytes.

  6. Fifteen Unicorn Points™ awarded to the byte-count leader after 2 weeks.
\$\endgroup\$
4
\$\begingroup\$

Python, 505

import f
A,B,C,D=map(range,[22,1000,101,500])
R=reduce
M=int.__mul__
a=x=0
b=1
n=2
p=[]
while b<=4e6:a,b=b,a+b;x+=b*(b%2<1)
while len(p)<=1e4:p+=[n]*all(n%i for i in p);n+=1
q=y=R(M,p[:8])
while any(y%(i+1)for i in A):y+=q
print[
    sum(i for i in B if i%3*(i%5)<1),
    x,
    max(i for i in p if 600851475143%i<1),
    max(a*b for a in B for b in B if`a*b`==`a*b`[::-1]),
    y,
    sum(C)**2-sum(i**2for i in C),
    p[-1],
    max(R(M,map(int,f.s[i:i+5]))for i in B),
    [a*b*(1000-a-b)for a in D for b in D if(a+b)*1e3==5e5+a*b][0]
][input()-1]

Whitespace is added to the last line for readability. The 1000-digit number is imported from a module named f.py containing the line:

s="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
\$\endgroup\$
3
\$\begingroup\$

Javascript

Solution: 1785 chars

execute code with nodeJS

note on problem 7 : algorithm is ok, but it take a moment ! If someone has a more efficient solution...

z=process.argv[2]
y=console.log
if(z==1){b=0;for(i=1e3;i--;)if(i%3<1||i%5<1)b+=i;y(b)}
if(z==2){d=e=1;f=0;while(e<=4e6)g=d+e,d=e,e=g,f+=e%2<1?e:0;y(f)}
if(z==3){e=Math.sqrt(d=600851475143)|0+1;f=2;while(f<e){g=d/f;if(g==(g|0))h=f,d=g;f+=f==2?1:2}y(h)}
if(z==4){for(a=b=100,c=0;a+b<1998;){d=a++*b;if(d==(""+d).split("").reverse().join("")&&d>c)c=d;if(a>999)a=b+1,b++}y(c)}
if(z==5){for(a=c=1;c;){for(b=20;b>1;)a%b>0?b=0:b--;b==1?c=0:a++}y(a)}
if(z==6){for(a=100,b=Math.pow(a*(a+1)/2,2),d=a+1;d--;)b-=d*d;y(b)}
if(z==7){for(a=3,c=d=0;c<1e4;){for(b=a;b>2;){b=b>3?b-2:2;if(a%b<1)b=0}if(b>0)c++,d=a;a=a+2}y(d)}
if(z==8){a="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";for(e=0,b=996;b--;){d=eval(a.substr(b,5).split("").join("*"));if(d>e)e=d};y(e)}
if(z==9){for(a=b=0;(c=a+b+Math.sqrt(a*a+b*b))!=1e3;)if(++a==500)a=0,b++;y(a,b,c-b-a)}

problem 1: 39 chars

b=0;for(i=1e3;i--;)if(i%3<1||i%5<1)b+=i

problem 2: 49 chars

d=e=1;f=0;while(e<=4e6)g=d+e,d=e,e=g,f+=e%2<1?e:0

problem 3: 85 chars

e=Math.sqrt(d=600851475143)|0+1;f=2;while(f<e){g=d/f;if(g==(g|0))h=f,d=g;f+=f==2?1:2}

problem 4: 105 chars

for(a=b=100,c=0;a+b<1998;){d=a++*b;if(d==(""+d).split("").reverse().join("")&&d>c)c=d;if(a>999)a=b+1,b++}

problem 5: 55 chars

for(a=c=1;c;){for(b=20;b>1;)a%b>0?b=0:b--;b==1?c=0:a++}

problem 6: 51 chars

for(a=100,b=Math.pow(a*(a+1)/2,2),d=a+1;d--;)b-=d*d

problem 7: 82 chars

for(a=3,c=d=0;c<1e4;){for(b=a;b>2;){b=b>3?b-2:2;if(a%b<1)b=0}if(b>0)c++,d=a;a=a+2}

problem 8: 1078 chars ^_^

a="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
for(e=0,b=996;b--;){d=eval(a.substr(b,5).split("").join("*"));if(d>e)e=d}

problem 9: 58 chars

for(a=b=0;a+b+Math.sqrt(a*a+b*b)!=1e3;)if(++a==500)a=0,b++
\$\endgroup\$
  • \$\begingroup\$ if(i%3<1||i%5<1)a+=i is shorter ! :) \$\endgroup\$ – Michael M. Feb 10 '14 at 9:03
  • \$\begingroup\$ Shorter problem 3: e=Math.sqrt(d=600851475143)|0+1;f=2;while(f<e){g=d/f;if(g==g|0)h=f,d=g;f+=f==2?1:2} \$\endgroup\$ – Florent Feb 10 '14 at 13:19
  • \$\begingroup\$ Shorter problem 4: for(a=b=100,c=0;a+b<1998;){d=a++*b;if(d==(""+d).split("").reverse().join("")&&d>c)c=d;if(a>999)a=b+1,b++} \$\endgroup\$ – Florent Feb 10 '14 at 13:26
  • \$\begingroup\$ @Florent : problem 3 g==g|0 doesn't work g|0 must be between parenthesis \$\endgroup\$ – guy777 Feb 10 '14 at 13:47
  • 1
    \$\begingroup\$ @Florent : the solution is in variable h, not the value returned by the script \$\endgroup\$ – guy777 Feb 10 '14 at 14:46
1
\$\begingroup\$

R 684 characters

f=function(N){r=rowSums;p=function(x,y)r(!outer(x,y,`%%`));S=sum;x=c(1,1);n=600851475143;a=900:999;b=20;c=1:100;m=2:sqrt(n);M=m[!n%%m];A=a%o%a;d=3;P=2;z=1:1e3;Z=expand.grid(z,z);Y=cbind(Z,sqrt(r(Z^2)));W=gsub("\n","",xpathApply(htmlParse("http://projecteuler.net/problem=8"),"//p",xmlValue)[[2]]);switch(N,S(which(p(1:999,c(3,5))>0)),{while(tail(x,1)<4e6)x=c(x,S(tail(x,2)));S(x[!x%%2])},max(M[p(M,M)<2]),max(A[sapply(strsplit(c(A,""),""),function(x)all(x==rev(x)))],na.rm=T),{while(any(b%%1:20>0))b=b+20;b},S(c)^2-S(c^2),{while(P<=1e4){d=d+2;if(sum(!d%%2:d)<2)P=P+1};d},max(sapply(5:nchar(W),function(i)prod(as.integer(strsplit(substr(W,i-4,i),"")[[1]])))),prod(Y[r(Y)==1000,][1,]))}

Indented:

f=function(N){
    r=rowSums
    p=function(x,y)r(!outer(x,y,`%%`))
    S=sum
    x=c(1,1)
    n=600851475143
    a=900:999
    b=20
    c=1:100
    m=2:sqrt(n)
    M=m[!n%%m]
    A=a%o%a
    d=3
    P=2
    z=1:1e3
    Z=expand.grid(z,z)
    Y=cbind(Z,sqrt(r(Z^2)))
    W=gsub("\n","",xpathApply(htmlParse("http://projecteuler.net/problem=8"),"//p",xmlValue)[[2]])
    switch(N,S(which(p(1:999,c(3,5))>0)),
             {while(tail(x,1)<4e6)x=c(x,S(tail(x,2)));S(x[!x%%2])},
             max(M[p(M,M)<2]),
             max(A[sapply(strsplit(c(A,""),""),function(x)all(x==rev(x)))],na.rm=T),
             {while(any(b%%1:20>0))b=b+20;b},
             S(c)^2-S(c^2),
             {while(P<=1e4){d=d+2;if(sum(!d%%2:d)<2)P=P+1};d},
             max(sapply(5:nchar(W),function(i)prod(as.integer(strsplit(substr(W,i-4,i),"")[[1]])))),
             prod(Y[r(Y)==1000,][1,]))
    }

Usage:

> f(1)
[1] 233168
> f(2)
[1] 4613732
> f(3)
[1] 6857
> f(4)
[1] 906609
> f(5)
[1] 232792560
> f(6)
[1] 25164150
> f(7)
[1] 104743
> f(8)
[1] 40824
> f(9)
[1] 31875000

Separately:

1: 48 chars sum(which(rowSums(!outer(1:999,c(3,5),`%%`))>0))

2: 64 chars x=c(1,1);while(tail(x,1)<4e6)x=c(x,sum(tail(x,2)));sum(x[!x%%2])

3: 73 chars n=600851475143;m=2:sqrt(n);M=m[!n%%m];max(M[rowSums(!outer(M,M,`%%`))<2])

4: 88 chars a=900:999;b=a%o%a;max(b[sapply(strsplit(c(b,""),""),function(x)all(x==rev(x)))],na.rm=T)

5: 34 chars a=20;while(any(a%%1:20>0))a=a+20;a

6: 25 chars a=1:100;sum(a)^2-sum(a^2)

7: 54 chars d=3;P=2;while(P<=1e4){d=d+2;if(sum(!d%%2:d)<2)P=P+1};d

8: 181 chars

First line reads the number from the project euler website, second line actualy perform the computation.

W=gsub("\n","",xpathApply(htmlParse("http://projecteuler.net/problem=8"),"//p",xmlValue)[[2]])
max(sapply(5:nchar(W),function(i)prod(as.integer(strsplit(substr(W,i-4,i),"")[[1]]))))

9: 87 chars z=1:1e3;Z=expand.grid(z,z);Y=cbind(Z,sqrt(rowSums(Z^2)));prod(Y[rowSums(Y)==1000,][1,])

\$\endgroup\$
  • \$\begingroup\$ As of now, f(7) takes 2 mins to compute so it doesn't really comply to the execution time constraint. I'll try to work on that. (f(5) takes 48s on my machine) \$\endgroup\$ – plannapus Feb 10 '14 at 13:15
1
\$\begingroup\$

J 245 236 232

load'n'
echo".>(<:".1!:1]1){<;._1'!+/I.+./0=5 3|,:~i.1e3!+/}:(],4&*@:{:+_2&{)^:(4e6>{:)^:_]0 2!{:q:600851475143!>./(#~(-:|.)@":"0),/*/~i.1e3!*./>:i.20!(([:*:+/)-[:+/*:)i.101!p:1e4!>./5*/\"."0 n!x:*/{.(#~1e3=+/"1)(+.,|)"0,j./~i.500'

n is a file containing the following:

n=:'7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'
\$\endgroup\$
0
\$\begingroup\$

TI-BASIC (Work in Progress)

For your TI-83 or TI-84 calculator

Main program, 15 bytes:

Input X:OpenLib(1):X:ExecLib:Disp Ans

Library 1 (4 bytes) does count toward the total number of bytes:

L1(Ans

Then, we have List #1:

work in progress
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.