Git Product home page Git Product logo

generate-rational's Introduction

My algorithm to generate rational is:

Method 0:

   valid (int n , int d){
     if( n and d are co-prime) then return true;
     return false;
   }
   
   
   gcd(int n , int d){
      if( n and d are co-prime) then return (n , d);
      else return gcd (n + 1 , d - 1);
   }
   
   next ( int n , int d) { // will give the next valid rational
      if( n % 2 == d % 2){ // n = numerator , d = denominator
        n++;
        if(d > 1) d--;
        if ( (n , d) is valid rational) then return (n, d);
        else return gcd (n , d);
      }
      else{
        if(n > 1) n--;
        d++;
        if ((n , d) is valid rational) then return (n, d);
        else return gcd (n , d);
      }
   }

// The only problem is, it takes too much time when i want to take first 15 element from the stream. Below 10 it does very fast.

// The fastest method Method1:

   next ( int n , int d) {
      if( n == 1) then return (d + 1 , 1);
      else return (pred n , suc d);
   }

//slower than mathod1 but faster than method0 Method2:

   next ( int n , int d) { // will give the next valid rational
      if( n % 2 == d % 2){ // n = numerator , d = denominator
        n++;
        if(d > 1) d--;
        return (n , d);
      }
      else{
        if(n > 1) n--;
        d++;
        return (n , d);
      }
   }

Note: we can see than method1 and method2 will generate all rational unique + non-unique. Now we have to process further for picking the valid rational.

generate-rational's People

Contributors

ajayv1 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.