Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Find the Gray code for an integer
| ResourceFunction["GrayCode"][n] gives the Gray code of the integer n. | 
Find the Gray code of an integer:
| In[1]:= | ![ResourceFunction["GrayCode"][14]](https://www.wolframcloud.com/obj/resourcesystem/images/392/392603a7-1e9d-46d3-8532-bf09565c732a/3b0e31c4e79c3a5c.png)  | 
| Out[1]= |   | 
GrayCode threads itself elementwise over lists:
| In[2]:= | ![ResourceFunction["GrayCode"][Range[25]]](https://www.wolframcloud.com/obj/resourcesystem/images/392/392603a7-1e9d-46d3-8532-bf09565c732a/08a15547e201e603.png)  | 
| Out[2]= |   | 
Visualize the sequence of values generated by GrayCode:
| In[3]:= | ![ListLinePlot[
 Table[FromDigits[ResourceFunction["GrayCode"][n], 2], {n, 100}]]](https://www.wolframcloud.com/obj/resourcesystem/images/392/392603a7-1e9d-46d3-8532-bf09565c732a/5c669b6eff57db19.png)  | 
| Out[3]= |   | 
Successive values differ in only one bit in the binary representation:
| In[4]:= | ![gc = PadLeft[ResourceFunction["GrayCode"][Range[15]], {15, 4}]](https://www.wolframcloud.com/obj/resourcesystem/images/392/392603a7-1e9d-46d3-8532-bf09565c732a/2ba02116959962c8.png)  | 
| Out[4]= |   | 
| In[5]:= | ![diff = Differences[gc]](https://www.wolframcloud.com/obj/resourcesystem/images/392/392603a7-1e9d-46d3-8532-bf09565c732a/4233c7393db45242.png)  | 
| Out[5]= |   | 
Visualize the differences:
| In[6]:= | ![ArrayPlot[diff, ColorFunction -> "TemperatureMap"]](https://www.wolframcloud.com/obj/resourcesystem/images/392/392603a7-1e9d-46d3-8532-bf09565c732a/1cbd3d2a4b52f19b.png)  | 
| Out[6]= |   | 
Create an animated fractal from the sequence generated by GrayCode:
| In[7]:= | ![ResourceFunction[
ResourceObject[
Association[
   "Name" -> "SimpleListAnimate", "ShortName" -> "SimpleListAnimate", "UUID" -> "fa4f1a81-dd00-480d-b3d3-edf987ea2c04", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "Create an animation from a list of expressions",
     "RepositoryLocation" -> URL[
     "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$\
e2dce3b063014c54a0f928097aae3b4a`SimpleListAnimate"], ResourceSystemBase -> Automatic]][
 Table[ListLinePlot[
   Table[FromDigits[ResourceFunction["GrayCode"][n], 2], {n, 2^m - 1}], Axes -> False], {m, 10}]]](https://www.wolframcloud.com/obj/resourcesystem/images/392/392603a7-1e9d-46d3-8532-bf09565c732a/60205c34b4cc7b17.png)  | 
| Out[7]= |   | 
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License