Categories
discuss

Homework: how to write own multiplication of big numbers?

In my project I have to deal with multiplication of big numbers ( greater then java.long ) stared in my own BigNumber class as int[]. Basically I need to implement something like this :

    157 x
    121 y
   ----
    157 result1
   314  + result2
  157   + result3
 ------
 18997  finalResult

But how do I implement it?

I thought about expanding result2,3 with zeros (3140, 15700) and adding them. But first I somehow need to navigate between each digit of y and multiply it by each digit of x.

Answer

Use the diagonal approach. Make an array, and multiply each digit by each other digit and fill in the numbers in each cell.

36 x 92

       3     6
    +-----+-----+
    | 2 / | 5 / |
9   |  /  |  /  |
    | / 7 | / 4 |
    +-----+-----+
    | 0 / | 1 / |
2   |  /  |  /  |
    | / 6 | / 2 |
    +-----+-----+

Add the numbers on each diagonal. Move from the least-significant digit (at the lower right) to the most (upper left).

2                                                                    2 (least-significant)
(6 + 1 + 4) = 11 (make this 1, and carry the 1 to the next digit)    1
(5 + 7 + 0 + 1(carried)) = 13 (make this 3, and carry the 1)         3
2 + 1(carried) = 3                                                   3 (most-significant)

The answer’s 3312.

Make a two-dimensional array of your digits. Fill the array with the multiplications of the single digits together.

Write some logic to scrape the diagonals as I did above.

This should work for arbitrarily large numbers (as long as you still have memory left).

Categories
discuss

Object vs byte[0] as lock

I commented earlier on this question (“Why java.lang.Object is not abstract?”) stating that I’d heard that using a byte[0] as a lock was slightly more efficient than using an java.lang.Object. I’m sure I’ve read this somewhere but I can’t recall where: Does anyone know if this is actually true?

I suspect it’s due to the instantiation of byte[0] requiring slightly less byte code than Object, although it was pointed out that byte[0] requires additional storage in order to store the length field and so it sounds like this might negate any benefit.

Answer

I got curious enough to test it. Sourcecode:

public class Test {
    public static Object returnObject() {
        return new Object();
    }

    public static byte[] returnArray(){
        return new byte[0];
    }
}

Bytecode:

public static java.lang.Object returnObject();
  Code:
   0:   new     #2; //class java/lang/Object
   3:   dup
   4:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   7:   areturn

public static byte[] returnArray();
  Code:
   0:   iconst_0
   1:   newarray byte
   3:   areturn

So you’re right in that the byte code is shorter for arrays, because array creation has its own JVM opcode. But what does that mean? Nothing, really. It’s a virtual machine, so there’s absolutely no guarantee that fewer bytecode instructions mean less work for the actual physical CPU. We could start profiling of course, but that would be quite pointless. If there is a difference at all, no matter which way, it will never ever matter. Object creation is incredibly fast nowadays. You’d probably have to start using long for your loop index before you can even measure the total time.

Categories
discuss

android:layout_gravity doesn’t work as expected

I have a simple LinearLayout with one TextView and one ImageView. I want the text in the TextView to be aligned to the right, but the result show that the Text was aligned to the left. Is there anything wrong with my layout xml? Thanks.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:background="#E8E3E4">
    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        >
        <TextView android:layout_width="260dp" android:layout_height="wrap_content"
            android:text="More Comments" android:textStyle="bold" android:background="#ff0000"
            android:textColor="#121222" android:layout_gravity="right" />
        <ImageView android:layout_width="50dp"
            android:layout_height="wrap_content" android:src="@drawable/arrow_right"
            android:layout_gravity="center" android:scaleType="centerInside" />
    </LinearLayout>
</LinearLayout>

Thanks, Michael. But the workaround you mentioned doesn’t work either.

I tried to use RelativeLayout, but it still gave me result like: More Comments (Icon)

What I expected is More Comments (Icon)

The RelativeLayout xml is below. Is there anything wrong with it?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/label" android:layout_width="260dp"
        android:layout_height="wrap_content" android:text="More Comments"
        android:textStyle="bold" android:background="#ff0000"
        android:textColor="#121222" android:layout_gravity="right" />
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content"
        android:src="@drawable/arrow_right" android:layout_gravity="center"
        android:scaleType="centerInside" android:layout_toRightOf="@id/label" />
</RelativeLayout>

Following the workaround in Michael’s comment, I have the XML below. I expected the output to be:

     RightLeft

but the actual output is:

Right             Left

So the workaround doesn’t work for me. Any thoughts?

   <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
           <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_gravity="right"
                   android:layout_weight="1.0"                       
                   android:text="RIGHT"/>
           <TextView
                   android:layout_width="wrap_content"

                   android:layout_height="wrap_content"
                   android:layout_gravity="left"
                   android:text="LEFT"/>
   </LinearLayout>

Answer

I figured out what is wrong with my xml. I should have used gravity instead of layout_gravity to right-align the text in TextView. The following XML works as expected.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/label" android:layout_width="260dp"
        android:layout_height="wrap_content" android:text="More Comments"
        android:textStyle="bold" android:background="#ff0000"
        android:textColor="#121222" android:gravity="right" />
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content"
        android:src="@drawable/arrow_right" android:layout_gravity="center"
        android:scaleType="centerInside" android:layout_toRightOf="@id/label" />
</RelativeLayout>
Categories
discuss

JavaScript charts – dynamically adding data points

I am trying to dynamically add data points to jqplot as a result of AJAX received data, but I do not see a way to accomplish this. Is this not possible?

If it isn’t, what other packages are available that can accomplish the same basic graphing plus allow for dynamic data?

Answer

You may want to check the example below on how this is handled in Flot. Flot is an open-source plotting library based on jQuery, like jqplot. Both libraries are very similar.

This is how fetching and plotting the data with AJAX would look like in code:

function fetchData() {
   $.ajax({
      url:      "json_fetch_new_data.php",
      method:   "GET",
      dataType: "json",
      success:  function(series) {
         var data = [ series ];

         $.plot($("#placeholder"), data, options);
      }
   });

   setTimeout(fetchData, 1000);
}

Make sure to check the following demo to see it in action:

For further information on Flot:

Categories
discuss

Web visualisations for data?

I would like to display a bunch of data in a meaningful way through visualizations on a site.

I have been experimenting with RaphaelJS library, but performance on IE8 is terrible, and HTML5 canvas is still not widely supported.

So I’m left with Flash and any existing libraries.

On the flash side there seem to be many, but because of this I am a bit lost and don’t have the possibility to experiment with each one.


( I will list any libraries that people have pointed out here. )

Flash / Flex:

Javascript:

Java:


Help would be very much appreciated. Thanks!

Answer

If you do end up going with Flash, consider the following visualization libraries. My personal favorite is Flare, but the Flex Charting Components probably have the easiest learning curve.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..