Functions

I ran one billion on my desktop computer and got the result in a little under thirty seconds. When I ran ten billion, there apparently wasn't enough memory to handle the task and the system froze. After a hard shutdown, I couldn't log in. Windows 10 didn't recognize my pin.

Enter the upper limit for a pseudo-random number:


The number to evaluate: 10000

The prime factors of 10000 are: Enter the number in the text box to see the result.

Enter a number to see its prime decomposition:

The prime factors of 10000 are Enter the number in the text box to see the result..

Here's how the script begins:
import { onMount } from 'svelte';
let A = 10000;
let B = 10000;
let C = 10000;
let D = 10000;
let E = "Enter the number in the text box to see the result.";
var socket1, socket2, handleKeydown1, handleKeydown2; 

The work is done in a single function named "start" placed in "onMount." Here's the code:

onMount(() => {
    socket1 = new WebSocket('wss://schalk.site:3002');
    socket2 = new WebSocket('wss://schalk.site:3003');
    function start () {
        handleKeydown1 = function handleKeydown1 (e) {
            if (e.keyCode == 13) {
                A = e.target.value;
                socket1.send(e.target.value);
                socket1.addEventListener('message', function (e) {
                    B = e.data;
                    D = e.data;
                    socket2.send(e.data);
                    socket2.onmessage = function (e) {
                        C = e.data;
                        E = e.data;
                    }
                }); 
            };
        };
        function func ()  {
            let x = socket1.readyState
            if (x !== 1) {
                console.log("Repeat");
                setTimeout(() => {
                    func();
                },500)
            }
            else {
                socket1.send(10000);
                socket1.onmessage = function(e) {
                    B = e.data;
                    D = e.data;
                    socket2.send(e.data);
                    socket2.onmessage = function (e) {
                        C = e.data;
                        E = e.data;
                    }
                }
            }
        }
        func();
        handleKeydown2 = function handleKeydown2 (e) {
            if (e.keyCode !== 13) {console.log("e.keyCode isn't 'Enter'")}
            else if (e.keyCode == 13) {
                socket2.send(e.target.value);
                D = e.target.value;
                socket2.onmessage = function (e) {
                    E = e.data;
                    B = E;
                };
            };
        };
    }
    start();
}); 

Here's the HTML:

<h3>Enter the upper limit for a pseudo-random number:</h3>
<input type="text" on:keydown={handleKeydown1} />
<br>
<p>The number to evaluate: {D}</p>
<br>
<p>The prime factors of {D} are {E}.</p> 

The code isn't annotated, but the key to understanding it is that socket1 returns a pseudo-prine number and socket2 returns the prime factors of the integer it receives. The purpose of this page is to show one way of using WebSockets in SvelteKit.

The WebSocket servers run on a droplet separate from the droplet hosting schalk.net. They're on a droplet that services traffic destined for schalk.site. Connections to the servers can be established with, for example, "var sock = new WebSocket('wss://schalk.site')". It's striking how fast WebSockets traffic moves between Digitalocean droplets on the East Coast.

Here's the code for the random number server:

var ws = require('ws');
var https = require('https');
var fs = require('fs');
var j;

function randomInt (num) {
        return Math.floor(Math.random() * num);
}

const server = https.createServer({
  cert: fs.readFileSync('/etc/letsencrypt/live/schalk.site/cert.pem'),
  key: fs.readFileSync('/etc/letsencrypt/live/schalk.site/privkey.pem')
});
const wss = new ws.WebSocketServer({server});
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
          var number = JSON.parse(message);
          j = randomInt(number);
          ws.send(j);
  });

});

server.listen(3002); 

And here's the code for the prime decomposition server:

var ws = require('ws');
var https = require('https');
var fs = require('fs');
var j,k,n,arr,ar;

const server = https.createServer({
  cert: fs.readFileSync('/etc/letsencrypt/live/schalk.site/cert.pem'),
  key: fs.readFileSync('/etc/letsencrypt/live/schalk.site/privkey.pem')
});
const wss = new ws.WebSocketServer({server});

console.log("started web socket server...")

function pNums(n) {
    var store  = [], i, j, primes = [];
    for (let i = 2; i <= n; ++i) {
        if (!store [i]) {
            primes.push(i);
            for (j = i << 1; j <= n; j += i) {
                store[j] = true;
            }
        }
    }
    return primes;
}

function small (primes, v) {
    return primes.slice(0, primes.indexOf(primes.find(e => e > v)));
};

function primeNums (x) {
    return pNums(x);
};

function pfactors (prms, n) {
    var ar = [];
    prms.map(p => {
        while (n/p === Math.floor(n/p)) {
            ar.push(p);
            n = n/p;
        };
    })
    return ar;
}

var   sortFactors = ar => ar.sort(function(x,y) {
    return (x - y);
});

var g = x => {
        console.log("In g. x is", x);
    var primes = primeNums(x);
    return pfactors(primes,x);
}

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log("server2 incoming message is", message);
    var xx = g(message);
    var x = JSON.stringify(xx);
    ws.send(x);
  });
});
server.listen(3003);