Friday, July 29, 2016

Property in C sharp

Thursday, July 28, 2016

Conditional operator

This is the "Conditional Operator ".The conditional operator ? : uses the boolean value of one expression to decide which of two other expressions should be evaluated.If the boolean value of the condition is true, the left side of colon ":" is executed. If it's false, the right side is executed.

public class QuestionTest { public static void main(String[] args) { String test; int num1 = 0; int num2 = 0; int num3 = 0; int num4 = 0; num1 = 5; test = num1 > 0 ? "Positive" : "Negative"; System.out.println(test); num2 = 6; test = num2 > 0 ? "Positive" : "Negative"; System.out.println(test); num3 = -3; test = num3 > 0 ? "Positive" : "Negative"; System.out.println(test); num4 = 78; test = num4 > 0 ? "Positive" : "Negative"; System.out.println(test); test = num1 > 0
? num2 > 0
? num3 > 0
? num4 > 0
? "All numbers are positive"
: "Num4 isn't positive"
: "Num3 isn't positive"
: "Num2 isn't positive"
: "Num1 isn't positive";
System.out.println(test+"\n"); //Only num3 is negative. num1 = 5; test = num1 > 0 ? "Positive" : "Negative"; System.out.println(test); num2 = 6; test = num2 > 0 ? "Positive" : "Negative"; System.out.println(test); num3 = 3; test = num3 > 0 ? "Positive" : "Negative"; System.out.println(test); num4 = 78; test = num4 > 0 ? "Positive" : "Negative"; System.out.println(test); test = num1 > 0 ? num2 > 0 ? num3 > 0 ? num4 > 0 ? "All numbers are positive" : "Num4 isn't positive" : "Num3 isn't positive" : "Num2 isn't positive" : "Num1 isn't positive"; System.out.println(test); //All numbers are positive. } }

Tuesday, July 26, 2016

BigDecimal sample code

The code written below is an example for BigDecimal.

-----------------------------------------------------------------------------
import java.math.BigDecimal;

public class HelloWorld
{
  public static void main(String[] args)
  {
    BigDecimal one = new BigDecimal("1.00");
    BigDecimal two = new BigDecimal("2.00");
    BigDecimal three = new BigDecimal("3.00");

    BigDecimal addition = one.add(two);
    BigDecimal subtraction = one.subtract(two);
    BigDecimal multiplication = two.multiply(three);
    BigDecimal division = one.divide(three, 2, BigDecimal.ROUND_HALF_UP);
   
    System.out.println("addition: " + addition);
    System.out.println("subtraction: " + subtraction);
    System.out.println("multiplication: " + multiplication);
    System.out.println("division: " + division);
  }
}
-----------------------------------------------------------------------------

Primality test using BigDecimal

Test this code here: https://www.compilejava.net/
Delete all of the code on the online compiler website and just copy and paste this code to the website as it is. Then click the button "compile and execute".

The result
By the way, this code is using a function "square root using BigDecimal".

import java.math.BigDecimal;
import java.math.MathContext;

public class HelloWorld
{
   public static void main(String[] args)
   {
       BigDecimal test1 = new BigDecimal(4);
       display(test1);
       BigDecimal test2 = new BigDecimal(3);
       display(test2);
       //If the number is larger than integer scope, pass the number as string.
       BigDecimal test3 = new BigDecimal("49999991");
       display(test3);
    }

    public static BigDecimal sqrt(BigDecimal a, int scale){

        BigDecimal x = new BigDecimal(Math.sqrt(a.doubleValue()), MathContext.DECIMAL64);
        if(scale < 17) return x;

        BigDecimal b2 = new BigDecimal(2);
        for(int tempScale = 16; tempScale < scale; tempScale *= 2){
            //x = x - (x * x - a) / (2 * x);
            x = x.subtract(
                    x.multiply(x).subtract(a).divide(
                    x.multiply(b2), scale, BigDecimal.ROUND_HALF_EVEN));
        }
        return x;
    }

 private static void display(BigDecimal Num)
    {
         if( isPrimeNum( Num ) ) {
            System.out.println( Num + " is a prime number." );
         } else{
            System.out.println( Num +" isn't a prime number.");
         }
      }

   private static boolean isPrimeNum( BigDecimal x )
   {
       BigDecimal zero = new BigDecimal(0);
       BigDecimal two = new BigDecimal(2);
       BigDecimal three = new BigDecimal(3);
       
         if( x.compareTo(two) == 0 )
         {      
          return true;
         }
       
         if( x.compareTo(two) < 0 || x.remainder(two).compareTo(zero) == 0 )
         {
         
          return false;
         }
 
         for(BigDecimal a = new BigDecimal(3); a.compareTo(sqrt(x, 2)) <= 0; a = a.add(two) )
         {
           if( x.remainder(a).compareTo(zero) == 0 )
           {
             return false;
           }
         }

     return true;
   }
}

Monday, July 25, 2016

Square root using BigDecimal

This is an implementation of "square root" using BigDecimal.

The Newton's methond:
https://en.m.wikipedia.org/wiki/Newton%27s_method
https://en.m.wikipedia.org/wiki/Integer_square_root
This can be used to find integer square roots.

Test this code here: https://www.jdoodle.com/online-java-compiler/
Delete all of the code on the online compiler website and just copy and paste this code to the website as it is. Then click the button "compile and execute".

The result
If you want more/less accuracy, just change the number 50 to another number.
If you pass 100 as argument, the significant figures will be 100 digits.

If you write like this:

BigDecimal sqrttest1000 = sqrt(new BigDecimal("2"), 1000); //Square root of 2 (1000 digits)
System.out.println(sqrttest1000);


The result is:
...and continues until 1000th digit

The code:

import java.math.BigDecimal;
import java.math.MathContext;

public class HelloWorld
{
   public static void main(String[] args)
   {
        BigDecimal sqrttest1 = sqrt(new BigDecimal("4"), 50); //Square root of 4 (50 significant digits)
        System.out.println(sqrttest1);
        BigDecimal sqrttest2 = sqrt(new BigDecimal("2"), 50); //Square root of 2 (50 significant digits)
        System.out.println(sqrttest2);
        BigDecimal sqrttest3 = sqrt(new BigDecimal("1"), 50); //Square root of 1 (50 significant digits)
        System.out.println(sqrttest3);
    }

    public static BigDecimal sqrt(BigDecimal a, int scale){

        BigDecimal x = new BigDecimal(Math.sqrt(a.doubleValue()), MathContext.DECIMAL64);
        if(scale < 17){
             x = x.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
             return x;
        }

        BigDecimal b2 = new BigDecimal(2);
        for(int tempScale = 16; tempScale < scale; tempScale *= 2){
            x = x.subtract(
                    x.multiply(x).subtract(a).divide(
                    x.multiply(b2), scale, BigDecimal.ROUND_HALF_EVEN));
        }
        return x;
    }
}

Thursday, July 21, 2016

DoEvents

What is DoEvents in VBA?

DoEvents is used to achieve a pseudo-multi-threading in VBA. Microsoft explains DoEvents as follows:

The DoEvents function surrenders execution of the macro so that the operating system can process other events.

But this is difficult to understand. Actually I do not understand what it is meaning. Now I will explain in better way.

Look at the code below:

Sub test()
Dim i As Long
Dim j As Long
    For i = 0 To 100000
        j = i
            If i = j Then
                Range("A1") = i
            End If
    Next
MsgBox ("Finished")
End Sub

This code takes a lot of time until it finishes executing. This is because comparison of two variables is time-consuming work.

If this code takes 20 seconds until finishing executing, the user of the program needs to wait for 20 seconds without doing nothing. This is ultimately frustrating, isn’t it?
You can solve this problem by using “DoEvents”. See the code below:

Sub test()
Dim i As Long
Dim j As Long
    For i = 0 To 100000
        j = i
        If i = j Then
            Range("A1") = i
        End If
        DoEvents
    Next
MsgBox ("Finished")
End Sub
In this code, you can see DoEvents is inserted inside the For loop.

What happens if we execute the code?

If we execute this code, the excel accepts our operation during the execution.The reason why it accepts operation is the For loop has DoEvents inside.If For loop doesn’t have DoEvents inside, the For loop just execute the code without doing anything else.

(For loop 1) -> (For loop 2) -> (For loop 3) -> … -> For loop finishes!

During this process, OS doesn’t accept any other operation.

Meanwhile, by exercuting “DoEvents”, the For loop is interrupted and OS accepts other operation during the interruption. Just after the interruption, another loop is started, so the whole process will be:

(For loop 1) -> (OS accepts other operation) -> (For loop 2) -> (OS accepts other operation) -> … -> For loop finishes!

This loop is repeated very quickly, so this can be interpreted as if the program is accepting other operations even during the For loop process. This is the pseudo-multi-threading in VBA.

So, if the For loop process is time consuming, you can make the program accept other operation made from users by inserting “DoEvents” inside the For loop.