Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mathematics reversing issue...

    Code:
    date = new Date();
      year = date.getYear();
      add  = year;
      for (i = 1; i<= year;i++)
      {
        add+=year+i;
      }
      if (add == 395425559298) 
      {
    window.location.href=year+".php";
    From what I can tell, add+=year is the same as add=add+year+i
    Given the i<= year, i++ statement, I assume that add and year are the same, and that "i" is equal to add or year +1.

    So should something like this work?
    Code:
    var add = 1;
    var year = 1;
    var i = 2;
    var final;
      for (final = 1; final< 395425559298;final++)
      {
        document.write(add+"<br>")
        document.write(year+"<br>")
        document.write(i+"<br>")
    add++
    year++
    i++
      }
    I would test it, but I am on a slow pc right now. I'm scared that it might crash my computer.

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If what you want to know is if what you wrote is equivalent to the prior code then the answer is no. Will it run, yes, even though it will take a long time. add += year + i as you stated is equiv to add = add + year + i.

    So, in the upper code year = 2006, add = year (2006) and the loop for i goes from 1 to year(2006).

    first iteration of add += year + i will make add = to 2006 + 2006 + 1 which is 4013. second pass through the loop will make add = 4013 + 2006 + 2 which is 6021. Each time add is updated to the new result of the addition.

    In your example you are simply counting from 1 to a huge number by increments of 1.

    BaldEagle

  • #3
    New Coder
    Join Date
    Jan 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script>
    var add = 2006;
    var year = 2006;
    var i = 1;
    var final;
    function addme(){
      if add!=395425559298
      {
      add+=add+year+i
      document.write(add+"<br>")
      }
     else{addme()
    }
    }
    </script>
    I have no idea how to write the script so that it parallels what you're saying.

    thxy
    Last edited by luckygirl; 01-28-2006 at 05:28 PM.

  • #4
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by luckygirl
    Code:
    var add = 2006;
    var year = 2006;
    var i = 1;
    var final;
    function addme()
      if add!=395425559298
      {
      add+=add+year+i
      document.write(add+"<br>")
      }
     else{addme()
    }
    I have no idea how to write the script so that it parallels what you're saying.

    thxy
    You forgot a { after the function. Just letting ya know in case it didn't work and you weren't sure why.

    ^_^

  • #5
    New Coder
    Join Date
    Jan 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is the code correct though? lol

    Thanks for the tip.

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var add = 2006;
    var year = 2006;
    var i = 1;
    var final;
    function addme()
    // add this
    {  
       if (add != 395425559298)
      {
      // change this
      add+=add+year+i 
      // to this
      add += year + i 
      document.write(add+"<br>")
      // add this
      i++  
      }
      // I rearranged this for clarity
      else
      {
       addme()
      }
    }
    BaldEagle

    [edit] using a loop as the original code does is probably more efficient

    [edit] One last thing, i didn't run this out to see if the value actually adds up the 395425559298, but if it doesn't your version will run forever. You can avoid that by changing it to
    if (add >= 395425559298)
    Last edited by BaldEagle; 01-28-2006 at 05:37 PM.

  • #7
    New Coder
    Join Date
    Jan 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script>
    var add = 2006;
    var year = 2006;
    var i = 1;
    var final;
    function addme(){ 
     if add!=395425559298
      {
      add += year + i 
      document.write(add+"<br>")
      i++  
      }
      else
      {
      addme()
      }
    }
    </script>
    
    <body onload=addme()>
    The new code won't work either.

    //Eagle My orignal way of looping I can't figure out how to make it equivilant to the first code posted. The mathematics are difficult for me to reverse.
    Last edited by luckygirl; 01-28-2006 at 05:41 PM.

  • #8
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script>
    var add = 2006;
    var year = 2006;
    var i = 1;
    var final;
    function addme(){ 
     if add!=395425559298
      {
      add += year + i 
      document.write(add+"<br>")
      i++  
      }
      else
      {
      addme()
      }
    }
    </script>
    
    <body onload=addme()>
    my mistake. Move the addMe() to the if side and don't have an else

    Code:
    <script>
    var add = 2006;
    var year = 2006;
    var i = 1;
    var final;
    function addme(){ 
     if (add!=395425559298)
      {
      add += year + i 
      document.write(add+"<br>")
      i++  
      addme()
      }
    }
    </script>
    
    <body onload=addme()>
    BaldEagle

  • #9
    New Coder
    Join Date
    Jan 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Lets start over. The new javascript is not going to bring up anything pertaining to the first javascript I posted.

    Code:
    date = new Date();
      year = date.getYear();
      add  = year;
      for (i = 1; i<= year;i++)
      {
        add+=year+i;
      }
      if (add == 395425559298) 
      {
    window.location.href=year+".php";
    I am looking to create a loop that will solve for year where the final add variable is equal to 395425559298 and add=add+year+i
    Last edited by luckygirl; 01-28-2006 at 05:57 PM.

  • #10
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You will need to change the if (add != ...) to if (add >= ...). I suspected this might be an issue. I come from a different background programming wise in which a stack is maintained to allow the called function to know where to return. I assume javascript works the same way. So if you have a function that calls itsself then the stack just keeps growing. As I stated earlier the looping method is the only real efficient way to do this.

    BaldEagle

  • #11
    New Coder
    Join Date
    Jan 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think that by posted my misinterpretation of your first reply, I threw us off course. My reply before this one should clarify. me hopes*

  • #12
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by luckygirl
    I am looking to create a loop that will solve for year where the final add variable is equal to 395425559298 and add=add+year+i
    I apologize because I did misinperpret what you wanted. Let me see if I got it straight. Do you want to start with a number: 395425559298 and then determine what year it represents based on the reversal of the algorithm from the original code? Is this right?

    BaldEagle

  • #13
    New Coder
    Join Date
    Jan 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes

    hehe

    ps - no apology necessary. I'm new with javascript and enjoy the opportunities to learn and help.

  • #14
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var year = 2006;
    var add = 395425559298;
    function findYear()
    {
      for (i = 1; i<= year;i++)
      {
        add -= (year*i) - i;
        if (add <= 0)
        {
          year = i;
          break;
        }
      }
    }
    Not certain this is what you want. Didn't test it either. Also this will not find years in the future as I have started it from current year.

    I have also made it that add does not have to be an exact match. You can modify to your liking.

    Baldeagle

  • #15
    New Coder
    Join Date
    Jan 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks// I'll work on it


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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