Numeri felici

« Older   Newer »
 
  Share  
.
  1. Ace.
     
    .

    User deleted


    Ho cominciato a fare anche c++, ecco cosa ho fatto:
    CODICE
    #include<iostream>
    using namespace std;
    main(){
    int somma=0,num,x,y;
    cin>>num;
    x=y=num;
    while (num>1){
         while (x>1){
               somma=somma+((x%2)*(x%2));
               x=x/10;
               }
         if (somma==1){
                      break;
                      }
         num=x=somma;
         }
         if (somma==1){
                       cout<<y<<" e' un numero felice"<<endl;
                       }
         else{
              cout<<y<<" non e' un numero felice"<<endl;
              }
    system("Pause");
    return 0;
    }
     
    Top
    .
  2. lumo
     
    .

    User deleted


    Ace, se il numero non è felice il ciclo come fa ad interrompersi?
     
    Top
    .
  3. Ace.
     
    .

    User deleted


    CITAZIONE (lumo @ 7/11/2009, 13:36)
    Ace, se il numero non è felice il ciclo come fa ad interrompersi?

    Non so :asd: io ho provato a farlo come potevo :P
     
    Top
    .
  4. Tankado©
     
    .

    User deleted


    Scusate se scrivo su una discussione vecchia.
    CODICE
    def numero_felice(x,i):
    i = input("Submit a number: ")
    x = i**2
    while (i > 1) :
           x = [(x / 10)** 2] + [( x % 10) ** 2]
           if ( x == 1 ):
                   return true
           else:
                   return false
     
    Top
    .
  5. x-reynik-x
     
    .

    User deleted


    Così come l'hai postato non funzionerà mai...

    EDIT: Intanto posto la mia soluzione, che non l'avevo fatto questo esercizio:
    SPOILER (click to view)
    CODICE
    def felice(n):
       blacklist = [4, 16, 37, 58, 89, 145, 42, 20]
       if n in blacklist or n == 0:
           return False
       if n == 1:
           return True
       while True:
           n2 = sum(int(i) ** 2 for i in str(n))
           if n2 in blacklist:
               return False
           if n2 == 1:
               return True
           n = n2


    Edited by x-reynik-x - 15/12/2010, 20:04
     
    Top
    .
  6. Tankado©
     
    .

    User deleted


    Perchè?
     
    Top
    .
  7. x-reynik-x
     
    .

    User deleted


    Perché è indentato male.
     
    Top
    .
  8. Tankado©
     
    .

    User deleted


    Puoi spiegarmi dove?
     
    Top
    .
  9.  
    .
    Avatar

    Senior Member

    Group
    Staff
    Posts
    10,796

    Status
    Anonymous
    Non hai indentato le variabili x,i ed il ciclo.
     
    Top
    .
  10. Tankado©
     
    .

    User deleted


    CITAZIONE (RootkitNeo @ 15/12/2010, 22:25) 
    Non hai indentato le variabili x,i ed il ciclo.

    Continuo a non capire...potresti farmi vedere dove sono gli errori?
     
    Top
    .
  11. x-reynik-x
     
    .

    User deleted


    Ma la stai leggendo una guida? È una cosa base l'indentazione...
    È la stessa cosa che hai fatto con il ciclo while, lo fai anche con la funzione:
    CODE
    def numero_felice(x,i):
       i = input("Submit a number: ")
       x = i**2
       while (i > 1) :
          x = [(x / 10)** 2] + [( x % 10) ** 2]
          if ( x == 1 ):
              return true
          else:
              return false


    2°: in Python vero e falso sono maiuscoli, Python è Case-Sensitive;
    3°: a che ti serve mettere come argomenti della funzione se poi li rimpiazzi con altri valori?
    4°: le parentesi quadre indicano le liste, per l'aritmetica usi solo tonde
    5°: e se il numero iniziale ha 3 o più cifre?
    6°: anche con tutte queste modifiche non sono sicuro che funzioni, provalo! ;)
     
    Top
    .
  12. x-reynik-x
     
    .

    User deleted


    Ah ancora una cosa: se tu metti quel else dopo l'if esci subito dal ciclo e non puoi continuare con il numero ottentuto.
    Es. 7
    7^2 = 49
    49 != 1 allora esci e ritorni falso. Invece 7 è un numero felice, perché se continui:
    4^2 + 9^2 = 97
    9^2 + 7^2 = 130
    1^2 + 3^2 + 02 = 10
    1^2 + 0^2 = 1
     
    Top
    .
  13. kulky
     
    .

    User deleted


    #include<stdio.h>
    #include<math.h>


    int restituiscicifre(int *num)
    {int res;
    res=(*num) % 10;
    *num =( (*num) / 10);
    return res;
    }

    int sommacifre(int num)
    { int somma,temp;
    somma=0;
    while (num)
    { temp=restituiscicifre(&num);
    somma=somma+temp*temp;
    }
    }

    int isfelice(int num)
    { int somma;
    somma=0;
    while (num!=1 && num!=4)
    {
    num=sommacifre(num);
    }
    if (num==1)
    return 1;
    else
    return 0;
    }

    void main()
    { int num,temp,somma,memoria;
    for(memoria=2;memoria<1000;memoria++)
    { somma=0;
    num=memoria;
    if(isfelice(memoria) )
    {
    printf("felice %d \n",memoria);
    }
    }
    getchar();
    }

    CODICE
    #include<stdio.h>
    #include<math.h>


    int restituiscicifre(int *num)
    {int res;
     res=(*num) % 10;
     *num =( (*num) / 10);
     return res;  
    }

    int sommacifre(int num)
     { int somma,temp;  
        somma=0;
             while (num)
                {  temp=restituiscicifre(&num);
                   somma=somma+temp*temp;
            }
      }

    int isfelice(int num)
    {  int somma;
       somma=0;
           while (num!=1 && num!=4)
                 {
                             num=sommacifre(num);
             }
       if (num==1)
                   return 1;
           else
                   return 0;
    }

    void main()
           { int num,temp,somma,memoria;
        for(memoria=2;memoria<1000;memoria++)
             {  somma=0;
                num=memoria;
                    if(isfelice(memoria) )
                    {
                                    printf("felice %d \n",memoria);
                        }
              }
       getchar();
    }
     
    Top
    .
  14. kulky
     
    .

    User deleted


    Ho modificato il codice:
    CODICE
    #include<stdio.h>
    #include<math.h>


    int restituiscicifre(int *num)
    {int res;
     res=(*num) % 10;
     *num =( (*num) / 10);
     return res;  
    }

    int sommacifre(int num)
     { int somma,temp;  
        somma=0;
             while (num)
                {  temp=restituiscicifre(&num);
                   somma=somma+temp*temp;
            }
      }

    int isfelice(int num)
    {          while (num!=1 && num!=4)
                 {
                             num=sommacifre(num);
             }
       if (num==1)
                   return 1;
           else
                   return 0;
    }

    void main()
    {
            int memoria;
        for(memoria=2;memoria<1000;memoria++)
             {  
                    if(isfelice(memoria) )
                    {
                                    printf("felice %d \n",memoria);
                        }
              }
       getchar();
    }
     
    Top
    .
  15. Guglielmoqwerty
     
    .

    User deleted


    Questa invece è la mia soluzione :)
    Soluzione

    Ne ho approfittato per utilizzare per la prima volta ArrayList

    Secondo voi come si potrebbe migliorare? Mi sembra ci siano troppi cicli....
     
    Top
    .
32 replies since 5/11/2009, 17:09   709 views
  Share  
.