Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Mathematics reversing issue...

1. ## Mathematics reversing issue...

Code:
```date = new Date();
year = date.getYear();
for (i = 1; i<= 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(year+"<br>")
document.write(i+"<br>")
year++
i++
}```
I would test it, but I am on a slow pc right now. I'm scared that it might crash my computer.

• 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

• Code:
```<script>
var add = 2006;
var year = 2006;
var i = 1;
var final;
{
}
}
}
</script>```
I have no idea how to write the script so that it parallels what you're saying.

thxy

• Originally Posted by luckygirl
Code:
```var add = 2006;
var year = 2006;
var i = 1;
var final;
{
}
}```
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.

^_^

• Is the code correct though? lol

Thanks for the tip.

• Code:
```var add = 2006;
var year = 2006;
var i = 1;
var final;
{
if (add != 395425559298)
{
// change this
// to this
add += year + i
i++
}
// I rearranged this for clarity
else
{
}
}```
BaldEagle

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

 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)

• Code:
```<script>
var add = 2006;
var year = 2006;
var i = 1;
var final;
{
add += year + i
i++
}
else
{
}
}
</script>

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.

• Code:
```<script>
var add = 2006;
var year = 2006;
var i = 1;
var final;
{
add += year + i
i++
}
else
{
}
}
</script>

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;
{
add += year + i
i++
}
}
</script>

BaldEagle

• 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();
for (i = 1; i<= 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

• 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

• 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*

• 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

• Yes

hehe

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

• 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

• Thanks// I'll work on it

•
Page 1 of 2 12 Last

#### Posting Permissions

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