I am developing a small Prime Number application for Android devices and am nearly done, however I would like some help with optimizing my factorization class.
I am still having one or two problems with some large numbers(Even Numbers) being factored within a reasonable amount of time. I won't be able to use the sieve of Eratosthenes for this particular project I think as I can only sieve up to 10 million without the app crashing on my physical device (Samsung Galaxy S4 Mini). So my work around algorithm is below. I am not sure if I can maybe make the Pollard Rho algorithm that I implemented any better.
Once I have established that the number being tested isn't prime or isn't a prime square, I quickly do trial division up to 10 000, after that if the number still isn't factored completely I use the Pollard Rho method to reduce it the rest of the way.
I want to be able to factor numbers in the range of 2 > 2^64.
This is an example of a number taking roughly 15 seconds 256332652145852
It's factorization is [2, 2, 1671053, 38348971].
Any help would be gladly appreciated.
try {
long num = Long.valueOf(input);
if(num == 1) {
return "1" + " = " + input;
} else if(num < 1) {
return "Cannot factor a number less than 1";
} else if(PrimeNumbers.isPrime(num) == true) {
return result = num + " is a Prime Number.";
} else if(isSquare(num) == true && PrimeNumbers.isPrime((long) Math.sqrt(num)) == true) {
return result = (int) Math.sqrt(num) + "<sup><small>" + 2 + "</small></sup>" + " = " + input;
} else {
factors(num, pFactors);
return result = exponentialForm(pFactors, num) + " = " + input;
}
} catch(NumberFormatException e) {
return result = "Unfortunately the number entered is too large";
}
}
public static void factors(long n, ArrayList<Long> arr) {
long number = trialDiv(n, arr);
if(number > 1) {
while(true) {
long divisor = pollard(number, 1);
if(PrimeNumbers.isPrime(divisor) == true) {
number /= divisor;
arr.add(divisor);
if(PrimeNumbers.isPrime(number) == true) {
arr.add(number);
break;
}
}
}
}
}
private static long trialDiv(long n, ArrayList<Long> arr) {
while(n % 2 == 0) {
n /= 2;
arr.add((long) 2);
}
for(long i = 3; i < 10000; i += 2) {
if(PrimeNumbers.isPrime(i) == true) {
while(n % i == 0) {
arr.add(i);
n /= i;
}
}
}
if(PrimeNumbers.isPrime(n) == true) {
arr.add(n);
return 1;
}
return n;
}
public static long pollard(long n, long c) {
long x = 2;
long y = 2;
long d = 1;
while (d == 1) {
x = g(x, n, c);
y = g(g(y, n, c), n, c);
d = gcd(Math.abs(y - x), n);
}
if (d == n) {
return pollard(n, c + 1);
} else {
return d;
}
}
static long g(long x, long n, long c) {
long g = (((x * x) + c) % n);
return g;
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
Your pollard function is okay but not great. You are using Pollard's original algorithm, but it would be better to use Brent's variant. But that's probably not the source of your slow performance.
Your trial division function is slow. Checking each possible divisor for primality is very expensive, and not necessary. It doesn't matter if you divide by a composite; the division will always fail, but you don't waste the time checking primality. A better approach is wheel factorization.
Related
I wanted to automate android gradle versioning my requirement is
Ver Code Version Name
So i have runned code in java application
Code:
public static void main(String[] args) {
int fileVersionCode = 1;
String fileVersionName;
for (int i = 0; i < 200; i++) {
if (fileVersionCode <= 10) {
fileVersionName = "1." + (fileVersionCode - 1);
} else if(fileVersionCode>=20 && fileVersionCode%10 ==0) {
fileVersionName = ( (fileVersionCode / 10)) + ".9";
}else {
fileVersionName = (1 + (fileVersionCode / 10)) + "." +( (fileVersionCode % 10)-1);
}
System.out.println(fileVersionCode+" "+fileVersionName);
fileVersionCode++;
}
}
And my code is working as expected but in gradle for the same code
task firstTask {
doFirst {
int fileVersionCode = 1;
String fileVersionName;
for (int i = 0; i < 200; i++) {
if (fileVersionCode <= 10) {
fileVersionName = "1." + (fileVersionCode - 1);
} else if(fileVersionCode>=20 && fileVersionCode%10 ==0) {
fileVersionName = ( (fileVersionCode / 10)) + ".9";
}else {
fileVersionName = (1 + (fileVersionCode / 10)) + "." +( (fileVersionCode % 10)-1);
}
System.out.println(fileVersionCode+" "+fileVersionName);
fileVersionCode++;
}
}
doLast{
// println 'firstTask doLast'
}
}
Run :> gradlew firstTask
output is different why i am getting like this
In groovy a division results in a BigDecimal if the operands are of type Integer, while in Java they are of the type int.
When fileVersionCode is 11, the result:
(fileVersionCode / 10)
translates to 1 in Java but to 1.1 in Groovy. In order to fix this, just add a (int) cast to trim the unnecessary decimal part.
I'm trying to develop an Android application which will take the picture of an object and then detect the color of that object. I want to show user which color has the object. I've implemented the detecting color according to density and luminance with the help of answers to my question in this link:
What is the best way to implement Color Detection in Android?
At this point, I'm able to get color as a hex code. What I really want to do is that being able to inform user about which color is that hex code.
I don't want to limit my application just to detect main colors so I want it to detect many different colors.
How can I do this by using these hex codes?
Thank you in advance.
After a lot of effort to find out an efficient solution for my problem, finally I got it! Here below anyone who will face with this problem can find an alternative solution:
Generate a method to find closest color name which is in your database:
private String findClosedColor(String hexColor) {
int rgb[] = hexToRGB(hexColor);
int min = 3 * (int) pow(256, 2) + 1;
ArrayList<HashMap<String, String>> colorList = getColorList();
String colorName = null;
int i;
int len = colorList.size();
for (i = 0; i < len; i++) {
HashMap<String, String> map = colorList.get(i);
String colorCode = map.get("code");
Log.w("myApp", "HashMap'ten gelen colorCode:" + colorCode);
if (colorCode != null) {
int df = rgbDistance(hexToRGB(colorCode), rgb);
if (df < min) {
min = df;
colorName = map.get("name");
}
}
}
return colorName;
}
private int rgbDistance(int[] c1, int[] c2) {
return ( (int) pow(c1[0] - c2[0], 2)) + ((int) pow(c1[1] - c2[1], 2)) + ((int) pow(c1[2] - c2[2], 2));
}
private int[] hexToRGB( String hexCode)
{
int returnValue[] = new int[3];
if (hexCode.charAt(0) == '#')
{
hexCode = hexCode.substring(1);
}
if (hexCode.length() < 6)
{
returnValue[0] = -1;
returnValue[1] = -1;
returnValue[2] = -1;
}
else
{
int r = fromHex(hexCode.substring(0, 2));
int g = fromHex(hexCode.substring(2, 4));
int b = fromHex(hexCode.substring(4, 6));
returnValue[0] = r;
returnValue[1] = g;
returnValue[2] = b;
}
return returnValue;
}
private int fromHex( String n) {
n = n.toUpperCase();
if (n.length() < 2)
return -1;
int f1 = letterToCode(n.charAt(0));
int f2 = letterToCode(n.charAt(1));
if (f1 == -1 || f2 == -1) {
return -1;
} else {
return f1 * 16 + f2;
}
}
private int letterToCode(char n) {
switch (n) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case 'E': return 14;
case 'F': return 15;
default: return -1;
}
}
getColorList() function returns the color list from my database. With this solution, I can easily detect every hex code by choosing closer name in my database.
Best Regards to everyone...
I am writing a program, and user input ends up as a mathematical expression inside a string. How do I evaluate it into a double?
I don't have a lot of experience in this language, I am mostly familiar with BASIC(lol). So if anyone can give me the simplest step by step instructions to do this, it would be very much appreciated.
It is unfortunately not too straightforward in Java. The two top options seem to be using the built-in javascript engine or using the exp4j library.
You can read more about them in these answers: evaluating-a-math-expression-given-in-string-form and java-parse-a-mathematical-expression-given-as-a-string
You can try this program written in this answer.
https://stackoverflow.com/a/26227947.
I have copy pasted the code here. The explanation of the code is in the original answer if you follow the link.
public static double eval(final String str) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
return x;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+')) x += parseTerm(); // addition
else if (eat('-')) x -= parseTerm(); // subtraction
else return x;
}
}
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('*')) x *= parseFactor(); // multiplication
else if (eat('/')) x /= parseFactor(); // division
else return x;
}
}
double parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z') nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt")) x = Math.sqrt(x);
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
else throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char)ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}`
Ok. I guess you basically have a String like
String abc="(56+55(6/655)-5522*1222)";
and you want to evaluate this without changing its type.
Yes, there is a library available for this.
Library
Update The Library if there is any update
implementation 'com.udojava:EvalEx:2.7'
----------`
import com.udojava.evalex.Expression;
String memory="";
equal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
memory=editText.getText().toString(); //Get User's Entered Equation
Expression expression=new Expression(memory); //This Library Evaluate It
String abc=expression.eval().toString(); //Insert The Data into A String
textView.setText(abc); //Show The Data
}
});
So here is my code,
public class GameView extends SurfaceView {
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private List<Sprite> sprites = new ArrayList<Sprite>();
private long lastClick;
public int d = 0;
public int color;
TextView tv;
public int score;
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(new Callback() {
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
createSprites();
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format,
int arg2, int height) {
}
});
}
private void createSprites() {
int c = 10;
{
Random rnd = new Random();
color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256),
rnd.nextInt(256));
for (int b = 1; b <= c; b++) {
int random = (int) Math.ceil(Math.random() * 24);
if (random == 1) {
sprites.add(createSprite(R.drawable.bad1));
} else if (random == 2) {
sprites.add(createSprite(R.drawable.bad2));
} else if (random == 3) {
sprites.add(createSprite(R.drawable.bad3));
} else if (random == 4) {
sprites.add(createSprite(R.drawable.bad4));
} else if (random == 5) {
sprites.add(createSprite(R.drawable.bad5));
} else if (random == 6) {
sprites.add(createSprite(R.drawable.bad6));
} else if (random == 7) {
sprites.add(createSprite(R.drawable.bad7));
} else if (random == 8) {
sprites.add(createSprite(R.drawable.bad8));
} else if (random == 9) {
sprites.add(createSprite(R.drawable.bad9));
} else if (random == 10) {
sprites.add(createSprite(R.drawable.bad10));
} else if (random == 11) {
sprites.add(createSprite(R.drawable.bad11));
} else if (random == 12) {
sprites.add(createSprite(R.drawable.bad12));
} else if (random == 13) {
sprites.add(createSprite(R.drawable.bad13));
} else if (random == 14) {
sprites.add(createSprite(R.drawable.bad14));
} else if (random == 15) {
sprites.add(createSprite(R.drawable.bad15));
} else if (random == 16) {
sprites.add(createSprite(R.drawable.bad16));
} else if (random == 17) {
sprites.add(createSprite(R.drawable.bad17));
} else if (random == 18) {
sprites.add(createSprite(R.drawable.good1));
} else if (random == 19) {
sprites.add(createSprite(R.drawable.good2));
} else if (random == 20) {
sprites.add(createSprite(R.drawable.good3));
} else if (random == 21) {
sprites.add(createSprite(R.drawable.good4));
} else if (random == 22) {
sprites.add(createSprite(R.drawable.good5));
} else if (random == 23) {
sprites.add(createSprite(R.drawable.good6));
} else if (random == 24) {
sprites.add(createSprite(R.drawable.good7));
}
}
}
}
private Sprite createSprite(int resource) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);
return new Sprite(this, bmp);
}
#SuppressLint({ "WrongCall", "DrawAllocation" })
#Override
public void onDraw(Canvas canvas) {
canvas.drawColor(color);
Paint paint = new Paint();
paint.setColor(Color.CYAN);
canvas.drawText("SCORE " + score, 10, 10, paint);
for (Sprite sprite : sprites) {
sprite.onDraw(canvas);
}
}
// this is the ontouch event to destroy the sprites and make the blood splat
// effect
#Override
public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 200) {
lastClick = System.currentTimeMillis();
synchronized (getHolder()) {
float x = event.getX();
float y = event.getY();
for (int i = sprites.size() - 1; i >= 0; i--) {
Sprite sprite = sprites.get(i);
if (sprite.isCollition(x, y)) {
{
if ((sprites).equals (R.drawable.bad1))
score = score + 5;
else if ((sprites).equals(R.drawable.bad2))
score = score + 5;
else if ((sprites).equals(R.drawable.bad3))
score = score + 5;
else if ((sprites).equals(R.drawable.bad4))
score = score + 5;
else if ((sprites).equals(R.drawable.bad5))
score = score + 5;
else if ((sprites).equals(R.drawable.bad6))
score = score + 5;
else if ((sprites).equals(R.drawable.bad7))
score = score + 5;
else if ((sprites).equals(R.drawable.bad8))
score = score + 5;
else if ((sprites).equals(R.drawable.bad9))
score = score + 5;
else if ((sprites).equals(R.drawable.bad10))
score = score + 5;
else if ((sprites).equals(R.drawable.bad11))
score = score + 5;
else if ((sprites).equals(R.drawable.bad12))
score = score + 5;
else if ((sprites).equals(R.drawable.bad13))
score = score + 5;
else
score = score - 5;
}
d++;
if (d >= 10) {
d = 0;
createSprites();
}
break;
}
}
}
}
return true;
}
}
What I am trying to do is get,
if ((sprites).equals(R.drawable.bad1))
score = score + 5;
To check to see if somewhere in this code,
#Override
public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 200) {
lastClick = System.currentTimeMillis();
synchronized (getHolder()) {
float x = event.getX();
float y = event.getY();
for (int i = sprites.size() - 1; i >= 0; i--) {
Sprite sprite = sprites.get(i);
Holds the value of one of the pics that are being deleted, but I am not sure how to code this properly. I am not sure if I need to place the pics into an array each time the randomizer runs or what but the code is taken from the "edu4java" tutorial from youtube.
I have the program on a loop as you can tell that I can delete the pics on touch and the score was right I just am not sure how to get the,
if ((sprites).equals(R.drawable.bad1))
score = score + 5;
To check to "see" the proper pic string. Do I need to check the array that the code "auto creates"? Is there a way to check and see what the value of a string is? Such as "seeing" what is actually being "held" by "sprite" or "sprites" ?
One problem is that you do not supply the source code for Sprite, but perhaps it looks like the code here? Given the code there, there is no neat solution to your problem with the class as it is.
So, how I would approach solving this problem is to add to each sprite a resource ID:
private Sprite createSprite(int resource) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);
return new Sprite(this, bmp, resource);
}
Note that I add the extra resource parameter to the constructor. Furthermore, I would add to the Sprite class a method int Sprite.getResource(), so your collision detection code becomes:
if (sprite.isCollition(x, y))
{
if (sprite.getResource() == R.drawable.bad1)
score = score + 5;
else if (sprite.getResource() == R.drawable.bad2)
score = score + 5;
else ...
}
Note: this code is in no way optimal, but hopefully this will point you in the right direction to discover for yourself a better solution. Here in Stack Overflow we don't throw fishes, we teach people to fish.
You can't compare a bitmap with a resource Id, and actually trying to do it manually your self would end up in quiet exhaustive performance for a simple validation, what i would do and to keep it simple, i would create my own class that extends from Sprite, and in stead of passing the context and bitmap, i would pass the context and resource Id, then within this class i would decode the resource and keep a reference of the resource id, i would override the equal method from Sprites and use the reference used to create the object to do the comparison, this would be my class
public class MySprite extends Sprite{
private int bmpID;
public MySprite(Context context, int bmpID){
this.bmpID = bmpID;
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), bmpID);
super(context, bmp);
}
#Override
public boolean equals(Object o) {
if(!(o instanceof MySprite))return false;
return this.bmpID == MySprite.class.cast(o).getBmpId();
}
public int getBmpId(){
return bmpID;
}
}
This way i keep it as a simple int comparison, and most important you can use it to compare two objects of same bmpID, or something like what u wanted by doing this:
if ((sprites).getBmpId() == R.drawable.bad1))
score = score + 5;
Regards!
I am trying to use the Wifimanager to calculate the Signal Level of the access points found during a scan.
I am using the following method:
WifiManager.calculateSignalLevel(int, int)
But it appears to always return the same int no matter what the RSSI level is.
Here is my code:
public int calculateQoS(int aRSSI){
signalLevel = WifiManager.calculateSignalLevel(RSSI, 5);
return signalLevel;
}
public void testCalculateQoS(){
Log.d("signal", "signal = : "
+ connMonitor.calculateQoS(-44)
+ " " + connMonitor.calculateQoS(-80)
+ " " + connMonitor.calculateQoS(-120)
+ " " + connMonitor.calculateQoS(-20));
}
The logging outputs 1 for all the test cases for calculateQoS(int).
Am I missing something simple here? Why is the SignalLevel always 1?
It seems that calculateSignalLevel is implemented this way:
public static int calculateSignalLevel(int rssi, int numLevels) {
if (rssi <= MIN_RSSI) {
return 0;
} else if (rssi >= MAX_RSSI) {
return numLevels - 1;
} else {
int partitionSize = (MAX_RSSI - MIN_RSSI) / (numLevels - 1);
return (rssi - MIN_RSSI) / partitionSize;
}
}
Maybe this code snippet can explain your problem. Also note:
http://code.google.com/p/android/issues/detail?id=2555
thanks to this question I could prevent problem on lower API versions then the one I'm targetting. So I made this so you can use it on any platform version.
public int getWifiSignalStrength(Context context){
int MIN_RSSI = -100;
int MAX_RSSI = -55;
int levels = 101;
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
int rssi = info.getRssi();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
return WifiManager.calculateSignalLevel(info.getRssi(), levels);
} else {
// this is the code since 4.0.1
if (rssi <= MIN_RSSI) {
return 0;
} else if (rssi >= MAX_RSSI) {
return levels - 1;
} else {
float inputRange = (MAX_RSSI - MIN_RSSI);
float outputRange = (levels - 1);
return (int)((float)(rssi - MIN_RSSI) * outputRange / inputRange);
}
}
}//end method
This issue is only in android 2.3,
you can replace it with latest code of WiFiManger of an android 4.2
Here is the code:
public int calculateSignalLevel(int rssi, int numLevels) {
if(rssi <= MIN_RSSI) {
return 0;
} else if(rssi >= MAX_RSSI) {
return numLevels - 1;
} else {
float inputRange = (MAX_RSSI - MIN_RSSI);
float outputRange = (numLevels - 1);
if(inputRange != 0)
return (int) ((float) (rssi - MIN_RSSI) * outputRange / inputRange);
}
return 0;
}