LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Add two very big numbers

Solved!
Go to solution

Hi guys

 

I am trying to add two big integers (like in Fibonacci sequence). Big, means much larger than I64. I wrote a vi, attached here, and I am wondering if it can be done better, aka faster (@Altenbach: if you provide a solution please add a ELI5 🙂 )

To explain, I am putting the numbers in arrays of U8 and do the addition as in elementary school.

nitad54449_0-1697375199441.png

 

If you have ideas how to do it faster pls let me know.

Regards

 

0 Kudos
Message 1 of 24
(1,489 Views)

I once found this on the web.

0 Kudos
Message 2 of 24
(1,469 Views)

@nitad54449 wrote:

Hi guys

 

I am trying to add two big integers (like in Fibonacci sequence). Big, means much larger than I64. I wrote a vi, attached here, and I am wondering if it can be done better, aka faster (@Altenbach: if you provide a solution please add a ELI5 🙂 )

If you have ideas how to do it faster pls let me know.


There are several overcomplications in your code. Maybe you can have a look at my primitive factorial. The subVIs could easily be expanded to add two numbers.

 

One possibility to improve speed would be to use integers with more bits, each containing several digits. (e.g. base 10000000 instead of base 10)

0 Kudos
Message 3 of 24
(1,449 Views)
Solution
Accepted by topic author nitad54449

A quick analysis of your VI:

 

  • The algorithm is flawed and gives incorrect results if the array sizes differ by more than one element. (e.g. try 1234+56. Your result is short!
  • Insert into array is the incorrect function to append to an array. either use "built array" of "reshape array". reshape has the advantage that it automatically pads with zeroes.
  • I don't think you need any case structure or green datatypes.

Here's what I might do. No guaranteed that it's faster... 😄

 

altenbach_0-1697385165499.png

 

Message 4 of 24
(1,441 Views)

Hi Christian

I forgot to mention that the first number (array) is the same length as the second or, at most, one digit larger. I will take a look at your code and check it is faster,

thanks

0 Kudos
Message 5 of 24
(1,435 Views)

Hi CA

after some tests, your vi is 3 times faster than mine !

thanks

0 Kudos
Message 6 of 24
(1,425 Views)

I am sure it could be made a few orders of magnitude faster 😄

 

 

0 Kudos
Message 7 of 24
(1,422 Views)

thanks

i tried the add and it does not work properly

N

0 Kudos
Message 8 of 24
(1,417 Views)

be my guest 🙂

I am trying to find (just for fun) big Fibonacci numbers.

The 10^6 number has 200K digits, it takes 400 seconds with my code. 

N

0 Kudos
Message 9 of 24
(1,414 Views)

Here's a quick draft to generate the elements if the Fibonacci sequence. A google search for element 1000 shows the the result is correct.

 

altenbach_0-1697391469840.png

 

As mentioned, it could still be sped up by orders of magnitude... 😄

Message 10 of 24
(1,396 Views)