drbitboy mentioned that simply using a loop may not be very effective in this case. If the ID begins with 99, then the 249th Fibonacci number (found by adding 99 to 150) is a long string of digits. It seems unlikely that either this Fibonacci number or the one before it can be represented in the PLC.
A more efficient approach could be to save the 150th to 249th Fibonacci numbers in two arrays, one in decimal and one in hexadecimal. By using the first two digits as the index, you can easily retrieve the desired Fibonacci number.
It's important to note that adding 150 as an offset may not be necessary if you start with the 149th and 150th Fibonacci numbers and continue from there. The only time you would need to add the offset is if you are using the one-step formula to calculate the number directly.
This solution seems promising. Although it may be challenging for someone relatively new like yourself, as you mentioned, since you are an intern in industrial automation. Feel free to ask for more guidance if needed. Thank you.
In a discussion about Fibonacci numbers, user drbitboy pointed out that in systems with IDs starting with 99, the 249th Fibonacci number is exceptionally large and beyond the capabilities of the PLC being used. The maximum value supported by the S7-1500 system is an ULINT (unsigned long integer) with a limit of 18446744073709551615, far smaller than the Fibonacci number mentioned. This raises questions about the practical applications of such large numbers in industrial automation. Citythunder, an intern in the field, expressed disbelief at the complexity of the task and wondered about the feasibility of using basic Fibonacci sequences without look-up tables. The limitations of the S7-1500 system add difficulty to solving such tasks, leading to uncertainty about their real-world relevance.
JesperMP noted that the largest native data type in S7-1500 is ULINT (unsigned long integer), with a maximum value of 18446744073709551615. However, this limit falls far short of the seemingly astronomical figure of 4880197746793002076754294951020699004973287771475874. It's puzzling to consider what industrial application could possibly require such immense numbers. The task at hand feels more like a challenging test rather than a realistic automation scenario. Additionally, the suggestion to use the basic Fibonacci sequence hints at a restriction on using a lookup table. One might feel perplexed when faced with this challenge, as the S7-1500 is unable to process numbers larger than ULINT.
Before embarking on the main questions, they provided me with some warm-up exercises. I see this as an opportunity to enhance my skills by tackling these initial questions. While it is understood that handling such large numbers directly may not be feasible, there have been suggestions to divide the data into smaller parts for processing. Thank you to those who have shared such insights.
When considering the fibonacci sequence, which involves adding the two previous numbers to get the next number, a method to calculate it is by splitting the numbers into two 64-bit ULINTs. This involves adding the higher ULINTs together, as well as the lower ULINTs. If the addition of the lower ULINTs results in an overflow, you would need to add 1 to the higher ULINT and place the remainder in the lower ULINT. Please note that this is a theoretical approach and has not been tested for accuracy. This concept is more academic in nature, similar to something a math teacher might discuss, rather than being directly applicable to industrial programming.
If you're looking to implement the Fibonacci sequence in ladder logic, it can be achieved by breaking down the numbers into 32-bit ULINTs and adding the previous two numbers. By adding the higher and lower ULINTs separately and carrying over any remainder, you can accurately calculate the Fibonacci sequence in PLC programming. This method simplifies the process and is a common query among industrial programmers. It's a unique approach that combines math and logic to efficiently solve a classic problem.
Citythunder expressed interest in learning more about implementing a clever idea using ladder logic. Unfortunately, I have limited time to provide a step-by-step guide. If this is a test, it would be beneficial for you to demonstrate your own skills. It's worth noting that even 128 bits are insufficient to accurately represent the large number 4880197746793002076754294951020699004973287771475874.
Creating loops in ladder logic follows a similar structure to loops in traditional programming. By incrementing a counter each time the ladder logic is executed, you can then check if the counter meets the required threshold. This iterative process is essential for automating tasks efficiently in ladder logic programming.
Jesper's concept of using two UDINTs as a foundation is sound, but a more inclusive approach is needed. The 249th Fibonacci number consists of 55 decimal digits, equivalent to approximately 180+ bits (55 divided by log10(2) = 55 / 0.3010 binary digits). Therefore, utilizing three 64-bit UDINTs with manual carry should be sufficient, especially for generating hexadecimal output as they can be easily converted into a hexadecimal string format. These three UDINTs can accurately represent the values of the Fibonacci numbers, although converting them into decimal strings is a complex task requiring a similar algorithm to calculating each of the 56 decimal digits individually. I may provide further insights later, but for now, I am going to read "Hairy Hettie the Highland Cow" to my granddaughter.
citythunder expressed interest in exploring a better solution, but being new to the field, finding it challenging. As an intern in industrial automation, they encountered a problem that may exceed the capabilities of their current knowledge. It's essential in the engineering field to recognize when a problem may be unsolvable with existing resources. It could be a test to push boundaries and explore new solutions. Seeking guidance from a mentor or supervisor on next steps is a wise approach. This could lead to learning opportunities and growth, whether through tackling complex programming tasks or addressing practical issues in a system. Communication with superiors is key in navigating challenges, whether it's managing limited budgets, space constraints, or operational inefficiencies. In such scenarios, a collaborative approach with leadership can lead to innovative solutions and new perspectives.
Drbitboy's suggestion seems correct, but implementing it would require utilizing 3 ULINTs. Converting a 192-bit answer back to decimal may be necessary for practical use. However, this process involves subtracting various powers of 10 in binary form, thus requiring 192-bit addition and subtraction routines.
- 01-09-2024
- Peter Nachtwey
@mk42 raises a valid point regarding the purpose of the exercise. It may not be about completing the task itself, but rather about recognizing that the sequence cannot be generated using basic data types. This could potentially lead to issues of plagiarism from online sources, especially in a certification course. Alternatively, the exercise may be designed to test your ability to utilize external resources such as the internet.
In my opinion, a straightforward approach would be to find and compile a list of 100 decimal strings containing the 150th through the 249th Fibonacci numbers online. This information can be stored in an array of strings, with the .Haneint value serving as the index for lookup. While this approach may not address the hexadecimal aspect of the problem, it provides a starting point.
It is worth mentioning that an analytical solution is achievable for those familiar with ladder logic or structured text, which is more suitable for algorithmic tasks like this one. It is important to acknowledge your current level of understanding in PLC programming and be honest with yourself about seeking help from online communities like plctalk.
Peter Nachtwey mentioned that using three ULINTs would be necessary for handling a 192-bit answer in binary format. Converting this answer back to decimal form could be achieved by implementing 192-bit add and subtract routines. Although initially referred to as UDINTs, they are actually 64-bit numbers, which solves the hexadecimal string issue. To convert to decimal, a library like BigNumber or Decimal module in Python would be needed. Alternatively, using BigNumber code for calculating the Fibonacci sequence elements may be more efficient. It's worth noting that there is a one-step formula for finding the n'th Fibonacci Number, but it requires capabilities like square root calculations. When using an iterative approach, there is a risk of a watchdog timeout if the calculation cannot be completed within one scan cycle. It's advisable to ensure you have the necessary resources for this task, and basic mathematical skills are sufficient to solve it. Education is not just about mastering calculations, but also about learning how to learn effectively.
I utilized a byte array to depict the digits of the Fibonacci Number result, allowing for easy scalability to handle large numbers. However, I have yet to implement the hexadecimal representation.
Calculate Fibonacci sequence up to 150 using this code.
When working on generating the Fibonacci sequence in ladder logic, I envisioned a more engaging approach compared to traditional ST/SCL programming. By adjusting line numbers 16 and 18 to use hexadecimal digits 15 and 16 instead of 9 and 10, you can optimize the code to only require 47 of the total 55 digits. Although line numbers 4 and 5 could be removed, consider initializing the arrays with digits for Fibonacci Numbers 149 and 150 for a more efficient process. This will allow for easier tracking of the "ID number" of steps taken in the sequence.
For those looking for a straightforward method, they can access Fibonacci Numbers 150-249 in decimal and hexadecimal form through a string array lookup. Below is the essential data and accompanying script for creating the array.
Now that you have completed the task on his behalf, perhaps he will secure the internship!
While mastering ladder logic may seem straightforward, the real challenge lies in developing critical thinking skills to apply it effectively. This is where the true difficulty arises. Cheers!