www.webdeveloper.com
Results 1 to 7 of 7

Thread: finding the period in decimals, try this out "thinkers" ;)

  1. #1
    Join Date
    Apr 2014
    Posts
    5

    Thumbs up finding the period in decimals, try this out "thinkers" ;)

    what kind of code will you write for javascript to let it find the period in decimal? for example in 0.05768787878787.... it should give you 87 and for 3.854523523523523.... it should give 523 good luck and have fun

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,033
    I don't understand this at all... is this some sort of coding challenge or game?

    Also, you keep saying "the period in the decimals" but it looks like you are talking about repeated numbers in a decimal...

    In any case, a simple regular expression can capture these patterns. Something like
    Code:
    (\d+)\1{3,}
    Seems to work, but I haven't really messed around with it enough.

    And for reference, this can be thrown into a simple javascript match like so
    Code:
    var $a = "0.05768787878787";
    var $b = "3.854523523523523";
    var $m = /(\d+)\1{3,}/;
    
    var $resultA = $a.match($m);
    var $resultB = $b.match($m);
    console.log($resultA[1]);
    console.log($resultB[1]);

  3. #3
    Join Date
    Apr 2014
    Posts
    5

    Thumbs up

    Quote Originally Posted by Sup3rkirby View Post
    I don't understand this at all... is this some sort of coding challenge or game?

    Also, you keep saying "the period in the decimals" but it looks like you are talking about repeated numbers in a decimal...

    In any case, a simple regular expression can capture these patterns. Something like
    Code:
    (\d+)\1{3,}
    Seems to work, but I haven't really messed around with it enough.

    And for reference, this can be thrown into a simple javascript match like so
    Code:
    var $a = "0.05768787878787";
    var $b = "3.854523523523523";
    var $m = /(\d+)\1{3,}/;
    
    var $resultA = $a.match($m);
    var $resultB = $b.match($m);
    console.log($resultA[1]);
    console.log($resultB[1]);
    First of all a repeated number in a decimal is called a period and please dont be lazy to explain what is (\d+)\1{3,} this, it looks so difficult for me to understand, by the way it is not a game of course, i need to convert repeating decimals into fractions so i am looking for the best way of doing it, also thanks for replying

  4. #4
    Join Date
    Apr 2014
    Posts
    5
    can you tell me what does (\d+)\1{3,} this do? i dont really understand anything about this and this is not a game

  5. #5
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,033
    The best way to explain this is probably to break up each major component of the regular expression.

    \d this searches for a number. Adding the + at the end allows it to search for any 1 or more numbers at a time. Placing the parenthasis around it turns this into a group (to which we can apply more conditions to later).
    \1 Forgive me as I'm not really an expert on (or big fan of) regular expressions. I know this has something to do with finding duplicates. It is applied after a capture/group and essentially checks that there is a duplicate (more or less).
    {3,} This simply checks that the previous conditions (duplicates of any set of numbers) exist 3 or more times. This is really only here to help with precision so it can return the 'root' of the duplicate numbers.


    Also please not that upon further testing there are some specific cases where this will not return your exact result. It will always find duplicate number patterns, but for example applying it to "0.333333333333333" will return "333" rather than "3".

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,276
    Try this code :

    Code:
    var nbr,nbs="7.33333|0.034343434|0.0434343434|0.05768787878787|3.854523523523523|0.142857142857".split('|');
    
    for (i=0;i<nbs.length;i++){var nbr=nbs[i],decimalPart=nbr.substr(nbr.indexOf('.')+1);
        var lng=--decimalPart.length
        var lst=decimalPart.charAt(lng);
        // What is the period ?
        while (lng-- && decimalPart.charAt(lng)!=lst);
        var motif=decimalPart.substr(lng+1)
        var rest=decimalPart,resu;
        while ((resu=rest.replace(new RegExp(motif+'$'),''))!=rest) rest=resu;
        var chr;
        while (rest.charAt(rest.length-1)==motif.charAt(motif.length-1)) {
            motif=motif.substr(motif.length-1)+motif.substr(0,motif.length-1);rest=rest.substr(0,rest.length-1)}
        var E=Math.floor(nbr),n=Math.pow(10,rest.toString().length),m=Math.pow(10,motif.length),P=rest*(m+1)+Number(motif),Q=(m-1)*n;
    
        alert(nbr+' rest:'+rest+' motif:'+motif+'\n This number is a rationnel égal to '+(E?E+' + ':'')+P+' / '+Q)
    }
    But we do not solve : 0. 3433 3433 3433 (we should test all repetitions with 1,2,3,.. characters ?) and we dispose only something like 17 digits.
    It would be better to build Big Integers with strings for further developments...
    Last edited by 007Julien; 04-10-2014 at 10:25 AM.

  7. #7
    Join Date
    Sep 2007
    Posts
    317
    Code:
      
    <script type="text/javascript">
    var num = "0.05768787878787";
    var num = "3.854523523523523";
    var t = num.split('.');
    alert('t = '+t);
    var tam = t[0];
    alert('tam  '+tam);
    var n = t[1];
    alert('n = '+n);
    //var n = "345634563456";
    //var n = "33333333333";
    //var n = "05768787878787";
    //var n = "854523523523523";
    var L = n.length;
    var s = n.slice(L-1);
    alert(s);
    
    var v = n.split(s);
    alert(v.length);
    alert(v);
    
    var devreden = v[v.length-2]+ ""+s;
    alert(devreden);
    
    
    
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles