Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 71
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
One birthday today, congrats!
mileswaldron (59)


Next birthdays
06/03 mileswaldron (59)
06/04 muze801 (33)
06/05 HVgeek (33)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

Mathematical quiz

Move Thread LAN_403
Bjørn
Wed Feb 20 2008, 12:39PM Print
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
L	movs	 r1,r0,lsr #1
	 adccs	r1,r0,r0,lsl #1
	 mov	  r0,r1
	 bne	  L

1. What does this code do
2. What is the name of the mathematician that made a conjecture about this algorithm
3. Which fundamental computability problem does it illustrate

The idea here is to learn something, that is why I tried to make it difficult to use Google to get the answers without thought.
Back to top
Avi
Sun Feb 24 2008, 04:23PM
Avi Registered Member #580 Joined: Mon Mar 12 2007, 03:17PM
Location: Melbourne, Australia
Posts: 410
from what i can work out using the ARM datasheet and some notes on ARM assembly, I'd say it does this.
pseudo code 1:
label0
r1=r0/2
carry=lsb(r0)
zero=ifzero(r1)
if carry is set then r1=r0+(r0*2)+carry
r0=r1
if zero isnt set then jump to label0

pseudo code 2 expanded:
loop=true
do while loop=true
if lsb(r0) then
r1=(r0*3)+1
else
r1=r0>>1
end if
loop=false
if r1<>0 then loop=true
r0=r1
loop

So it seems quite complex. It does not seem easy to work out how many times it will loop or if it will exit, by just looking at it.
Back to top
Bjørn
Sun Feb 24 2008, 06:15PM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
That is correct. Most of the information needed to solve part 1 can be found in the HvWiki Link2

It is remarkably complex for a 4 instruction long program. The difficulty to fully grasp it is what makes it interesting to matematicians and hints to the answer of part 3.

Part 2 is just a simple google search away for someone that is used to seach for mathematical equations (simple or not).
Back to top
Chris Russell
Sun Feb 24 2008, 10:52PM
Chris Russell ... not Russel!
Registered Member #1 Joined: Thu Jan 26 2006, 12:18AM
Location: Tempe, Arizona
Posts: 1052
Basically, the program does this:

Divide r0 by two, store the result in r1, set the zero and carry flags appropriately. If that operation sets the carry flag (r0 was not even, but was odd), multiply r0 by three and add one, and store that result in r1. Copy the value of r1 to r0. If the zero flag has not been set, start over.

Basically, you are basically starting the loop with some integer, r0. If r0 is even, divide it by two. If it's odd, multiply it by three and add one. This process repeats until it reaches one of the powers of two, at which point it will slide down the ladder quickly, finally reaching one and then exiting, ending something like 64,32,16,8,4,2,1. But does this always work? Will it always reach one? Difficult to say just by looking at it.

Curiously, on the last iteration, when r0 is set to 1, r0 will be set to 4 before the loop exits. I suppose you would extract the correct sequence if you extracted your output between the adccs instruction and the mov instruction.

Here's what I would get in c, this should be more or less correct:

int r0 = ; /* starting number here */
        int r1,zero;
        do
                {
                r1 = r0 / 2;
                zero = r1;
                if (r0 & 1)
                        {
                        r1 = (r0 * 3) +1;
                        }
                        r0 = r1;
                } while (zero);


Complete program, modified to accept an integer from the command line and have nice output, is attached.

]bjoern.c.txt[/file]

Example output:

Variable check: 123
Starting...
123, 370, 185, 556, 278, 139, 418, 209, 628, 314, 157, 472, 236, 118, 59, 178, 89, 268, 134, 67, 202, 101, 304, 152, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, done!

Variable check: 112486
Starting...
112486, 56243, 168730, 84365, 253096, 126548, 63274, 31637, 94912, 47456, 23728, 11864, 5932, 2966, 1483, 4450, 2225, 6676, 3338, 1669, 5008, 2504, 1252, 626, 313, 940, 470, 235, 706, 353, 1060, 530, 265, 796, 398, 199, 598, 299, 898, 449, 1348, 674, 337, 1012, 506, 253, 760, 380, 190, 95, 286, 143, 430, 215, 646, 323, 970, 485, 1456, 728, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1, done!

I think that explains pretty well what the code does, which bring us to the second item on Bjoern's list: which mathematician made a conjecture about this algorithm? Some googling around turned up that it was Collatz, who said: This process will eventually reach the number 1, regardless of which positive integer is chosen initially.

This looks like an illustration of the halting problem, ie, is it possible to determine if this loop will ever terminate for all possible inputs? Avi already noticed that it's not easy to tell by looking at it. More googling turns up that there is a $500 prize available if you can solve the Collatz conjecture.
Back to top
Bjørn
Sun Feb 24 2008, 11:07PM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
That is 100% correct on both part 2 and 3. I came over this algorithm ages ago before the internet and I tried to solve the problem before I knew that it was a real problem that people had been struggling with since 1937.

I tried all numbers up to 2^32 on an 8 MHz computer without much luck and even ran it on a Transputer Link2 trying out a few billion really large numbers before I gave up the brute force method. I got so far that I was sure it would be interesting to do it with complex numbers to make a 2D map before I discovered that it was an old problem and that hundreds of really clever people have tried and failed to solve before me.

It illustrates the halting problem Link2 very well, if the brightest minds have spent 70 years trying to find out if a 4 instruction long program is faulty or not, what is the chance of proving the correctness of a real program? (A Turing machine Link2 is said to be faulty if it enters an endless loop)
Back to top

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.