Skip to main content

PrimeTest


After reading Uncle Petros and Goldbach's Conjecture, I wanted to write a program that would break an even number into two primes. However, I also decided to add functionalities like prime tests and my favourite functionality of all time - Real Random Generator (here: Real Random Prime Generator).

Part one: Primality Test



Depending on the size of the input string, two algorithms were implemented:


try {
 long inputhere = Long.parseLong(input);
 if (isPrime(inputhere)) {

 } else {

 }
}

catch (NumberFormatException ee) {
 if (probablePrime(input)) {

 } else {

 }
}

If the input string can be parsed into a long type, the following method was used:


boolean isPrime(long n) {
 if (n < 2)
  return false;
 if (n == 2 || n == 3)
  return true;
 if (n % 2 == 0 || n % 3 == 0)
  return false;
 long sqrtN = (long) Math.sqrt(n) + 1;
 for (long i = 6L; i <= sqrtN; i += 6) {
  if (n % (i - 1) == 0 || n % (i + 1) == 0)
   return false;
 }
 return true;
}

If not, the input was cast into a BigInteger type and the following method was used:


boolean probablePrime(String input) {
 return new BigInteger(input).isProbablePrime(10);
}

This way. the app can take care of any input size.

Part two: Goldbach's prime pair

 



  According to the famous Goldbach's Conjecture, Every even integer greater than 2 can be expressed as the sum of two primes. This part of the app allows the users to break an even number into two primes. If the input is less than or equal to 50000, it will also list all distinct sum of two prime that make up the number. Using Sieve of Eratosthenes and looping through n/2 number of primes before the input, if prime+x=input, where x is another prime was satisfied, a pair (prime, x) would satisfy Goldbach's conjecture.

Part three: Random Prime Generator


This part of the app allows users to generate random primes. Prime number less than a million was stored in a String array. Then the index of the array was randomly generated. If the user's device has an active internet connection, the user can choose to generate a real random prime number. The index for the real random prime number was imported from Random.org's integer generator.


public int realRandom() throws Exception {
 String sendto = "http://www.random.org/integers/?num=1&min=0&max=78497&col=1&base=10&format=plain&rnd=new";
 URL url = new URL(sendto);
 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
 return (Integer.parseInt(in.readLine()));
}

Popular posts from this blog

SalsaNight

Zorgania

The Zorganian Republic has some very strange customs. Couples only wish to have female children as only females can inherit the family's wealth, so if they have a male child they keep having more children until they have a girl. If they have a girl, they stop having children. What is the ratio of girls to boys in Zorgania?
The ratio of girls to boys in Zorgania is 1:1. This might be a little counter-intuitive at first. Here are some ways of tackling this problem. 1. Monte Carlo Simulation: Although, Monte Carlo simulation does not necessarily show why the result is 1:1, it is appropriate because of the very counter-intuitive nature of the problem. At the very least, it helps us see that the result is indeed 1:1. Therefore, this is a good start.
The following R code estimates the probability of a child being a boy in Zorgania. 
couples <-100000 boycount <-0for (i in1:couples){ # 0: boywhile (sample(c(0,1),1) ==0) { boycount=boycount+1 } } probability <- boycount/(co…

Simple Launcher

A simple minimal launcher application for Android devices that shows battery percentage using lzyzsd's CirclProgress library (ArchProgress used in this case) and BroadcastReciever for battery state, Android's clock widgets, a built-in flash light switch and an app list view that can be toggled. Currently, the toggle simply filters all the app that I am working on at present. Future implementation can allow users to select their favorite apps or populate second toggle based on the most used applications.