hisoka Posted February 28, 2015 Share Posted February 28, 2015 I write this little piece of code to get the divisors of 4 until 40000 (can be trillion and zillion ) if you have a very powerful computer . You can get the divisors of any number you want all you have to do is change the value of 4 to the number you want I said may be it can help someone who is looking for divisors :blush: <script>var tmp = 0;for(i=0 ; i<100 ; i++){for(j=0 ; j<100 ; j++){document.write("<br />");document.write(tmp+=4);}}</script> this gives the divisors of 4 to 40000 4812162024283236404448525660646872768084889296100104108112116120124128132136140144148152156160164168172176180184188192196200204208212216220224228232236240244248252256260264268272276280284288292296300304308312316 .... 40000 Link to comment Share on other sites More sharing options...
Ingolme Posted February 28, 2015 Share Posted February 28, 2015 You could simplify that to: for(var i = 0; i < 10000; i++) { document.write(i*4); document.write("<br>");} 1 Link to comment Share on other sites More sharing options...
hisoka Posted March 1, 2015 Author Share Posted March 1, 2015 Thank you very much I wanted to train myself on loop inside another loop like for example this little code that gives the same result of the previous code <script>var tmp = 0;for(i=0 ; i<4 ; i++){for(j=0 ; j<4 ; j++){for(k=0 ; k<4 ; k++){document.write("<br />");document.write(tmp+=4);}}}</script> :) Link to comment Share on other sites More sharing options...
Ingolme Posted March 1, 2015 Share Posted March 1, 2015 It does, but perhaps you should find a good reason to have nested loops. For example when working with matrices. Let's say we have this structure: var matrix = [ [1, 1, 0, 0], [1, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]; Try using loops to figure out if it is symmetrical along the diagonal. You will need to use a loop inside a loop for that. 1 Link to comment Share on other sites More sharing options...
hisoka Posted March 2, 2015 Author Share Posted March 2, 2015 (edited) var matrix = [1,1,0,0], [1,2,0,0], [0,0,3,0], [0,0,0,4], ];for(i=0; i<matrix.length ; i++){for(j=0 ; j<1 ; j++){alert((matrix[i+j]==matrix ? "matrix is symetric along the diagonal" :"matrix is not symetric along the diagonal"));}} Edited March 2, 2015 by hisoka Link to comment Share on other sites More sharing options...
Ingolme Posted March 2, 2015 Share Posted March 2, 2015 Can you explain what your code does to somebody who understands math but not Javascript? 1 Link to comment Share on other sites More sharing options...
hisoka Posted March 2, 2015 Author Share Posted March 2, 2015 var matrix = [1,1,0,0], [1,2,0,0], [0,0,3,0], [0,0,0,4], ]; this creates an array named matrix (a parent array) and assigns it 4 arrays as elements ( 4 children) the for(i=0; i<matrix.length ; i++) loops through the array parents matrix the second for loop which is inside the for(i=0; i<matrix.length ; i++) loops through the parent array ( matrix ) vertically ( from top left in a descending order to the bottom right . Why ? because according to wikepedia , a symmetric matrix is a square matrix that is equal to its transpose . The transpose of a matrix A is another matrix AT (also written A′, Atr, tA or At) created by any one of the following equivalent actions: reflect A over its main diagonal (which runs from top-left to bottom-right) to obtain AT write the rows of A as the columns of AT write the columns of A as the rows of AT for example if we have [1 2 3] [1 4 7] [4 5 6] = its transpose is [2 5 8] [7 8 9] [3 6 9] from that comes the idea of looping from the top left to the right bottom which is done through the second for loop : in the for(j=0 ; j<1 ; j++) the j<1 is used so that the loop will loop only one time so that we do not get unecessary numbers and to avoid errors the matrix[i+j] is to loop the array parent matrix from top left to the bottom right this alert((matrix[i+j]==matrix ? "matrix is symetric along the diagonal" :"matrix is not symetric along the diagonal")); says : after the looping ends compare the result you got with the matrix parent . if they are equal ( the result and the matrix , then the array matrix is symetric along the diagonal and a box popup in which it is stated "matrix is symetric along the diagonal" otherwise a box popup in which is written "matrix is not symetric along the diagonal "Can you explain what your code does to somebody who understands math but not Javascript?" Why you asked me to explain it . Is my result wrong or am I missing something in my code or is it wrong ? and if yes why and what is the correction step by step? or by asking me to explain it do you doubt that I understand what I am writing ( am i crazy so that I write a code I cannot understand !!!! as if I write a sentence that I could not understand!!)? Link to comment Share on other sites More sharing options...
Ingolme Posted March 2, 2015 Share Posted March 2, 2015 It doesn't work, you can test that by feeding it a few different matrices, symmetrical and not, to see how it reacts. The most basic problem is that I only should see one message, but I'm getting as many messages as there are rows in the matrix. Your code also makes the mistake of always telling me that the matrix is symmetrical even when it isn't. I suggest you keep trying until you finally have a solution that works when you test it with one symmetrical matrix and one asymmetrical matrix. Learning to solve problems is the most important part of programming. Link to comment Share on other sites More sharing options...
hisoka Posted March 2, 2015 Author Share Posted March 2, 2015 (edited) <script>var matrix= [ [1, 1, 0, 0], [1, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4],];var matrix1= [ [1, 1, 0, 0], [1, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4],];for(i=0 ; i<1; i++){for(j=0 ; j<1 ; j++){alert((matrix.join()==matrix1.join() ? "matrix is symetric along the diagonal" :"matrix is not symetric along the diagonal"));}}</script> could you please test it and tell me . I think that you will get matrix is symetric if it is and matrix not symetric if it is not change the order , make the size bigger , change the value and you will get only the true result Edited March 2, 2015 by hisoka Link to comment Share on other sites More sharing options...
Ingolme Posted March 3, 2015 Share Posted March 3, 2015 Try feeding this data to your program: var matrix= [ [1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 0, 1],];var matrix1= [ [1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 0, 1],]; What procedure are you following to determine whether a matrix is symmetrical or not? Link to comment Share on other sites More sharing options...
hisoka Posted March 4, 2015 Author Share Posted March 4, 2015 I am working on it . Once the code is ready I will put it and I will appreciate it and be happy when you evaluate it as always :) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now