Skip to main content

Playing with prime numbers

A prime number is a number that is divisible only by itself, for example the first few prime numbers are 2,3,5,7,11,13,17,.....
The straightforward way to find all prime numbers is to iterate through the numbers and see each number meets a specific criterion.
The program does this by,
  1. Check if the number ends with a 2,4,5,6,8 or a 0. If this is the case it's not prime.
  2. Check if the number is divisible by 3 or 7, then it's not prime
  3. These 2 conditions alone doesn't suffice. For example take the number 1079, this is divisible by 13, so we have to check if each number can be divided by any other prime numbers which are lower than the number itself. But there's a shortcut. We don't have to check with every prime number lower than the number. Take the prime number 37. We take it's square root rounded down, which is 6. Then we check the prime numbers only upto 7 because otherwise we do unnecessary checks, e.g. we can check 2*6. 3*6, 5*6 but as soon as we go 7*6 that is larger than the number 37 so it's useless. In other words the maximum number that a prime number should be multiplied with is the square root of the number.
http://www.counton.org/explorer/primes/checking-if-a-number-is-prime/


Rendering...

Comments

Popular posts from this blog

Detaching a process from terminal - exec(), system(), setsid() and nohup

Linux processes are created by fork() and exec(). The very first process of POSIX systems is init and subsequent processes are derived from the init as parent. These subsequent processes are child processes. During forking the parent process copies itself - with all I/O, address space, stack, everything. The only thing that is different is the process ID. The parent and child will have 2 different process IDs. The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows: execl("/bin/sh", "sh", "-c", command, (char *) 0); system() returns after the command has been completed. system() executes a command specified in command by calling /bin/sh -c command , and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.  system() calls are often made within programs to execut

Vaadin vs GWT

From Chapter 1 of book of Vaadin I quote the following. Vaadin Framework is a Java web application development framework that is designed to make creation and maintenance of high quality web-based user interfaces easy. Vaadin supports two different programming models: server-side and client-side . The server-driven programming model is the more powerful one . It lets you forget the web and program user interfaces much like you would program a desktop application with conventional Java toolkits such as AWT, Swing, or SWT. But easier. While traditional web programming is a fun way to spend your time learning new web technologies, you probably want to be productive and concentrate on the application logic. The server-side Vaadin framework takes care of managing the user interface in the browser and the AJAX communications between the browser and the server . With the Vaadin approach, you do not need to learn and deal directly with browser technologies, such as HTML or JavaScript.

C++ Callbacks using function pointers vs boost bind +boost function

In C, the most common uses of callbacks are as parameters to library functions like qsort , and as callbacks for Windows functions, etc. For example you might have a library that provides some sorting functions but you want to allow the library user to provide his own sorting function. Since the arguments and the return values do not change depending on the sorting algorithm, this can be facilitated in a convenient manner using function callbacks. Callbacks are also used as event listeners. onMouseClick(), onTerminalInput(), onData(), onConnectionStatus(), onRead() are probably some examples you've already seen in different libraries. The libraries have these callback functions and the users of the library are supposed to implement them. The library has a function pointer to these functions and calls them on their event loop which will invoke the code of the inherited classes of the library user. The implementation of function pointers is simple: they are just "code p