Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Get a list of lucky numbers
| ResourceFunction["LuckyNumbers"][n] gives the lucky numbers less than or equal to n. | |
| ResourceFunction["LuckyNumbers"][n,k] gives the lucky numbers less than or equal to n, after iterating the sieving procedure k times. | 
Give all the lucky numbers less than 100:
| In[1]:= | ![ResourceFunction["LuckyNumbers"][100]](https://www.wolframcloud.com/obj/resourcesystem/images/6a3/6a341579-a22d-4fd5-b5f2-eb63a2bf55e2/2c0360513f4e37f4.png) | 
| Out[1]= |  | 
Lucky primes are primes that are also lucky numbers:
| In[2]:= | ![If[PrimeQ[#], Style[#, 18], #] & /@ ResourceFunction["LuckyNumbers"][100]](https://www.wolframcloud.com/obj/resourcesystem/images/6a3/6a341579-a22d-4fd5-b5f2-eb63a2bf55e2/491039bc82a7b795.png) | 
| Out[2]= |  | 
The first 300 integers surviving 6 steps of the sieving process:
| In[3]:= | ![ResourceFunction["LuckyNumbers"][300, 6]](https://www.wolframcloud.com/obj/resourcesystem/images/6a3/6a341579-a22d-4fd5-b5f2-eb63a2bf55e2/49026746bf3c6643.png) | 
| Out[3]= |  | 
Number of lucky numbers less than a million:
| In[4]:= | ![Length[ResourceFunction["LuckyNumbers"][1000000]] // Timing](https://www.wolframcloud.com/obj/resourcesystem/images/6a3/6a341579-a22d-4fd5-b5f2-eb63a2bf55e2/7d192d95ba5e1d0d.png) | 
| Out[4]= |  | 
Visualize the sieving procedure for lucky numbers step by step:
| In[5]:= | ![Grid[With[{r = Range[1, 60, 2]}, (ReplacePart[r, Thread[List /@ ( Complement[r, #1]/2 + 1/2) -> ""]] &) /@ Table[ResourceFunction["LuckyNumbers"][65, n], {n, 0, 5}]], Spacings -> .45]](https://www.wolframcloud.com/obj/resourcesystem/images/6a3/6a341579-a22d-4fd5-b5f2-eb63a2bf55e2/4f5959aab18032aa.png) | 
| Out[5]= |  | 
Distance between lucky numbers:
| In[6]:= | ![luckies = ResourceFunction["LuckyNumbers"][1000];](https://www.wolframcloud.com/obj/resourcesystem/images/6a3/6a341579-a22d-4fd5-b5f2-eb63a2bf55e2/68f128f42130b201.png) | 
| In[7]:= | ![ListLinePlot[Differences[luckies]]](https://www.wolframcloud.com/obj/resourcesystem/images/6a3/6a341579-a22d-4fd5-b5f2-eb63a2bf55e2/2cd4d345c5806d3f.png) | 
| Out[7]= |  | 
Highlight lucky numbers in an array of integers:
| In[8]:= | ![Grid[Partition[
  MapAt[Style[#1, Background -> RGBColor[1, 0.47, 0]] &, Range[500], List /@ TakeWhile[luckies, # < 500 &]], 20], ItemStyle -> {10}]](https://www.wolframcloud.com/obj/resourcesystem/images/6a3/6a341579-a22d-4fd5-b5f2-eb63a2bf55e2/58179703cef0e61f.png) | 
| Out[8]= |  | 
Compare lucky numbers with the prime numbers:
| In[9]:= | ![ListLogPlot[{luckies, Table[Prime[k], {k, Length[luckies]}]}, PlotLegends -> {"lucky numbers", "primes"}]](https://www.wolframcloud.com/obj/resourcesystem/images/6a3/6a341579-a22d-4fd5-b5f2-eb63a2bf55e2/6d9a10428c0c9948.png) | 
| Out[9]= |  | 
Select "twin" lucky numbers (pairs of lucky numbers that differ by 2):
| In[10]:= | ![Cases[Partition[luckies, 2, 1], {m_, n_} /; n - m == 2]](https://www.wolframcloud.com/obj/resourcesystem/images/6a3/6a341579-a22d-4fd5-b5f2-eb63a2bf55e2/1d5f5a2a719064d0.png) | 
| Out[10]= |  | 
Verify a version of the Goldbach conjecture for lucky numbers (an even number is a sum of two lucky numbers) for the first few even numbers:
| In[11]:= | ![Table[Catch[
  Table[If[MemberQ[luckies, n - luckies[[i]]], Throw[{n - luckies[[i]], luckies[[i]]}]], {i, 2, 100}]], {n, 4, 100, 2}]](https://www.wolframcloud.com/obj/resourcesystem/images/6a3/6a341579-a22d-4fd5-b5f2-eb63a2bf55e2/244ff1968331f7fe.png) | 
| Out[11]= |  | 
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License