<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mywikibiz.com/index.php?action=history&amp;feed=atom&amp;title=Module%3AUlam_spiral</id>
	<title>Module:Ulam spiral - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mywikibiz.com/index.php?action=history&amp;feed=atom&amp;title=Module%3AUlam_spiral"/>
	<link rel="alternate" type="text/html" href="https://mywikibiz.com/index.php?title=Module:Ulam_spiral&amp;action=history"/>
	<updated>2026-06-15T21:58:27Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.3</generator>
	<entry>
		<id>https://mywikibiz.com/index.php?title=Module:Ulam_spiral&amp;diff=479846&amp;oldid=prev</id>
		<title>Zoran: Pywikibot 6.4.0</title>
		<link rel="alternate" type="text/html" href="https://mywikibiz.com/index.php?title=Module:Ulam_spiral&amp;diff=479846&amp;oldid=prev"/>
		<updated>2021-07-16T07:44:16Z</updated>

		<summary type="html">&lt;p&gt;Pywikibot 6.4.0&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;-- Generate Ulam spiral based on primes, fibonacci, triangular numbers etc.&lt;br /&gt;
-- Gts@el wiki, Sep. 2017&lt;br /&gt;
&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
-- determine whether number is prime&lt;br /&gt;
-- params: n (number)&lt;br /&gt;
-- return: boolean&lt;br /&gt;
local function is_prime(n)&lt;br /&gt;
    for i = 2, n^(1/2) do&lt;br /&gt;
        if (n % i) == 0 then&lt;br /&gt;
            return false&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
    return true&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- determine whether number is triangular&lt;br /&gt;
-- params: n (number)&lt;br /&gt;
-- return: boolean&lt;br /&gt;
local function is_triangular(n)&lt;br /&gt;
   x = (math.sqrt(8*n + 1) - 1) / 2&lt;br /&gt;
   return (x % 1 == 0)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- determine whether number is part of standard fibonacci sequence&lt;br /&gt;
-- params: n (number)&lt;br /&gt;
-- return: boolean&lt;br /&gt;
local function is_fib(n)&lt;br /&gt;
    if n == 0 then&lt;br /&gt;
      return true&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    phi = 0.5 + (0.5 * math.sqrt(5.0))&lt;br /&gt;
    a = phi * n&lt;br /&gt;
    return math.abs(math.floor(a+0.5) - a) &amp;lt; 1.0 / n&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- determine total divisors of a number&lt;br /&gt;
-- params: n (number)&lt;br /&gt;
-- return: int&lt;br /&gt;
local function divisors(n)&lt;br /&gt;
    limit = n;&lt;br /&gt;
    total = 0;&lt;br /&gt;
&lt;br /&gt;
    if n == 1 then&lt;br /&gt;
       return 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    for i = 1, limit-1 do&lt;br /&gt;
        if n % i == 0 then&lt;br /&gt;
            limit = n / i&lt;br /&gt;
            if limit ~= i then&lt;br /&gt;
               total = total + 1&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            total = total + 1&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return total&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- CSS border for cells in order to recreate spiral&lt;br /&gt;
-- params: row (number), column (number), size (number)&lt;br /&gt;
-- return string&lt;br /&gt;
function border(row, column, size)&lt;br /&gt;
&lt;br /&gt;
   local on = 'solid '&lt;br /&gt;
   local top, left, right, bottom = 'none ', 'none ', 'none ', 'none '&lt;br /&gt;
   local last = size - 1 &lt;br /&gt;
&lt;br /&gt;
   if column == math.floor(size/2) and row == column then&lt;br /&gt;
       bottom = on&lt;br /&gt;
   elseif row == math.floor(size/2) and column==row+1 then&lt;br /&gt;
       bottom=on&lt;br /&gt;
       right=on&lt;br /&gt;
   end &lt;br /&gt;
&lt;br /&gt;
   if row &amp;lt; column and column &amp;lt; last - row then&lt;br /&gt;
      bottom = on&lt;br /&gt;
   elseif row &amp;gt; column and row &amp;lt; last - column then&lt;br /&gt;
      right = on&lt;br /&gt;
   elseif row &amp;gt;= column-1 and row &amp;lt; last and row &amp;gt; size/2 then&lt;br /&gt;
      bottom = on&lt;br /&gt;
&lt;br /&gt;
      if column == row + 1 and column &amp;lt; last then&lt;br /&gt;
        right = on&lt;br /&gt;
      end &lt;br /&gt;
   elseif column&amp;gt;row and column &amp;lt; last then&lt;br /&gt;
      right = on&lt;br /&gt;
   end &lt;br /&gt;
&lt;br /&gt;
   result = top .. right .. bottom .. left&lt;br /&gt;
   result = result:gsub(&amp;quot;%s$&amp;quot;, &amp;quot;&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
   return result&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- convert values to wikitable. The wikitable string is preproccessed in frame.&lt;br /&gt;
-- params: data (dictionary), size (int), fontSize (int)&lt;br /&gt;
-- return: string object&lt;br /&gt;
local function wikitable(data, size, fontSize)&lt;br /&gt;
	local fontSize = '1' --em&lt;br /&gt;
    if tonumber(size) &amp;gt; 15 and tonumber(size) &amp;lt;=30 then&lt;br /&gt;
    	fontSize = '0.8'&lt;br /&gt;
    elseif tonumber(size) &amp;gt; 30 and tonumber(size) &amp;lt;= 45 then&lt;br /&gt;
    	fontSize = '0.6'&lt;br /&gt;
    elseif tonumber(size) &amp;gt; 45 and tonumber(size) &amp;lt;= 60 then&lt;br /&gt;
    	fontSize = '0.4'&lt;br /&gt;
    elseif tonumber(size) &amp;gt; 60 then   &lt;br /&gt;
    	fontSize = '0.2'&lt;br /&gt;
    end&lt;br /&gt;
	&lt;br /&gt;
	local result = '{| style=&amp;quot;font-size:' .. fontSize .. 'em;text-align:center;border-spacing:0px&amp;quot;'&lt;br /&gt;
&lt;br /&gt;
    -- background colour of a number corresponding to number of divisors&lt;br /&gt;
    divi = {&lt;br /&gt;
                 [2] = '#8080ff',&lt;br /&gt;
                 [3] = '#80ff80',&lt;br /&gt;
                 [4] = '',&lt;br /&gt;
                 [5] = '',&lt;br /&gt;
                 [6] = '',&lt;br /&gt;
                 [7] = '',&lt;br /&gt;
                 [8] = '',&lt;br /&gt;
                 [9] = '',&lt;br /&gt;
               }&lt;br /&gt;
&lt;br /&gt;
    -- create wikitable&lt;br /&gt;
	for row=0, size-1 do&lt;br /&gt;
		result = result .. '\n|-\n|'&lt;br /&gt;
		for column=0, size-1 do&lt;br /&gt;
            if column &amp;gt; 0 then&lt;br /&gt;
               result = result .. '||'&lt;br /&gt;
            end&lt;br /&gt;
&lt;br /&gt;
            number = data[row .. ':' .. column]&lt;br /&gt;
            bgcolor =''&lt;br /&gt;
&lt;br /&gt;
			divtotal = divisors(number)&lt;br /&gt;
            if divtotal &amp;gt;= 47 then&lt;br /&gt;
              bgcolor = '#ff8080'&lt;br /&gt;
            else&lt;br /&gt;
              bgcolor = tostring(divi[divtotal])&lt;br /&gt;
            end&lt;br /&gt;
            &lt;br /&gt;
            result = result .. 'title=&amp;quot;' .. math.floor(number) .. '&amp;quot; style=&amp;quot;border-color:#9e9e9e;border-size:1px;border-style:' .. border(row, column, size) ..  ';background-color:'.. bgcolor .. ';color:black&amp;quot;|'&lt;br /&gt;
            --result = result .. 'title=&amp;quot;' .. math.floor(number) .. '&amp;quot; style=&amp;quot;border-radius:50%;background-color:'.. bgcolor .. ';color:black !important&amp;quot;|'&lt;br /&gt;
&lt;br /&gt;
            number = math.floor(data[row .. ':' .. column])&lt;br /&gt;
 			--if bgcolor ~= '' and bgcolor ~= 'nil' then&lt;br /&gt;
 				result = result .. '[[' ..	number .. ' (number)|' .. number .. ']]'&lt;br /&gt;
            --else  		&lt;br /&gt;
 			--    result = result .. number&lt;br /&gt;
 			--end&lt;br /&gt;
        end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	result = result .. '\n|}'&lt;br /&gt;
&lt;br /&gt;
    -- preprocess string in frame and render table&lt;br /&gt;
    local frame = mw.getCurrentFrame()&lt;br /&gt;
 	result = frame:preprocess(result)&lt;br /&gt;
&lt;br /&gt;
	return result&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- calculate ulam spiral value for each x,y tabular point.&lt;br /&gt;
-- algorithm concept based on Python version at GPL 3 https://rosettacode.org/wiki/Ulam_spiral_(for_primes)#Python&lt;br /&gt;
-- use of bitwise ops was attempted but Mediawiki Scribunto Lua doesn't seem to like them&lt;br /&gt;
-- params: n (int), x (int), y (int), start (int)&lt;br /&gt;
-- return: int&lt;br /&gt;
local function cell(n, x, y, start)&lt;br /&gt;
	local d = 0&lt;br /&gt;
&lt;br /&gt;
	local x = x - math.floor((n - 1) /2)&lt;br /&gt;
	local y = y - math.floor(n / 2)&lt;br /&gt;
&lt;br /&gt;
	l = 2 * math.max(math.abs(x), math.abs(y))&lt;br /&gt;
&lt;br /&gt;
	if y &amp;lt;= x then&lt;br /&gt;
		d = (l*3) + x + y&lt;br /&gt;
	else&lt;br /&gt;
		d = l - x - y&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return math.pow(l - 1, 2) + d + start - 1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- prepare spiral display for output type (i.e. wikitable, coords only, etc)&lt;br /&gt;
-- params: size (int), start (int)&lt;br /&gt;
-- return: mixed&lt;br /&gt;
local function show_spiral(size, start)&lt;br /&gt;
   local result = {}&lt;br /&gt;
&lt;br /&gt;
   for i=0, size-1 do&lt;br /&gt;
       for x=0, i do&lt;br /&gt;
         for y=0, i do&lt;br /&gt;
           result[x .. ':' .. y] = cell(size, x, y, start)&lt;br /&gt;
         end&lt;br /&gt;
      end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   return wikitable(result, size)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- main&lt;br /&gt;
-- params: frame&lt;br /&gt;
-- return: mixed&lt;br /&gt;
function p.ulam(frame)&lt;br /&gt;
	local size = tonumber(frame.args[1])&lt;br /&gt;
	local start = tonumber(frame.args[2])&lt;br /&gt;
    &lt;br /&gt;
    if size &amp;gt; 75 then&lt;br /&gt;
       return '&amp;lt;span style=&amp;quot;font-family:Roboto;&amp;quot;&amp;gt;The maximum allowed size is 75x75&amp;lt;/span&amp;gt;'	&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
	return show_spiral(size, start)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Zoran</name></author>
	</entry>
</feed>