Java Ystävyysluvut

forever-paranoid

Yritin tehdä tälläistä ohjelmaa joka etsii ystävyys lukuja, mutta vastaan tuli aika iso ongelma. Tässä ohjelma niille jotka tahtovat ajaa sen, ja mahdollisesti auttaa... Ongelman huomaa, jos ajaa ohjelman ja tietää mitä ovat ystävyysluvut.

public class YstavyysLuvut {
   public static void main (String[] args) {
      
      int raja=10000;
      int luku1=0;
      int luku2=0;
      int jakaja=0;
      int summa=0;
      int summa2=0;
      boolean ystävyysluvut=false;
      
      do {
         
         luku1 ;
         luku2=0;
         
         do {
            
            luku2 ;
            jakaja=0;
            summa=0;
               
            do {
               
               jakaja ;
               
               if (luku1%jakaja==0) {
                  
                  summa=summa jakaja;
               }
            } while (jakaja

6

788

    Vastaukset 6

    Anonyymi (Kirjaudu / Rekisteröidy)
    5000
    • forever-paranoid

      En tahtonut aloittaa uutta keskustelua, joten päätin kysyä tässä että on kenelläkään muulla ollut ongelmia saada laajalle levinnyt RandomMosaicWalk toimimaan? Tässä nyt vaikka kaksi versiota siitäkin. En jaksanut poistaa niistä tekijän turhia kommentteja.

      /**
      * This program opens a window full of randomly colored squares. A "disturbance"
      * moves randomly around in the window, randomly changing the color of each
      * square that it visits. The program runs until the user closes the window.
      */

      public class RandomMosaicWalk {

      static int currentRow; // Row currently containing the disturbance.
      static int currentColumn; // Column currently containing disturbance.

      /**
      * The main program creates the window, fills it with random colors,
      * and then moves the disturbakcs in a random wals around the window
      * as long as the window is open.
      */
      public static void main(String[] args) {
      Mosaic.open(10,20,10,10);
      fillWithRandomColors();
      currentRow = 5; // start at center of window
      currentColumn = 10;
      while (Mosaic.isOpen()) {
      changeToRandomColor(currentRow, currentColumn);
      randomMove();
      Mosaic.delay(20);
      }
      } // end main

      /**
      * Fills the window with randomly colored squares.
      * Precondition: The mosaic window is open.
      * Postcondition: Each square has been set to a random color.
      */
      static void fillWithRandomColors() {
      for (int row=0; row < 10; row ) {
      for (int column=0; column < 20; column ) {
      changeToRandomColor(row, column);
      }
      }
      } // end fillWithRandomColors

      /**
      * Changes one square to a new randomly selected color.
      * Precondition: The specified rowNum and colNum are in the valid range
      * of row and column numbers.
      * Postcondition: The square in the specified row and column has
      * been set to a random color.
      * @param rowNum the row number of the square, counting rows down
      * from 0 at the top
      * @param colNum the column number of the square, counting columns over
      * from 0 at the left
      */
      static void changeToRandomColor(int rowNum, int colNum) {
      int red = (int)(256*Math.random()); // Choose random levels in range
      int green = (int)(256*Math.random()); // 0 to 255 for red, green,
      int blue = (int)(256*Math.random()); // and blue color components.
      Mosaic.setColor(rowNum,colNum,red,green,blue);
      } // end changeToRandomColor

      /**
      * Move the disturbance.
      * Precondition: The global variables currentRow and currentColumn
      * are within the legal range of row and column numbers.
      * Postcondition: currentRow or currentColumn is changed to one of the
      * neighboring positions in the grid -- up, down, left, or
      * right from the current position. If this moves the
      * position outside of the grid, then it is moved to the
      * opposite edge of the grid.
      */
      static void randomMove() {
      int directionNum; // Randomly set to 0, 1, 2, or 3 to choose direction.
      directionNum = (int)(4*Math.random());
      switch (directionNum) {
      case 0: // move up
      currentRow--;
      if (currentRow < 0)
      currentRow = 9;
      break;
      case 1: // move right
      currentColumn ;
      if (currentColumn >= 20)
      currentColumn = 0;
      break;
      case 2: // move down
      currentRow ;
      if (currentRow >= 10)
      currentRow = 0;
      break;
      case 3: // move left
      currentColumn--;
      if (currentColumn < 0)
      currentColumn = 19;
      break;
      }
      } // end randomMove

      } // end class RandomMosaicWalk

      ************************************************

      public class RandomMosaicWalk {

      /*
      This program shows a window full of randomly colored
      squares. A "disturbance" moves randomly around
      in the window, randomly changing the color of
      each square that it visits. The program runs
      until the user closes the window.
      */

      static int currentRow; // row currently containing the disturbance
      static int currentColumn; // column currently containing disturbance

      public static void main(String[] args) {
      // Main program creates the window, fills it with
      // random colors, then moves the disturbance in
      // a random walk around the window.
      Mosaic.open(10,20,10,10);
      fillWithRandomColors();
      currentRow = 5; // start at center of window
      currentColumn = 10;
      while (Mosaic.isOpen()) {
      changeToRandomColor(currentRow, currentColumn);
      randomMove();
      Mosaic.delay(20);
      }
      } // end of main()

      static void fillWithRandomColors() {
      // fill every square, in each row and column,
      // with a random color
      for (int row=0; row < 10; row ) {
      for (int column=0; column < 20; column ) {
      changeToRandomColor(row, column);
      }
      }
      } // end of fillWithRandomColors()

      static void changeToRandomColor(int rowNum, int colNum) {
      // change the square in row number rowNum and
      // column number colNum to a random color.
      int red = (int)(256*Math.random()); // choose random levels in range
      int green = (int)(256*Math.random()); // 0 to 255 for red, green,
      int blue = (int)(256*Math.random()); // and blue color components
      Mosaic.setColor(rowNum,colNum,red,green,blue);
      } // end of changeToRandomColor()

      static void randomMove() {
      // Randomly move the disturbance in one of
      // four possible directions: up, down, left, or right;
      // if this moves the disturbance outside the window,
      // then move it to the opposite edge of the window.
      int directionNum; // Randomly set to 0, 1, 2, or 3 to choose direction.
      directionNum = (int)(4*Math.random());
      switch (directionNum) {
      case 0: // move up
      currentRow--;
      if (currentRow < 0)
      currentRow = 9;
      break;
      case 1: // move right
      currentColumn ;
      if (currentColumn >= 20)
      currentColumn = 0;
      break;
      case 2: // move down
      currentRow ;
      if (currentRow >= 10)
      currentRow = 0;
      break;
      case 3:
      currentColumn--;
      if (currentColumn < 0)
      currentColumn = 19;
      break;
      }
      } // end of randomMove()

      } // end of class RandomMosaicWalk

    • forever-paranoid

      Nyt on koodissa jokin pahempi virhe. Muutin vahingolliset if-else tai merkinnät ja merkinnöiksi ja nyt sen sijaan, että kaikki luvut 1-10000 olisivat 10000:n ystävyys lukuja ohjelma eliminoi kaikki luvut, vaikka 1-10000 välillä on 2 tai 3 ystävyyslukuparia. No, tässä uusi ohjelma vaikka muutokset ovat pienet.

      public class YstavyysLuvut {
         public static void main (String[] args) {
            
            int raja=10000;
            int luku1=0;
            int luku2=0;
            int jakaja=0;
            int summa=0;
            int summa2=0;
            boolean ystävyysluvut=false;
            
            do {
               
               luku1 ;
               luku2=0;
               
               do {
                  
                  luku2 ;
                  jakaja=0;
                  summa=0;
                     
                  do {
                     
                     jakaja ;
                     
                     if (luku1%jakaja==0) {
                        
                        summa=summa jakaja;
                     }
                  } while (jakaja

    • pjy

      En tiedä mitä meinaat tarkoittaa ystävyysluvuilla, mutta softa toimii niin kuin se on tehtykkin. Eli siinä on hiton motonta do while lausetta ja ulommaisessa kasvatetaan luku1:stä yhdellä ja sisimmäisessä luku2:sta 1:llä niin kauan, että se on 1000 ja ulommaisessa loopin lopussa todetaan että luku1 ystäväluku luku2:lle eikä tätä ystavaluku muuttujaa koskaan alusteta takaisin false:ksi joten se on aina true.

      Ei millään pahalla, mutta pieni määrä koodia näyttää tosi sekavalta ja tehottomalta (ainakin mun macbookissa).

      • forever-paranoid

        Ystävyys luku pari on sellainen, että luvukujen joilla ensimmäisen luvun saa jaettua tasan summa on toinen luku ja toisinpäin.


    • heebo

      Kannattaisi varmaan jakaa ongelma pienempiin osiin. Kirjoita vaikka metodi

      private static int jakajienSumma(int n),

      jonka avulla saat tehtyä lukujen ystävyyden selvittävän metodin:

      private static boolean onYstavyysluvut(int a, int b) {
      return b*jakajienSumma(a) == a*jakajienSumma(b);
      }

      Pääohjelmassa teet sitten sisäkkäiset silmukat a=0...10000 ja b=a 1...10000 jne.

    Ketjusta on poistettu 1 sääntöjenvastaista viestiä.

    Luetuimmat keskustelut

    1. Nyt se syksy on sitten alkanut

      Esimerkiksi tänään sataa aamusta iltaan, ja nytkin.
      Maailman menoa
      54
      1017
    2. Minä en ole koskaan

      Rakastanut ketään toista yhtä paljon kuin sinua
      Ikävä
      98
      1004
    3. Tiesitkö? Jorma Uotisen EX-rakas on Helena Lindgren - Nämä ovat välit nyt: "Me ollaan..."

      Jorma Uotinen ja Helena Lindgren olivat avoliitossa v. 1982-1999. Pariskunta oli aikansa näyttävä julkkispari, missä i
      Kotimaiset julkkisjuorut
      21
      852
    4. Vähän pitää

      Kiusia ja näyttää
      Ikävä
      135
      802
    5. Anteeksi J-mies

      Anteeksi että myötävaikutin siihen että rakastuit minuun, jota et voi koskaan saada. Viimeksi kun näimme ja katselimme t
      Ikävä
      121
      786
    6. Olet nainen ollut vaikein rasti

      Eniten olet tunteitani myllännyt ja niitä kuluttanut, ja tuottanut. 🤔
      Ikävä
      55
      774
    7. Kenen viisautta on ollut laittaa tie väärällä murskeella pilalle

      Kenen viisautta on ollut kun Siimeksen/Kylmänpurontie pantu pilalle kun on ajettu liian karkeaa mursketta. Rengasliikke
      Hyrynsalmi
      25
      659
    8. Taidat olla

      Luovuttanut jo minun suhteeni osalta?
      Ikävä
      45
      656
    9. Riittääkö mahkut

      Kaivattuusi 🤭.
      Ikävä
      50
      638
    10. Minä muistan

      Minä muistan sinut nauravana. Sellaisena, joka sai tavallisenkin hetken tuntumaan vähän kevyemmältä, kuin huoneeseen ol
      Ikävä
      45
      610
    11. Mä voisin rakastua noihin tähti silmiin

      Katseesi on niin lumoava nainen💚.
      Ikävä
      19
      607
    12. Jos tullaan toisiamme vastaan, niin

      pitäiskö pysähtyä ja onnitella?
      Ikävä
      46
      578
    13. Voisiko vielä välillemme jotain kehittyä

      Vai heihei ja ei nähdä enää.
      Ikävä
      35
      570
    14. Kuhmolainen opettaja tuomittu vainoamisesta

      https://yle.fi/a/74-20248184 ”Kuusikymppinen kuhmolaisnainen piinasi naapureitaan lähes kahdeksan vuoden ajan. Nainen
      Kuhmo
      13
      570
    15. Siinä kävi mies nyt niin että tämä ämmä muuttaa ulkomaille

      Vuonojen maa kutsuu, joten ei hätää. Et kuule minusta enää. 😂 Lycka till! Ämmä riittää.
      Ikävä
      72
      558
    16. Koska Martina viettää aikaansa lapsiensa kanssa

      Äiti vaan juoksee miehen perässä Tallinnassa, Lontoossa, Turussa. Kyllä oli kätilö oikeassa, kun sanoi, että ei olisi ka
      Kotimaiset julkkisjuorut
      141
      555
    17. Ähtäri on taas TV: n ja radion ykkösuutinen

      Uusi podcast on julkaistu ja on nyt kuunneltavissa. Mot on tehnyt hyvää työtä! Mikko Savola ja kepu ei ota mitään vast
      Ähtäri
      47
      548
    18. Mitä muuta on olemassa kuin lisääntyminen

      Jos mies ei pääse lisääntymään niin mikä hänen osansa elämässä on? mitä jää jäljelle? Hän on vain muiden palvelija kok
      Sinkut
      120
      536
    19. Tilanteenne

      Onko tilanteenne tällä hetkellä hyvä?
      Ikävä
      47
      515
    20. Pahkalassa tapahtuu

      Mitäs on tapahtunut yöllä pahkalan kerrostaloilla.oli poliisia ja ambulanssia taas vaihteeksi
      Parkano
      9
      513
    Aihe