Front Euler 9

11

 

Project Euler adalah situs tantangan pemrograman menyenangkan lainnya untuk bersaing (well, play). Masalah awal dimulai dengan lembut, tetapi kemudian meledak dalam kesulitan di luar sekitar seratus pertama. Beberapa masalah pertama memiliki beberapa kesamaan antara menemukan bilangan prima, kelipatan, dan faktor-faktor, sehingga mungkin ada beberapa kode yang menarik peluang penggunaan kembali mikro untuk dipermainkan.

Jadi, tulislah sebuah program yang memecahkan, tanpa menggunakan pengetahuan apriori , salah satu dari 9 masalah pertama .

  1. Masalahnya dipilih oleh pengguna, ASCII '1' hingga '9', inklusif, melalui argumen saat memanggil atau input standar saat berjalan. (Anda dapat menghitung semua jawaban, tetapi hanya menunjukkan satu.)
  2. Jawaban yang benar harus dicetak pada baris baru, di basis 10, menggunakan ASCII.
  3. Program harus dijalankan dalam waktu kurang dari satu menit (saran PE).
  4. Dengan "tanpa pengetahuan apriori ", maksud saya kode Anda harus mendapatkan jawabannya tanpa sumber daya eksternal . Program seperti ini akan dianggap tidak valid (jika tidak benar, dengan asumsi saya tidak membuat kesalahan ketik):

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

    untuk masalah # 8 (melibatkan angka 1000 digit), Anda dapat membaca nomor dari file eksternal, cukup tentukan bagaimana penyimpanannya (misalnya biner, teks, header, modul yang diimpor) dan / atau sertakan dalam pos jawaban Anda ( tidak dihitung terhadap panjang program utama).

  5. Skor adalah dengan byte.

  6. Lima belas Unicorn Points ™ diberikan kepada pemimpin byte-count setelah 2 minggu.
Nick T
sumber

Jawaban:

4

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]

Spasi ditambahkan ke baris terakhir untuk dibaca. Nomor 1000 digit diimpor dari modul bernama yang f.pyberisi baris:

s="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
grc
sumber
3

Javascript

Solusi: 1785 karakter

mengeksekusi kode dengan nodeJS

perhatikan pada masalah 7: algoritma ok, tapi perlu beberapa saat! Jika seseorang memiliki solusi yang lebih efisien ...

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

masalah 1: 39 karakter

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

masalah 2: 49 karakter

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

masalah 3: 85 karakter

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}

masalah 4: 105 karakter

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

masalah 5: 55 karakter

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

masalah 6: 51 karakter

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

masalah 7: 82 karakter

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}

masalah 8: 1078 karakter ^ _ ^

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

masalah 9: 58 karakter

for(a=b=0;a+b+Math.sqrt(a*a+b*b)!=1e3;)if(++a==500)a=0,b++
guy777
sumber
if(i%3<1||i%5<1)a+=ilebih pendek! :)
Michael M.
Masalah yang lebih pendek 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}
Florent
Masalah yang lebih pendek 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++}
Florent
@Florent: masalah 3 g==g|0tidak berhasil g|0harus antara kurung
guy777
1
@Florent: solusinya dalam variabel h, bukan nilai yang dikembalikan oleh skrip
guy777
1

R 684 karakter

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,]))}

Bertakuk:

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,]))
    }

Pemakaian:

> 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

Terpisah:

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

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

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

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

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

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

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

8: 181 karakter

Baris pertama membaca nomor dari situs web proyek euler, baris kedua sebenarnya melakukan perhitungan.

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 karakter z=1:1e3;Z=expand.grid(z,z);Y=cbind(Z,sqrt(rowSums(Z^2)));prod(Y[rowSums(Y)==1000,][1,])

plannapus
sumber
Sampai sekarang, f(7)membutuhkan 2 menit untuk menghitung sehingga tidak benar-benar memenuhi batasan waktu eksekusi. Saya akan mencoba mengatasinya. ( f(5)memakan waktu 48
detik di
1

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 adalah file yang mengandung yang berikut:

n=:'7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'
AlliedEnvy
sumber
0

TI-BASIC (Work in Progress)

Untuk kalkulator TI-83 atau TI-84 Anda

Program utama, 15 byte :

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

Perpustakaan 1 ( 4 byte ) tidak dihitung terhadap jumlah total byte:

L1(Ans

Kemudian, kita memiliki Daftar # 1:

work in progress
Timtech
sumber