Comments 0

Programming is an art of writing algorithm into computer language. So computer could help us to do something we supposed to do, like calculating or managing information. There are many computer languages may be used, each of them has own characteristic. You might know C, C++, Pascal, Basic, Python, PHP, Javascript, Java, C#, VBNet, Ruby etc. Which programming language do you able to use?

Sometimes any student who get study at Information and Technology faculty ask me why they must take Math into their roster? You know, many people do not like that subject. Math is not easy to understand, so they try to avoid that subject.

Unfortunately, I could not answer that question. The director of faculty might has any reason why Math is a must to be taken by student by taking into account of National Curriculum that must become a formal references to conduct each lesson.


Comments 0

Today, I was learning to create a function in Python 3. The interested problem I took was generating prime numbers below a certain number. As this is a mathematical subject, I really enjoyed while writing the program.

The prime numbers is a set of numbers those have only 1 and its number as divisor, eg: 2, 3, 5, 7, 11, 13, 17, 19, 23 and so on. Twelve (12) is not prime number, because it has 1, 2, 3, 4, 6 and 12 as divisor.

After trying several times, I got optimal simple algorithm (It should be any other algorithm that more optimal than mine version). The source program were written in Python 3 as follows:

import time

def getprime(pmax):
	primelist = list()
	check = 1
	while True:
		check += 1
		if check > pmax:

		isprime = True
		for p in primelist:
			if p**2 > check:
			if check % p == 0:
				isprime = False
		if isprime:

	return primelist

if __name__ == "__main__":

	pmax = int(input("Maximum = "))

	t1 = time.time()	
	primelist = getprime(pmax)
	t2 = time.time()

	for p in primelist:
		print(p, end=", ")

	print ("\n\r", t2-t1, "secs")
	print (len(primelist), "numbers")

For prime numbers below 2,500,000, I got 183,072 numbers for about 19.9 seconds of producing prime numbers list using my notebook with i3 processor.

I was not satisfied for this achievement. I tried to write a script using PHP 7 with similar algorithm as follows:


function getprime($pmax){
	$primelist = [];
	$check = 1;
	while (true) {
		if (++$check > $pmax) break;

		$isprime = true;
		foreach ($primelist as $p){
			if ($p**2 > $check) break;
			if ($check % $p == 0){
				$isprime = false;
		if ($isprime) $primelist[] = $check;
	return $primelist;

$pmax = readline("Maximum = ");

$t1 = time();
$primelist = getprime($pmax);
$t2 = time();

foreach($primelist as $p){
	echo $p .", ";
echo "\n".($t2-$t1)." secs";
echo "\n".count($primelist)." numbers";

Unbelievable, this script only need 3 seconds to generate similar prime numbers set.

So, if PHP-Cli is more faster than Python, why do I fall in love to Python programming language? The answer might be like in this article

New Algorithm to Make Faster

There are many algorithm to generate prime numbers list. I found a new algorithm that make me shocks because of speed increasing drastically. That method is using sieve of Eratosthenes as described here

def sieve_for_primes_to(n):
    size = n//2
    sieve = [1]*size
    limit = int(n**0.5)
    for i in range(1,limit):
        if sieve[i]:
            val = 2*i+1
            tmp = ((size-1) - i)//val 
            sieve[i+val::val] = [0]*tmp
    return sieve

print [2] + [i*2+1 for i, v in enumerate(sieve_for_primes_to(10000000)) if v and i>0]

Wow, it takes only 0.2 seconds to generate prime numbers below 2,500,000.

Categories PHP, Python

Comments 0

My friend told me that his gadget was missing. He asked me a help to find it. As a programmer I had to say “almost nothing impossible”. That jargon meant I would find an algorithm to solve any problem. But the result might be accepted or rejected. I have to try..

“Well, okay. You are programmer, aren’t you?” I asked to know his background.
“Yes, I am. I know much PHP,” he answered.
“Okay, I’ll send you an email this night,” I promised.

Then.. At the middle of the night I wrote a short script to help him, as follows:

// Dear friend,
// Try this script. It might work :)


function is_near($p, $q){
  $r = sqrt(($q[0]-$p[0])**2 + ($q[1]-$p[1])**2);
  return $r < 1;

function find_my_gadet($location){
  $x = rand();
  $y = rand();

  if (is_near(explode(",",$location), [$x, $y])){
    echo "play horay.mp3";
    return true;
  if (SavingAccount::balance > 3e6){
    $h = new Gadget();
    return $h;
  return new Hope("interval 1 year");

if (isset($_GET['location'])){
  $location = explode(",", $_GET['location']);
  $result = find_my_gadget($location);
} else {
  echo "Location must be set!";

I sent him as an email attachment as I promised, but never received his comment. :)

Categories PHP

Comments 0

Although being a root in recovery mode, we can not change anything. The partitions are mounted in read-only status. So, what should we do?

Recovery mode can be accessed by selecting “Ubuntu Advance Options” of grub menu shows while computer booting up.

In general, to change system in Linux, you have to be a root user. But unfortunately, Ubuntu prohibited us changing anything in recovery mode, despite we were logged as root user, cause of system mounting the partitions in read-only status (see A).

To overcome this, an easiest step is by selecting “fsck” menu (see B). After finished, you could then select “root” menu (see C) to access system without limitation.

Categories Linux

Comments 0

We are going to analyze: does random generator using rand() function in PHP return fair values? Just imagine, each side of coin and dice should have equal probability. So, we want to know if using rand() function to simulate tossing coin or dice was fair enough.

Generating Random Values

We could generate random values by creating a PHP script as follows:

//random.php size min max [outfile]
echo "Random Values Generator\n\n";
$argc = $_SERVER['argc'];
$script = $_SERVER['argv'][0];
if ($argc < 4){
	echo "Usage:\n\n";
	echo "{$script} size min max outfile\n\n"
		."\tsize\tNumber/size of figures generated\n"
		."\tmin\tMinimum value\n"
		."\tmax\tMaximum value\n"
		."\toutfile\tOutput filename, default screen\n";
$size = $_SERVER['argv'][1];
$min = $_SERVER['argv'][2];
$max = $_SERVER['argv'][3];
$outfile = @$_SERVER['argv'][4];
$output = "";
for ($i=0; $i<$size; $i++){
	$output .= round(rand($min, $max))."\n";
//save to output filename
if ($outfile){
	echo "Writing data to {$outfile} ..";
	if (file_put_contents($outfile, $output)!==FALSE) echo "Success\n";
	else echo "Failed\n";
} else echo str_replace("\n"," ",$output)."\n";

At terminal, we call the script by typing a command as follows to generate one hundred random values laid between 1 to 6.

$ php random.php 1000 1 6

We also could save the output to a file by specified filename

$ php random.php 1000 1 6 sampel.txt

Next, we proceed the output to check if the data generated were fair enough.

Analysing The Fairness

In my cases, I use R Statistics software to analyze the data. Here is the R script:

The histogram of random values displayed as follows:

Seems the data of each number 1-6 was similar with subsequent frequencies 179, 168, 159, 159, 162 and 173. By using ChiSquare Test procedure, we got p-value = 0.8491, high enough, meant all generated random values had equal probabilities.


Yes, we could use rand() function, as data generated by PHP script will follow Uniform Distribution.

Categories PHP, R Statistics