Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 101857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:51:07+00:00 2026-05-11T00:51:07+00:00

I have written a simple brainfuck interpreter in MATLAB script language. It is fed

  • 0

I have written a simple brainfuck interpreter in MATLAB script language. It is fed random bf programs to execute (as part of a genetic algorithm project). The problem I face is, the program turns out to have an infinite loop in a sizeable number of cases, and hence the GA gets stuck at the point.
So, I need a mechanism to detect infinite loops and avoid executing that code in bf.
One obvious (trivial) case is when I have

[] 

I can detect this and refuse to run that program.
For the non-trivial cases, I figured out that the basic idea is: to determine how one iteration of the loop changes the current cell. If the change is negative, we’re eventually going to reach 0, so it’s a finite loop. Otherwise, if the change is non-negative, it’s an infinite loop.
Implementing this is easy for the case of a single loop, but with nested loops it becomes very complicated. For example, (in what follows (1) refers to contents of cell 1, etc. )

++++ Put 4 in 1st cell (1) >+++ Put 3 in (2) <[   While( (1) is non zero)     --   Decrease (1) by 2     >[   While( (2) is non zero)         -    Decrement (2)         <+   Increment (1)      >]        (2) would be 0 at this point     +++  Increase (2) by 3 making (2) = 3 <]   (1) was decreased by 2 and then increased by 3, so net effect is increment 

and hence the code runs on and on. A naive check of the number of +’s and -‘s done on cell 1, however, would say the number of -‘s is more, so would not detect the infinite loop.
Can anyone think of a good algorithm to detect infinite loops, given arbitrary nesting of arbitrary number of loops in bf?

EDIT: I do know that the halting problem is unsolvable in general, but I was not sure whether there did not exist special case exceptions. Like, maybe Matlab might function as a Super Turing machine able to determine the halting of the bf program. I might be horribly wrong, but if so, I would like to know exactly how and why.

SECOND EDIT: I have written what I purport to be infinite loop detector. It probably misses some edge cases (or less probably, somehow escapes Mr. Turing’s clutches), but seems to work for me as of now. In pseudocode form, here it goes:

subroutine bfexec(bfprogram) begin     Looping through the bfprogram,         If(current character is '[')             Find the corresponding ']'             Store the code between the two brackets in, say, 'subprog'             Save the value of the current cell in oldval             Call bfexec recursively with subprog             Save the value of the current cell in newval             If(newval >= oldval)                 Raise an 'infinite loop' error and exit             EndIf         /* Do other character's processings */         EndIf     EndLoop end 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:51:07+00:00Added an answer on May 11, 2026 at 12:51 am

    When I used linear genetic programming, I just used an upper bound for the number of instructions a single program was allowed to do in its lifetime. I think that this is sensible in two ways: I cannot really solve the halting problem anyway, and programs that take too long to compute are not worthy of getting more time anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a simple jQuery script that changes the hash tag of a
I have written a simple file upload script but it gives me the error
I have written a simple perl script to read a line from a .csv
I have written simple script: #!/bin/bash find . -name *.m4a | while read filename;
I have written a simple random number generator in C. int l is the
I have written simple code to get content from xml file to php. $xml
I have written a simple Java dispatcher with a daemon thread to handle the
I have written a simple WCF service that accepts and stores messages. It works
I have written a simple CRUD form which has one select list. However the
I have written a simple C++ shell program to parse large XML files and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.