Wednesday, March 20, 2013

Aptitude: Easy Solution to find Square and Square root of a number


Square of numbers ending with 5
It’s simple logic, any multiplication of 5 will end with 25, so at end of the result it will be 25, in the result starting, it will be the multiplication of n * (n+1)
For Eg: 35 * 35
Where n = 3,
 3 * (3+1) = 3*4 = 12.
Append 25 to the result the answer is 1225
5
15
25
5
15
25
=
=
=
0 * (0+1) 25
1 * (1+1) 25
2 * (2+1) 25
25
225
625

Square of numbers ending with 1, 4, 6 and 9.
There is a small logic for this numbers ending.
For 1 and 6
262 = 252 + (25+26) = 625 + 51 = 676
712 = 702 + (70+71) = 4900 + 141 = 5041
For 4 and 9
242 = 252 – (25+24) = 625 – 49 = 576
692 = 702 - (70+69) = 4900 - 139 = 4761
Square root for four digit number.
First right out the squares from1 to 9.
1             1
2             4
3             9
4             16
5             25
6             36
7             49
8             64
9             81

Number ends with
1                 1 and 9
4                 2 and 8
9                 3 and 7
6                 4 and 6
5                 5

For Ex: find the square root for 1156
Now, Split the number 1156 as 11 and 56
Check where the first split lies between the nearest squares.
9 ≤ 11 ≤ 16
32 ≤ 11 ≤ 42
The lowest nearest one has to be taken here. Means, we are going to take 3.
So, the result of the first number will be 3
Multiple with n+1 number, for ex, the lowest nearest one is 3, then n=3.
So, multiple n with n+1 as below
3 * 4 = 12

Check whether 12 is less than 11 or not.
Since the unit number of 1156 is 6, as per the table the number can be 4 or 6.
If it is less take 4 else 6.
Since 12 is greater than 11, we are going to take 6.

So, the square root of 1156 is 36.

One More Ex:

Let check for 2401
Split number 24 and 01

16 ≤ 24 ≤ 25
42 ≤ 24 ≤ 52

4*5 = 20

Unit digit is 1, so the result of end number can be 1 and 9
24 is the first split one,
Check 20 with 24 is greater than or less than the first split number,
20 < 24
So take 9.

Result = 49

Let check for 1681
Split number 16 and 81

16 ≤ 16 ≤ 25
42  ≤ 16 ≤ 52

4*5 = 20

Unit digit is 1, so the result of end number can be 1 and 9
16 is the first split one,
Check whether 16 with 20, 20 > 16, and so take 1.
Result = 41


Wednesday, February 20, 2013

Solution: Emulator Failed to allocate memory: 8...


I have started my AVD from eclipse, suddenly I faced an exception:

[2013-02-20 09:18:12 - Emulator] Failed to allocate memory: 8
[2013-02-20 09:18:12 - Emulator]
[2013-02-20 09:18:12 - Emulator] This application has requested the Runtime to terminate it in an unusual way.
[2013-02-20 09:18:12 - Emulator] Please contact the application's support team for more information.

The error occurred due to less amount of physical RAM.

Solution is to edit C:\Users\<user>\.android\avd\<avd-profile-name>.avd\config.ini and change the value 

hw.ramSize=1024
to
hw.ramSize=1024MB

hw.ramSize : Device ram size : The amount of physical RAM on the device, in megabytes. Default value is "96".

Monday, February 18, 2013

Solution: Invalid payload item type onMenuItemSelected

If you're developing for Android 2.3 or lower, users can reveal the options menu panel by pressing the Menu button.
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated.

You have a sample code in 2.3, which is working fine for MENU option.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sample, menu);
        return true;
    }
   
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.logout:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


But there is a following exception when you have run same code in 4.0+ version

java.lang.IllegalArgumentException: Invalid payload item type
at android.util.EventLog.writeEvent(Native Method)
at android.app.Activity.onMenuItemSelected(Activity.java:2565)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:973)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:309)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1099)
at android.widget.ListView.performItemClick(ListView.java:4758)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2556)
at android.widget.AbsListView$1.run(AbsListView.java:3223)
at android.os.Handler.handleCallback(Handler.java:608)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

Solution: 
Override onMenuItemSelected, and use return true or false than super.onMenuItemSelected(featureId, item).