Pairing unit fractions












2












$begingroup$


(I was suggested to post here from stack overflow so hey guys!)



I have been given a problem where fractions between 1/2 - 1/1000 have to be added to create the longest sequence of all unique unit fractions.



The rules on constructing these fractions:



Let create a set: D , D is only to hold unique unit fractions , sub-fractions can add up to the same fraction for example:



       1/10             
/
1/110 + 1/11 1/35 + 1/14


All sub-fractions can be held within the set as long as they themselves are not the same fractions once we are adding them together it is ok for them to total up to the same root.



The goal:



The fractions have to be added in a way to sum up to exactly 1. Any sub-fractions are not allowed to be over 1000 it is explicitly between 2 and 1000 hence the fractions which make up 1/1000 would not be applicable ( 1/1004 + 1/251000 ).



What currently I found to be the most effective:



Find the two lowest multiples which make-up the current fraction that I am looking at so for e.g 1/6 = A = 3 , B = 2. And now we complete the following equation: C = (A+B)*A , D = (A+B)*B. Now C & D are the sub-fractions which add up to my initial fraction



      1/6
/
1/15 1/10


In code:



public static int provideFactorsSmallest(int n) {
int k = new int[2];

for(int i = 2; i <= n - 1; i++) {
if(n % i == 0) {
k[0] = i;
break;
}
}

for(int i = k[0] + 1; i <= n - 1 && k[0] != 0; i++) {
//System.out.println("I HAVE BEEN ENTERED");
if(k[0] * i == n) {
k[1] = i;
int firstTerm = k[0];
int secondTerm = k[1];
k[0] = (firstTerm + secondTerm) * firstTerm;
k[1] = (firstTerm + secondTerm) * secondTerm;
return k;
}
}
return null;
}


My question:



What would be the most effective way to pair and group the numbers to achieve possible longest fraction sequence?










share|improve this question









New contributor




MKUltra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    2












    $begingroup$


    (I was suggested to post here from stack overflow so hey guys!)



    I have been given a problem where fractions between 1/2 - 1/1000 have to be added to create the longest sequence of all unique unit fractions.



    The rules on constructing these fractions:



    Let create a set: D , D is only to hold unique unit fractions , sub-fractions can add up to the same fraction for example:



           1/10             
    /
    1/110 + 1/11 1/35 + 1/14


    All sub-fractions can be held within the set as long as they themselves are not the same fractions once we are adding them together it is ok for them to total up to the same root.



    The goal:



    The fractions have to be added in a way to sum up to exactly 1. Any sub-fractions are not allowed to be over 1000 it is explicitly between 2 and 1000 hence the fractions which make up 1/1000 would not be applicable ( 1/1004 + 1/251000 ).



    What currently I found to be the most effective:



    Find the two lowest multiples which make-up the current fraction that I am looking at so for e.g 1/6 = A = 3 , B = 2. And now we complete the following equation: C = (A+B)*A , D = (A+B)*B. Now C & D are the sub-fractions which add up to my initial fraction



          1/6
    /
    1/15 1/10


    In code:



    public static int provideFactorsSmallest(int n) {
    int k = new int[2];

    for(int i = 2; i <= n - 1; i++) {
    if(n % i == 0) {
    k[0] = i;
    break;
    }
    }

    for(int i = k[0] + 1; i <= n - 1 && k[0] != 0; i++) {
    //System.out.println("I HAVE BEEN ENTERED");
    if(k[0] * i == n) {
    k[1] = i;
    int firstTerm = k[0];
    int secondTerm = k[1];
    k[0] = (firstTerm + secondTerm) * firstTerm;
    k[1] = (firstTerm + secondTerm) * secondTerm;
    return k;
    }
    }
    return null;
    }


    My question:



    What would be the most effective way to pair and group the numbers to achieve possible longest fraction sequence?










    share|improve this question









    New contributor




    MKUltra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      2












      2








      2





      $begingroup$


      (I was suggested to post here from stack overflow so hey guys!)



      I have been given a problem where fractions between 1/2 - 1/1000 have to be added to create the longest sequence of all unique unit fractions.



      The rules on constructing these fractions:



      Let create a set: D , D is only to hold unique unit fractions , sub-fractions can add up to the same fraction for example:



             1/10             
      /
      1/110 + 1/11 1/35 + 1/14


      All sub-fractions can be held within the set as long as they themselves are not the same fractions once we are adding them together it is ok for them to total up to the same root.



      The goal:



      The fractions have to be added in a way to sum up to exactly 1. Any sub-fractions are not allowed to be over 1000 it is explicitly between 2 and 1000 hence the fractions which make up 1/1000 would not be applicable ( 1/1004 + 1/251000 ).



      What currently I found to be the most effective:



      Find the two lowest multiples which make-up the current fraction that I am looking at so for e.g 1/6 = A = 3 , B = 2. And now we complete the following equation: C = (A+B)*A , D = (A+B)*B. Now C & D are the sub-fractions which add up to my initial fraction



            1/6
      /
      1/15 1/10


      In code:



      public static int provideFactorsSmallest(int n) {
      int k = new int[2];

      for(int i = 2; i <= n - 1; i++) {
      if(n % i == 0) {
      k[0] = i;
      break;
      }
      }

      for(int i = k[0] + 1; i <= n - 1 && k[0] != 0; i++) {
      //System.out.println("I HAVE BEEN ENTERED");
      if(k[0] * i == n) {
      k[1] = i;
      int firstTerm = k[0];
      int secondTerm = k[1];
      k[0] = (firstTerm + secondTerm) * firstTerm;
      k[1] = (firstTerm + secondTerm) * secondTerm;
      return k;
      }
      }
      return null;
      }


      My question:



      What would be the most effective way to pair and group the numbers to achieve possible longest fraction sequence?










      share|improve this question









      New contributor




      MKUltra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      (I was suggested to post here from stack overflow so hey guys!)



      I have been given a problem where fractions between 1/2 - 1/1000 have to be added to create the longest sequence of all unique unit fractions.



      The rules on constructing these fractions:



      Let create a set: D , D is only to hold unique unit fractions , sub-fractions can add up to the same fraction for example:



             1/10             
      /
      1/110 + 1/11 1/35 + 1/14


      All sub-fractions can be held within the set as long as they themselves are not the same fractions once we are adding them together it is ok for them to total up to the same root.



      The goal:



      The fractions have to be added in a way to sum up to exactly 1. Any sub-fractions are not allowed to be over 1000 it is explicitly between 2 and 1000 hence the fractions which make up 1/1000 would not be applicable ( 1/1004 + 1/251000 ).



      What currently I found to be the most effective:



      Find the two lowest multiples which make-up the current fraction that I am looking at so for e.g 1/6 = A = 3 , B = 2. And now we complete the following equation: C = (A+B)*A , D = (A+B)*B. Now C & D are the sub-fractions which add up to my initial fraction



            1/6
      /
      1/15 1/10


      In code:



      public static int provideFactorsSmallest(int n) {
      int k = new int[2];

      for(int i = 2; i <= n - 1; i++) {
      if(n % i == 0) {
      k[0] = i;
      break;
      }
      }

      for(int i = k[0] + 1; i <= n - 1 && k[0] != 0; i++) {
      //System.out.println("I HAVE BEEN ENTERED");
      if(k[0] * i == n) {
      k[1] = i;
      int firstTerm = k[0];
      int secondTerm = k[1];
      k[0] = (firstTerm + secondTerm) * firstTerm;
      k[1] = (firstTerm + secondTerm) * secondTerm;
      return k;
      }
      }
      return null;
      }


      My question:



      What would be the most effective way to pair and group the numbers to achieve possible longest fraction sequence?







      java algorithm






      share|improve this question









      New contributor




      MKUltra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      MKUltra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 7 hours ago









      mdfst13

      17.9k62157




      17.9k62157






      New contributor




      MKUltra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 8 hours ago









      MKUltraMKUltra

      111




      111




      New contributor




      MKUltra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      MKUltra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      MKUltra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          1












          $begingroup$


          1. In first for loop we can stop when i*i>=n. As we need two different divisors, smallest must be less than Math.sqrt(n)

          2. Second for loop looks like simple integer division.


          Whole code is equivalent to this:



              private static int provideFactorsSmallest_v(int n) {
          for (int firstTerm = 2; firstTerm*firstTerm < n; firstTerm++) {
          if (n % firstTerm == 0) {
          int secondTerm = n / firstTerm;
          return new int{
          (firstTerm + secondTerm) * firstTerm,
          (firstTerm + secondTerm) * secondTerm
          };
          }
          }
          return null;
          }







          share|improve this answer








          New contributor




          Mikhail Efimov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$













          • $begingroup$
            wow that return new int that got me spiced up!
            $endgroup$
            – MKUltra
            6 hours ago












          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          MKUltra is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216440%2fpairing-unit-fractions%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1












          $begingroup$


          1. In first for loop we can stop when i*i>=n. As we need two different divisors, smallest must be less than Math.sqrt(n)

          2. Second for loop looks like simple integer division.


          Whole code is equivalent to this:



              private static int provideFactorsSmallest_v(int n) {
          for (int firstTerm = 2; firstTerm*firstTerm < n; firstTerm++) {
          if (n % firstTerm == 0) {
          int secondTerm = n / firstTerm;
          return new int{
          (firstTerm + secondTerm) * firstTerm,
          (firstTerm + secondTerm) * secondTerm
          };
          }
          }
          return null;
          }







          share|improve this answer








          New contributor




          Mikhail Efimov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$













          • $begingroup$
            wow that return new int that got me spiced up!
            $endgroup$
            – MKUltra
            6 hours ago
















          1












          $begingroup$


          1. In first for loop we can stop when i*i>=n. As we need two different divisors, smallest must be less than Math.sqrt(n)

          2. Second for loop looks like simple integer division.


          Whole code is equivalent to this:



              private static int provideFactorsSmallest_v(int n) {
          for (int firstTerm = 2; firstTerm*firstTerm < n; firstTerm++) {
          if (n % firstTerm == 0) {
          int secondTerm = n / firstTerm;
          return new int{
          (firstTerm + secondTerm) * firstTerm,
          (firstTerm + secondTerm) * secondTerm
          };
          }
          }
          return null;
          }







          share|improve this answer








          New contributor




          Mikhail Efimov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$













          • $begingroup$
            wow that return new int that got me spiced up!
            $endgroup$
            – MKUltra
            6 hours ago














          1












          1








          1





          $begingroup$


          1. In first for loop we can stop when i*i>=n. As we need two different divisors, smallest must be less than Math.sqrt(n)

          2. Second for loop looks like simple integer division.


          Whole code is equivalent to this:



              private static int provideFactorsSmallest_v(int n) {
          for (int firstTerm = 2; firstTerm*firstTerm < n; firstTerm++) {
          if (n % firstTerm == 0) {
          int secondTerm = n / firstTerm;
          return new int{
          (firstTerm + secondTerm) * firstTerm,
          (firstTerm + secondTerm) * secondTerm
          };
          }
          }
          return null;
          }







          share|improve this answer








          New contributor




          Mikhail Efimov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$




          1. In first for loop we can stop when i*i>=n. As we need two different divisors, smallest must be less than Math.sqrt(n)

          2. Second for loop looks like simple integer division.


          Whole code is equivalent to this:



              private static int provideFactorsSmallest_v(int n) {
          for (int firstTerm = 2; firstTerm*firstTerm < n; firstTerm++) {
          if (n % firstTerm == 0) {
          int secondTerm = n / firstTerm;
          return new int{
          (firstTerm + secondTerm) * firstTerm,
          (firstTerm + secondTerm) * secondTerm
          };
          }
          }
          return null;
          }








          share|improve this answer








          New contributor




          Mikhail Efimov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Mikhail Efimov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 6 hours ago









          Mikhail EfimovMikhail Efimov

          1111




          1111




          New contributor




          Mikhail Efimov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Mikhail Efimov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Mikhail Efimov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • $begingroup$
            wow that return new int that got me spiced up!
            $endgroup$
            – MKUltra
            6 hours ago


















          • $begingroup$
            wow that return new int that got me spiced up!
            $endgroup$
            – MKUltra
            6 hours ago
















          $begingroup$
          wow that return new int that got me spiced up!
          $endgroup$
          – MKUltra
          6 hours ago




          $begingroup$
          wow that return new int that got me spiced up!
          $endgroup$
          – MKUltra
          6 hours ago










          MKUltra is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          MKUltra is a new contributor. Be nice, and check out our Code of Conduct.













          MKUltra is a new contributor. Be nice, and check out our Code of Conduct.












          MKUltra is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216440%2fpairing-unit-fractions%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?