Category Archives: Techniques

Một bài lập trình hay về số nguyên tố

Số nguyên tố là thứ quá đỗi quen thuộc với những ai học toán, cũng như đối với các lập trình viên. Ai học giải thuật đều đã giải qua những bài về số nguyên tố, với độ khó dàn trải từ đơn giản tới phức tạp, từ vòng lặp thử tính chia hết tới sàng số nguyên tố Eratosthene…

Hôm nay mình rảnh thì có nghĩ ra một bài như thế này, các bạn xem qua nhé!

Đề bài: Cho hai số nguyên dương x và y (các test case đưa vào luôn đảm bảo x > y). Viết chương trình kiểm tra xem x2 – y2 có phải là số nguyên tố hay không.

Thoạt nhìn qua thì bài này khá đơn giản. Tính x2 – y2, rồi kiểm tra. Nhưng khi x và y lớn, ví dụ cỡ 1018 thì cách này sẽ chạy quá thời gian.

Hãy quan sát biểu thức x2 – y2. Nếu bạn đã học 7 hằng đẳng thức đáng nhớ, sẽ dễ thấy rằng x2 – y2 có thể được viết dưới dạng (x-y)(x+y). Vì x và y đều là các số nguyên dương, nên x 1 và y 1. Từ đó suy ra: (x+y) 1.

Xét biểu thức còn lại, x-y. Sẽ xảy ra hai trường hợp là (x-y) 1 và (x-y)=1. Với trường hợp đầu tiên, x2 – y2 sẽ không là số nguyên tố vì vi phạm định nghĩa (chỉ có hai ước là 1 và chính nó). Do đó với (x-y)=1, biểu thức ban đầu sẽ tương đương với chỉ x+y. Ta chỉ cần xét x+y là đủ. Sau đây là code miêu tả bài toán này.

bool primeSquare(long long a, long long b)
    if (a-b != 1) return false;
    long long num = a + b;
    if (num % 2 == 0) return false;
    for (long long i = 3; i*i <= num; i += 2)
        if (num % i == 0) return false;
    return true;

Lost Desktop icons after uninstalling Komorebi on Linux Mint

Komorebi is an application that enables animations for desktop wallpapers. When uninstall Komorebi, there is a possibility that desktop will become unresponsive: desklets are still displaying but no icons, right click doesn’t show anything. To fix it, run this code in Terminal:

gsettings set org.nemo.desktop show-desktop-icons true

Thing should be fine after this.

Best way to install Node.js and NPM

I’m writing this post as I need to install node.js and npm to install packages for reveal.js, I’m on Ubuntu 18.04 Bionic.

Normally we can install node.js via terminal by

sudo apt install nodejs

But this is not the best way since the version is outdated and some dependencies won’t meet. The best way to install the latest node.js and npm is via nvm.

curl -o- | bash

Note that the version number can be checked by going directly to the website and see. The above script will download all the package and put nvm into the Linux alias (so we can use the nvm command globally). After this, log out and log in to activate the alias.

Once you log in, run below script

nvm install node

To update:

nvm install node
nvm alias default nodenvm install node
nvm alias default node

From this answer:

Change Footer Text in Slide Master in Powerpoint 2016 and later

It is normal that we use a presentation more than one time for different conference. Anytime doing that, modifying the footer, where the conference’s name and the date are, is an important job to do to avoid embarrassing moment.

It is pretty obvious that you can simply open Slide Master and edit directly the footer text to whatever you want. However, for Powerpoit 2016 and later, when you do that and return to the slide, the old text content is still there and the modification you’ve done is applicable only for new slide.

In order to edit that, now what we should do is manually edit them. Not slide by slide but by Insert -> Header & Footer -> Modify the content, then click “Apply to All”.

It’s a nuisance but we can solve it by that trick.

Proper set-up of Zotero to manage your bibliography

Anyone who works in academy world would hear at least once about using Zotero to manage the reference. In this post I will show you how to install Zotero and set it up properly to max out its capacity by connecting increasing the storage size, connect with sci-hub, and how to grab a paper from a journal in just one click. I mention only Windows platform, but on Linux it is pretty similar.

Where to find Zotero and how to use them?

Well Google is your friend and installing Zotero is as easy as installing other software on a Windows computer. Here is the homepage of the Zotero we are talking about:

(by the time I paste that link, I realize Zotero has just introduced a new tool called Zoterobib for fast crafting a abstract. Nice work Zotero!).

Just download the file and install on Windows platform, as well as the Firefox add-on because you will need that.

Download PDF to local computer using Zotero

If you installed Zotero on your lab computer, chance is it will be easy to grab a PDF when you are browser those journals website. If your institute has access to the source, just click the button of the add-on in Firefox while Zotero in running to let it connect to the database and make a copy on your local computer for you. Next time you can navigate inside Zotero and read the PDF however you like.

If you install on your personal computer or if you connect to a paper that your school does not have access, then you have few other choices to get the PDF: (a) message directly the corresponding author of the paper, normally they will reply you very fast if you have interest in their work (we are! we are really happy when there is someone want to read what we are doing!); (b) if you cannot wait, there is the site sci-hub where you can search and download the paper for free, then go to Zotero and attach the PDF to the paper query in Zotero; (c) or you can do the b step automatically by modifying a bit of how Zotero searching for the PDF, thanks to a great tip by Simon on medium. Using (b) and (c) may be illegal in some countries so prepare to secure your connection (tip: don’t worry much, people just don’t care).

Sync your file to your personal or 3rd party storage service (WebDav)

Zotero itself supports free user with 300MB to store and sync the paper they collect across all server and to be fair this is enough. However when you are working with a big database, it is advised to use a WebDav (Dropbox, GDrive…) to sync your files there. I advise you to use pcloud service because they offer 10GB and it is very easier to configure. Guys at Berkeley University wrote a clear instruction to do that. Enjoy your fast 10GB!

Sync file with your Android phone

Last info I want to share is bringing Zotero to Android phone. The situation we may encounter is there is possibility that you want to read again a paper from your library when you are in a seminar without your laptop, or you are on a bus and you cannot wait till home to open and grab the idea. So let us install a connector of Zotero on Android phone to make it work.

There are several apps who help us do that trick. Although they are still having limits, but basically they do the job: connect to Zotero, sync the library and download the PDF. I recommend ZotEZ², don’t let the rating fool you, it really depends on what you want, if you just want to look at the library the most simple way and want to download the PDF and read on your phone, this app deserves a 5-star rating. It is free and with some in-app purchases you can unlock other utilities which is probably useful for you with a rather OK price (~5 euros).


I shared what I am using for my research. Few free to add some more comments if you know Zotero can do more or there is better option to do those tasks (I know there is!). Hope the provided information helps you and your research work. May the force be with you!

Use conda package in Sublime Text to switch between Python Environment

More and more people are using Anaconda for their Python needs. One important thing to notice is that Python has two version 2.7 (supported till 2020) and 3.6. Some packages are still using Python 2.7, also, when you install a suite of program for certain purpose (data analysis, image processing…), creating one environment for each purpose is a wise move.

Sublime Text (ST) is a very fast and intuitive text editor, it supports Win/Linux/Mac. We can download and use it for free (with some notification about purchasing), or we can buy a license ($80) at the time I write this blog post.

Running a Python script in ST is simple. You edit the script, choose the build system as Python, then Ctrl + B or choose Build to run the script. However, this would work only when you define Python Path for the system. How about those environment you created in Anaconda? You can do that easily by the package in ST, named CONDA.

To install CONDA, you need Package Manager inside ST. The complete guide can be found here. Then in ST, you should choose the Build System as Conda instead of Python. Now every time to want to activate the right environment, in ST just press Ctrl + Shift + P (to open the Package Option Panel) then type in CONDA, all the possible options are there.

Sort by moving elements to start or end with fewest moves

Getting started

You may be familiar with quick-sort, but when you stumble across this problem, it turns out to be a lot more complex. Since you need to determine the method that involves the fewest moves possible, you will have to take another approach. The given problem can be re-stated into this problem:

Problem statement

Hien is the class monitor and he wants his classmates to form a line, in which the height of every students is in ascending order. He needs to form that line by moving his classmates from the line to the start or end of it and it has to be a quick process, since Hien is very lazy and needs to play Age Of Empires right away. Write an algorithm to help him.

Input format

First line: n.

Second line: numbers indicating the height of every student in the class, each seperated by a space.


n ≤ 100; H[i] ≤ 100000 (H[i] is the height of an individual).

Output format

An integer indicating the fewest moves possible.

Sample input


2 1 3 5

Sample output



The student with height 1 is moved to the start of the line.

Let’s not pay attention to the ‘fewest moves’ for a while. Normally, when you see these types of ‘moving’ elements to start or end of an array, you can take a look at a basic approach.

Let’s take the Sample input as an example. With the basic approach, we search for the smallest element in the array, which is now 1. After that, we move it to the far right of the array. Then, we search for the next smallest element, which is 2, and we keep doing it until 5 is moved to the far right of the array. We come to the conclusion that for this approach, the number of moves that are taken is exactly equal to the number of elements present in the array itself. But let’s have a closer look. We can see that 2, 3 and 5 are contiguous, meaning that the relative order between them is not changed at all when sorting is completed. So, we can know that in the required algorithm, we need to conserve the order of contiguous integers. That is when std::pair comes to use.

Basic approach illustration

What is std::pair, exactly?

Std::pair is a pre-defined class in C++. A pair element is consisted of 2 other sub-elements, which can be classified as first and second. In the algorithm we are searching for, as stated earlier, we can utilise std::pair to get the job done by assigning the input elements to first and its index to second.

Get the job done

After that, quick-sort comes in handy. We then use it to sort the array in ascending order. Because std::pair is used, when sorting the elements, each index is carried along with the data. Then, we can just compare the indexes of every subsequent element. Job done!

Final approach illustration

Source code (C++)

List of free stock photos – Update 2019

A well-picked picture can play an important role to a blogpost or a document in general. However, the search for a good one requires effort (taking photos yourself, finding a good one on the internet) or money (stock market). Fortunately, there are free websites and nice people who are willing to provide their photos for free (or almost free), here is the update list of 2019 which I’m currently using: Continue reading List of free stock photos – Update 2019