Pages

Thursday, August 18, 2011

Lagrange's four-square theorem implementation code in JAVA

Lagrange's four-square theorem : any natural number can be represented as the sum of four integer squares

p = a02 + a12 + a22 + a32

where the four numbers a0, a1, a2, a3 are integers. For illustration, 3, 31 and 310 can be represented as the sum of four squares as follows:

3 = 12 + 12 + 12 + 02
31 = 52 + 22 + 12 + 12
310 = 172 + 42 + 22 + 12.

 int n, t1, t2, t;  
           n = 23;//Your number  
           for (int i = (int) Math.sqrt(n / 4); i * i <= n; i++) {  
                t1 = n - i * i;  
                for (int j = (int) Math.sqrt(t1 / 3); j <= i && j * j <= t1; j++) {  
                     t2 = t1 - j * j;  
                     for (int k = (int) Math.sqrt(t2 / 2); k <= j && k * k <= t2; k++) {  
                          t = (int) Math.sqrt(t2 - k * k);  
                          if (t <= k && t * t == t2 - k * k) {                                
                               System.out.println("(" + i + "^2) + (" + j + "^2) + ("+ k + "^2) + ("+ t +"^2)");  
                          }  
                     }  
                }  
           }  


Thursday, August 4, 2011

Prevent copy paste on input : allow only numeric values jQuery


The following example won't allow values other than numeric.
  1. From keyboard (even copy paste)
  2. From drag drop
  3. From mouse (copy paste)
Demo : Try demo here
 <!DOCTYPE html>  
 <html>  
      <head>  
           <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />  
           <title>jQuery UI Example Page</title>  
           <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>  
           <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script>  
      </head>  
      <body>  
           <input type="text" id="textBox" class="numericOnly" />  
      </body>  
      <script type="text/javascript">  
           $(function(){  
                $(".numericOnly").bind('keypress',function(e){  
                          if(e.keyCode == '9' || e.keyCode == '16'){  
                                return;  
                           }  
                           var code;  
                           if (e.keyCode) code = e.keyCode;  
                           else if (e.which) code = e.which;   
                           if(e.which == 46)  
                                return false;  
                           if (code == 8 || code == 46)  
                                return true;  
                           if (code < 48 || code > 57)  
                                return false;  
                     }  
                );  
                $(".numericOnly").bind("paste",function(e) {  
                     e.preventDefault();  
                });  
                $(".numericOnly").bind('mouseenter',function(e){  
                      var val = $(this).val();  
                      if (val!='0'){  
                           val=val.replace(/[^0-9]+/g, "")  
                           $(this).val(val);  
                      }  
                });  
           });  
      </script>  
 </html>  

Tuesday, August 2, 2011

Override Invalid value for field [fieldName] message in Struts2

The default field error message in struts 2 is Invalid field value for field "[fieldName]".

To override this message you need to create a property file with a file name pattern [ClassName].properties under the package of the Action class.



If you are getting error message like : Invalid field value for field "birthDate". You can override this by adding a line in properties file like,

             invalid.fieldvalue.birthDate = Birthdate is invalid

So now your message is : "Birthdate is invalid"