[very easy] trova il numero che...

Stato
Discussione chiusa ad ulteriori risposte.

Oromis92

Utente Silver
22 Dicembre 2007
102
12
2
84
[si, l'ho già postata su 250 forum diversi, ma la sfida è riuscire a scrivere un programma che lo risolva in meno tempo. adesso sta vincendo darkjoker, in perl :)tunz:) e con 0.006 secondi di elaborazione. Anch'io posto il perl :)tunz:), ma ci mette ben 0.450 secondi :mad: :mad:]

esercizio:

trovare l'UNICO numero a 5 cifre che...
1) la somma delle prime due cifre è 15
2) l'ultima cifra è 2 oppure 6
3) la prima e l'ultima cifra hanno come somma 10
4) tuttle le cifre sommate tra di loro hanno come somma 22
5) le cifre sono tutte diverse tra di loro
6) non c'è neanche uno 0
7) la quarta cifra è pari

può essere tranquillamente risolto a mano e ancora più tranquillamente con un computer...

ecco la soluzione in perl (molto ripetitiva, ma mi è sembrato il modo più rapido):
Codice:
#!/usr/bin/perl -w
print "\nResolve the Bancomat's problem\n\n";

for ($num=0;$num<99999;$num++) {
@numb = split//,$num;
if ($numb[4]) {
if ($numb[0]+$numb[1] == 15) {
if ($numb[4] == 2 or $numb[4] == 6) {
if ($numb[0]+$numb[4] == 10) {
if ($numb[0]+$numb[1]+$numb[2]+$numb[3]+$numb[4] == 22) {
if ($numb[0] != $numb[1]) {
if ($numb[0] != $numb[2]) {
if ($numb[0] != $numb[3]) {
if ($numb[0] != $numb[4]) {
if ($numb[1] != $numb[2]) {
if ($numb[1] != $numb[3]) {
if ($numb[1] != $numb[4]) {
if ($numb[2] != $numb[3]) {
if ($numb[2] != $numb[4]) {
if ($numb[3] != $numb[4]) {
if ($numb[0] != 0) {
if ($numb[1] != 0) {
if ($numb[2] != 0) {
if ($numb[3] != 0) {
if ($numb[4] != 0) {
if ($numb[3] % 2 == 0) {
print "number ==> $num\n\n";


}}}}}}}}}}}}}}}}}}}}}
@numb = ();
}
 
in java:
Codice:
public void cercaNumero(){
        for(int i=12345;i<98765;i++){
            String is=""+i;
            int v0=Integer.parseInt(""+is.charAt(0));
            int v1=Integer.parseInt(""+is.charAt(1));
            int v2=Integer.parseInt(""+is.charAt(2));
            int v3=Integer.parseInt(""+is.charAt(3));
            int v4=Integer.parseInt(""+is.charAt(4));
            if(v0+v1==15){
                if(v4==2 | v4==6){
                    if(v0+v4==10){
                        if(v0+v1+v2+v3+v4==22){
                          if(v0!=v1 && v0!=v2 && v0!=v3 && v0!=v4 && v1!=v2 && v1!=v3 && v1!=v4 && v2!=v3 && v2!=v4 && v3!=v4){
                            if(v0!=0 && v1!=0 && v2!=0 && v3!=0 && v4!=0){
                                if(v3%2==0){
                                    System.out.println(i);
}}}}}}}}}
il mio per ora ci mette 219 millisecondi!
 
Codice:
#include <iostream>
#include <string>
#include <string.h>

using namespace std;

int f(int _last);
int first,second,third,fourth,last;

int main()
{
    int res = f(2);
    if (res!=-1){cout<<res<<endl; return 0;}
    res = f(6);
    cout<<res<<endl;
    return 0;
}

int f(int _last){
    first = 10 - _last;
    second = 15 - first;
    if (second>9) return -1; //deve essere una cifra sola
    int tot = 22 - (first+second+_last); //sicuramente 5
    for (int i=1; i<tot;i++){
        if ((i!=first)&&(i!=second)&&(i!=_last))
            fourth = ((i%2)==0)?i:tot-i; break;
    }
    third = tot - fourth;
    return (_last+fourth*10+third*100+second*1000+first*10000);
}

Result:
Codice:
87142

Process returned 0 (0x0)   execution time : 0.011 s
Press any key to continue.
 
scusate come si fa a sapere in C quanto tempo ci mette un programma ???
(ot)

in linux dai il time prima dell'esecuzione di un comando:
Codice:
oromis92@localhost:~/Scrivania/perl$ time perl bancomat.pl

Resolve the Bancomat's problem


number ==> 87142


real    0m0.493s
user    0m0.492s
sys     0m0.000s

se sei un windozziano... con un cronometro alla mando ;)
 
cronometro a mano? ma sei pazzo, si parla di millisecondi :asd:
nn potrò sapere se è ottimizzato al max ma ci proverò, cmq il trucco è fare prima i test che sembra che escludano + velocemente i numeri
 
lo so, stavo scherzando :D
(perchè sinceramente non so come si fa (e se si può) su windows :look: )

ho abbassato ulteriolmente il tempo di elaborazione...
ho eliminato i print non necessari e aggiunto un "last" che mi era sfuggiuto

Codice:
oromis92@localhost:~/Scrivania/perl$ time perl bancomat.pl

 87142


real    0m0.401s
user    0m0.400s
sys     0m0.004s

anche se penso che il mio codice sarebbe più veloce se fosse così:

Codice:
#!/usr/bin/perl
print "87142\n";
sei millesimi netti :D
 
qualcuno mi testa il mio script?

Codice:
for i in range(10000,100000):
    x=str(i)
    for k in x:
        if x is "0":
            z=True
    try:
        if z:
            continue
    except:
        pass
    if x[0] is not x[1] and x[1] is not x[2] and x[2] is not x[3] and x[3] is not x[4] \
    and x[4] is not x[1] and x[4] is not x[2] and x[4] is not x[0] and x[3] is not x[1] \
    and x[3] is not x[0] and x[2] is not x[0]:
        continue
    if ((int(x[3])%2) is 0) and ((x[-1] is "2") or (x[-1] is "6")) and ((int(x[0])+int(x[1])) is 15) and ((int(x[0])+int(x[-1])) is 10) \
    and (sum([int(f) for f in x]) is 22):
        print x

ad occhio su windows ci mette circa mezzo secondo ma sarei curioso sapere quanto dovrei esattmente ancora ottimizzarlo....
 
Codice:
oromis92@localhost:~/Scrivania$ time python b.py
87322

real    0m1.020s
user    0m1.000s
sys     0m0.008s

ma il risultato è sbagliato...
 
346 millisecondi in PHP! (misurato con il time di GNU/Linux)
Ecco:
PHP:
<?php
for($s=20000;$s<99999;$s++){
	$i = str_split($s,1);
	if ($i[4]) {
		if ($i[0]+$i[1] == 15){
			if ($i[4] == 2 or $i[4] == 6) {
				if ($i[0]+$i[4] == 10) {
					if ($i[0]+$i[1]+$i[2]+$i[3]+$i[4] == 22) {
						if ($i[0] != $i[1] and $i[0] != $i[2] and $i[0] != $i[3] and $i[0] != $i[4]) {
							if ($i[1] != $i[2] and $i[1] != $i[2] and $i[3] != $i[4]) {
								if ($i[2] != $i[3] and $i[3] != $i[4]) {
									if ($i[3] != $i[4]) {
										if ($i[0] != 0 and $i[1] != 0 and $i[2] != 0 and $i[3] != 0 and $i[4] != 0) {
											if ($i[3] % 2 == 0) {
												echo "Numero trovato: ".$s;
												break;
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
?>
Ciauz
 
Malex ha detto:
qualcuno mi testa il mio script?

Codice:
for i in range(10000,100000):
    x=str(i)
    for k in x:
        if x is "0":
            z=True
    try:
        if z:
            continue
    except:
        pass
    if x[0] is not x[1] and x[1] is not x[2] and x[2] is not x[3] and x[3] is not x[4] \
    and x[4] is not x[1] and x[4] is not x[2] and x[4] is not x[0] and x[3] is not x[1] \
    and x[3] is not x[0] and x[2] is not x[0]:
        continue
    if ((int(x[3])%2) is 0) and ((x[-1] is "2") or (x[-1] is "6")) and ((int(x[0])+int(x[1])) is 15) and ((int(x[0])+int(x[-1])) is 10) \
    and (sum([int(f) for f in x]) is 22):
        print x

ad occhio su windows ci mette circa mezzo secondo ma sarei curioso sapere quanto dovrei esattmente ancora ottimizzarlo....

ecco il mio output:
Codice:
kripnos@kripnos-laptop:~$ time python ou.py
87322

real	0m1.314s
user	0m1.140s
sys	0m0.004s
kripnos@kripnos-laptop:~$


R4z0r_Cr4$H ha detto:
chi testa il mio su linux?credo che ora riesco a scendere sotto i 219 millisecondi!:D

IO :poverannuj:

ecco razor:
ma mi fa errore forse ho sbagliato qualcosa.... ti allego il file *.java
Codice:
kripnos@kripnos-laptop:~$ time java test.class
Exception in thread "main" java.lang.NoClassDefFoundError: test/class
Caused by: java.lang.ClassNotFoundException: test.class
	at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Could not find the main class: test.class. Program will exit.

real	0m0.152s
user	0m0.052s
sys	0m0.008s
kripnos@kripnos-laptop:~$
sulla mia macchina: celeron 1.6 ghz, 1 gb ram , intel x3100 , ubuntu 8.10
 
dovevi inserirlo nella classe :asd:...
Codice:
public class CercaNum{
public void cercaNumero(){
        for(int i=12345;i<98765;i++){
            String is=""+i;
            int v0=Integer.parseInt(""+is.charAt(0));
            int v1=Integer.parseInt(""+is.charAt(1));
            int v2=Integer.parseInt(""+is.charAt(2));
            int v3=Integer.parseInt(""+is.charAt(3));
            int v4=Integer.parseInt(""+is.charAt(4));
            if(v0+v1==15){
                if(v4==2 | v4==6){
                    if(v0+v4==10){
                        if(v0+v1+v2+v3+v4==22){
                          if(v0!=v1 && v0!=v2 && v0!=v3 && v0!=v4 && v1!=v2 && v1!=v3 && v1!=v4 && v2!=v3 && v2!=v4 && v3!=v4){
                            if(v0!=0 && v1!=0 && v2!=0 && v3!=0 && v4!=0){
                                if(v3%2==0){
                                    System.out.println(i);}}}}}}}}}}
 
Per chi lo crea in c/c++ sotto windows se lo compila e lo esegue con code::blocks viene visualizzato il tempo di esecuzione
 
R4z0r_Cr4$H ha detto:
dovevi inserirlo nella classe :asd:...
Codice:
public class CercaNum{
public void cercaNumero(){
        for(int i=12345;i<98765;i++){
            String is=""+i;
            int v0=Integer.parseInt(""+is.charAt(0));
            int v1=Integer.parseInt(""+is.charAt(1));
            int v2=Integer.parseInt(""+is.charAt(2));
            int v3=Integer.parseInt(""+is.charAt(3));
            int v4=Integer.parseInt(""+is.charAt(4));
            if(v0+v1==15){
                if(v4==2 | v4==6){
                    if(v0+v4==10){
                        if(v0+v1+v2+v3+v4==22){
                          if(v0!=v1 && v0!=v2 && v0!=v3 && v0!=v4 && v1!=v2 && v1!=v3 && v1!=v4 && v2!=v3 && v2!=v4 && v3!=v4){
                            if(v0!=0 && v1!=0 && v2!=0 && v3!=0 && v4!=0){
                                if(v3%2==0){
                                    System.out.println(i);}}}}}}}}}}


Codice:
kripnos@kripnos-laptop:~/java$ time java CercaNum
87142

real	0m0.340s
user	0m0.264s
sys	0m0.028s
kripnos@kripnos-laptop:~/java$
 
vabbè...alla fine gli script deve testarli sono uno,perchè la velocità varia da pc a pc...a me fa 219 millisecondi su windows xD!
 
R4z0r_Cr4$H ha detto:
vabbè...alla fine gli script deve testarli sono uno,perchè la velocità varia da pc a pc...a me fa 219 millisecondi su windows xD!
[ot] io ho una cariola con la cpu :asd::asd::asd: [/ot]
 
Oromis92 ha detto:
Codice:
oromis92@localhost:~/Scrivania$ time python b.py
87322

real    0m1.020s
user    0m1.000s
sys     0m0.008s

ma il risultato è sbagliato...

in effetti ho notato che è errato, devo un attimo controllare lo script, forse ho dimenticato qualcosa

EDIT: avevo messo un continue al contrario, ora ho messo il pass-else-continue che serviva, tester please :asd:

Codice:
for i in range(10000,100000):
    x=str(i)
    if x.find("0") is not -1:
        continue
    if x[0] is not x[1] and x[1] is not x[2] and x[2] is not x[3] and x[3] is not x[4] \
    and x[4] is not x[1] and x[4] is not x[2] and x[4] is not x[0] and x[3] is not x[1] \
    and x[3] is not x[0] and x[2] is not x[0]:
        pass
    else:
        continue
    if ((int(x[3])%2) is 0) and ((x[-1] is "2") or (x[-1] is "6")) and ((int(x[0])+int(x[1])) is 15) and ((int(x[0])+int(x[-1])) is 10) \
    and (sum([int(f) for f in x]) is 22):
        print x

reedit: ho modficato lo script inserendo una funzione già fatta :asd: ora dovrebbe essere + veloce
 
Malex ha detto:
Oromis92 ha detto:
Codice:
oromis92@localhost:~/Scrivania$ time python b.py
87322

real    0m1.020s
user    0m1.000s
sys     0m0.008s

ma il risultato è sbagliato...

in effetti ho notato che è errato, devo un attimo controllare lo script, forse ho dimenticato qualcosa

EDIT: avevo messo un continue al contrario, ora ho messo il pass-else-continue che serviva, tester please :asd:

Codice:
for i in range(10000,100000):
    x=str(i)
    if x.find("0") is not -1:
        continue
    if x[0] is not x[1] and x[1] is not x[2] and x[2] is not x[3] and x[3] is not x[4] \
    and x[4] is not x[1] and x[4] is not x[2] and x[4] is not x[0] and x[3] is not x[1] \
    and x[3] is not x[0] and x[2] is not x[0]:
        pass
    else:
        continue
    if ((int(x[3])%2) is 0) and ((x[-1] is "2") or (x[-1] is "6")) and ((int(x[0])+int(x[1])) is 15) and ((int(x[0])+int(x[-1])) is 10) \
    and (sum([int(f) for f in x]) is 22):
        print x

reedit: ho modficato lo script inserendo una funzione già fatta :asd: ora dovrebbe essere + veloce
Codice:
kripnos@kripnos-laptop:~$ time python ou.py
87142

real	0m0.637s
user	0m0.400s
sys	0m0.016s
kripnos@kripnos-laptop:~$
[ot]
faccio prima a fare cosi.. :asd:
Codice:
print "87142"
 
@Malex

Codice:
oromis92@localhost:~/Scrivania$ time python b.py
87142

real    0m0.367s
user    0m0.356s
sys     0m0.008s

nettamente migliorato ;)
 
[Python]
Codice:
#!/usr/bin/python
for i in xrange(10000, 99999):
  i=str(i)
  i.split()
  if int(i[0])+int(i[1]) == 15:
    if 	int(i[4]) == 2 or int(i[4]) == 6:
      if int(i[0])+int(i[4]) == 10:
        if int(i[0])+int(i[1])+int(i[2])+int(i[3])+int(i[4]) == 22:
          if int(i[0]) != int(i[1]) and int(i[0]) != int(i[2]) and int(i[0]) != int(i[3]) and int(i[0]) != int(i[4]) and int(i[1]) != int(i[2]) and int(i[1]) != int(i[3]) and int(i[1]) != int(i[4]) and int(i[2]) != int(i[3]) and int(i[2]) != int(i[4]) and int(i[3]) != int(i[4]):
            if int(i[0]) != 0 and int(i[1]) != 0 and int(i[2]) != 0 and int(i[3]) != 0 and int(i[4]) != 0:
              if int(i[3]) % 2 == 0:
                  print i
 
[ot]
che ne dite se io faccio semplicemente un
print 87142
secondo l'esercizio è giusto
perchè non c'è scritto che vanno fatti al pc i calcoli
:asd:
[/ot]
 
[ot]
@r4z0r : scherzavo ci metto un System.out.println("87142");
:asd: no dai il fatto che non so in che linguaggio postare.....
pascal!!!!!!!!!!!!!!!!!!!!!!!!1
[/ot]
 
Stato
Discussione chiusa ad ulteriori risposte.