use eratosthenes sieve to find prime - android

hello i want my app find prime between two number with sieve algorithm i am use this code but its not working well what can i do for improve this code to my goal please help me
public int[] GetPrimes() {
int[] primesData = new int[maxValue+1];
int[] results = new int[maxValue];
int i;
int result_count = 0;
for (i=MIN_VALUE; i<=maxValue; i++) {
primesData[i] = PRIME_UNKNOWN;
}
for (i=MIN_VALUE; i<=maxValue; i++) {
if (primesData[i] == PRIME_UNKNOWN) {
primesData[i] = PRIME_YES;
results[result_count++] = i;
int j;
for (j=i; j<=maxValue; j = j + i) {
primesData[j] = PRIME_NO;
}
}
}
int[] retval = new int[result_count];
for (i=0; i<result_count; i++) {
retval[i] = results[i];
}
tvresult.setText(retval.toString());
return retval;
}
}

The algorithm is correct. Likely you are launching it with wrong values as input.
Ensure to have MIN_VALUE set to value 2 and prepopulate retval with 1 i.e.:
int[] retval = new int[result_count+1];
retval[0] = 1;
for (i=0; i<result_count; i++) {
retval[i+1] = results[i];
For the printing issue:
tvresult.setText(Arrays.toString(retval));
Because retval.toString() just returns the object reference not the content. This is true for any Java array. Instead use Arrays.toString().

Related

for loop overextending index

I'm attempting to iterate through the EditText "name" and check the letters so see if they match any of the letters in the objects of the array uNamesList.
If they do I want to break out of the loop and return the placement of the object that had the matching letter. At the moment I am getting this error, anyone know what might be wrong?
java.lang.StringIndexOutOfBoundsException: length=9; index=9
uNamesList.add("bob");
uNamesList.add("mike");
uNamesList.add("sike");
uNamesList.add("othername");
uNamesList.add("name");
public int getName(EditText name) {
String text = name.getText().toString();
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
match = o;
break;
}
}
}
return match;
}
You get the text inside edit text in wrong way. it should :
public int getName(EditText name) {
String text = name.getText(); //get the text
int match = 10;
for (int i = 0; i < text.toString().length(); i++) { //get the length of the text
for (int o = 0; o < uNamesList.size(); o++) {
char cLetter = name.toString().charAt(i);
String uName = (String)uNamesList.get(o);
Okay, here's the edit for another problem. i dont know if this what you want.
public int getName(EditText name) {
int match = 9999;
String text = name.getText().toString();
boolean found = false;
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
match = o;
found = true;
break;
}
}
if(found) break;
}
return match;
}
The simplest way is just only call return when it found.
public int getName(EditText name) {
int match = 9999;
String text = name.getText().toString();
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
return o;
}
}
}
return match;
}
change your for loop to the following, you accessing name from the uNameList loop where the length might be different
for (int i = 0; i < name.toString().length(); i++) {
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
For EditText you need GetText()
name.toString() gives you the java desciption of the object EditText
You are getting a StringIndexOutOfBoundsException most likely due to this line:
char uLetter = uName.charAt(i);
i in this case can be greater than the length of uName

Android, Random do not repeat same number twice in a row

I need to fill a vector with integers, but I have some troubles, I need to fill it with random numbers, but not two numbers in a row. (ex. not like this: 1,4,4,3,5,9)
I made this code but it does not work well :
# first time loja=1;
but until the game : loja++;
int[] con;
Random Method :
private int nasiqim (int max){
Random nasiqimi = new Random();
int i = 0;
i=nasiqimi.nextInt(max);
return i;
}
Working Code :
int i;
con = new int [loja];
for (i=0; i<loja; i++)
{
con[i] = nasiqim(8);
if(i>0){
while(con[i]==con[i-1])
{
con[i] =nasiqim(8);
}
}
i++;
}
The results are like this:
1
1,4
1,4,1
1,4,1,4
1,4,1,4,1
5,3,5,3,5,3
5,3,5,3,5,3,5
And this is not what I need, I need the numbers to really random, not like this,
Will be great if list will be like this something : 1,5,6,7,3,0,2,4,1,0,2,3...
Thank you!!
private int[] con = null;
private final Random nasiqimi = new Random();
/**
* Test run random.
*/
#Test
public void testRunRandom() {
int loja = 10;
con = new int[loja];
for (int i = 0; i < loja; i++) {
int nextRandom = 0;
while (true) {
nextRandom = nasiqim(8);
if (i == 0 || con[i - 1] != nextRandom) {
con[i] = nextRandom;
break;
}
}
}
}
/**
* Gets the random.
*
* #param max the max
* #return the random
*/
private int nasiqim(int max) {
return nasiqimi.nextInt(max);
}
I've created a sample class
import java.util.*;
public class Foo {
static Random r = new Random();
static int[] con;
static int loja = 8;
private static int randomInt(int max) {
return r.nextInt(max);
}
public static void main(String args[]) {
int i;
con = new int[loja];
for (i = 0; i < loja; i++) {
con[i] = randomInt(8);
if (i > 0) {
while (con[i] == con[i - 1]) {
con[i] = randomInt(8);
}
}
}
System.out.println( Arrays.toString(con));
}
}
All variables are static, notice I get rid of the i++; increment at the end of the for loop.

How to generate random number with out duplicates using JSON array in android

I am new to android. I am doing quize app.I have one JSON array text file.how to generate random number with out repetation using JSON array in android..please help me
Thank you adv..
this is my sample code
public static JSONArray getQuesList()throws JSONException{
ArrayList<Integer> list = new ArrayList<Integer>(size);
for(i =size - 1; i >= 0; i--) {
//index = rnd.nextInt(list.size());
list.add(i);
}
Random rand = new Random();
while(list.size() > 0) {
index = rand.nextInt(list.size());
Object object = quesList.get(index);
quesList.put(index, quesList.get(i));
quesList.put(i, object);
Log.d("","Selected: "+list.remove(index));
}
return quesList;
Edited
QuestionActivity.java
Global variable
int[] quizarray = null;
Add two functions
private void createQuizIndex() {
int[] array = new int[QuizFunActivity.getQuesList().length()];
quizarray = new int[QuizFunActivity.getQuesList().length()];
for(int i = 0 ; i < QuizFunActivity.getQuesList().length() ; i++){
array[i] = i;
}
Random random = new Random();
int m = 0;
for (int n = array.length ; n > 0; n--){
int r = random.nextInt(n);
quizarray[m++] = array[r];
array[r] = array[n-1];
}
}
private int getIndexNum(int quesIndex2) {
return quizarray[quesIndex2];
}
Change every showQuestion
showQuestion(getIndexNum(quesIndex),review);
Call createQuizIndex() in line 80, and line 194 if "Retake" means another random quiz

How to sort numbers in TextViews and display them?

Hi can some one suggest me a sample example of how i can sort the textviews based on the numbers in textviews. I am able to get the text from the TextViews need to sort and place the lowest number first.
Thank you.
public void sortNumbers(View v) {
String[] numbers = new String[7];
numbers[0] = textView23.getText().toString();
numbers[1] = textView33.getText().toString();
numbers[2] = textView43.getText().toString();
numbers[3] = textView53.getText().toString();
numbers[4] = textView63.getText().toString();
numbers[5] = textView73.getText().toString();
numbers[6] = textView83.getText().toString();
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));
for (int i = 0; i < intValues.length; i++) {
Integer intValue = intValues[i];
//here I want to assign sorted numberes to the TextViews
}
}
So I have followed Jeffrey's advice. Here is the code which still doesn't work properly. What could be wrong?
Created an array of TextViews:
TextView[] tvs = new TextView[7];
tvs[0] = textView23;
tvs[1] = textView33;
tvs[2] = textView43;
tvs[3] = textView53;
tvs[4] = textView63;
tvs[5] = textView73;
tvs[6] = textView83;
Sorted the array and assinged new values to the TextViews:
Arrays.sort(tvs, new TVTextComparator());
textView23.setText(tvs[0].getText().toString());
textView33.setText(tvs[1].getText().toString());
textView43.setText(tvs[2].getText().toString());
textView53.setText(tvs[3].getText().toString());
textView63.setText(tvs[4].getText().toString());
textView73.setText(tvs[5].getText().toString());
textView83.setText(tvs[6].getText().toString());
And here is the Comporator class:
public class TVTextComparator implements Comparator<TextView> {
public int compare(TextView lhs, TextView rhs) {
Integer oneInt = Integer.parseInt(lhs.getText().toString());
Integer twoInt = Integer.parseInt(rhs.getText().toString());
return oneInt.compareTo(twoInt);
}
}
to sort your textViews, first put them in an array,
TextView[] tvs = new TextView[7];
tvs[0] = textView23;
tvs[1] = textView33;
// and so on
note that if you have handle to the parent container, you could easily build the array by using ViewGroup.getChildCount() and getChildAt().
now write a comparator for a text view,
class TVTextComparator implements Comparator<TextView> {
#Override
public int compare(TextView lhs, TextView rhs) {
return lhs.getText().toString().compareTo(rhs.getText().toString());
// should check for nulls here, this is NOT a robust impl of compare()
}
}
now use the comparator to sort the array,
Arrays.sort(tvs, 0, tvs.length, new TVTextComparator());
public void sortNumbers(View v) {
String[] numbers = new String[7];
numbers[0] = textView23.getText().toString();
numbers[1] = textView33.getText().toString();
numbers[2] = textView43.getText().toString();
numbers[3] = textView53.getText().toString();
numbers[4] = textView63.getText().toString();
numbers[5] = textView73.getText().toString();
numbers[6] = textView83.getText().toString();
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));
textView23.setText(intValues[0]);
textView33.setText(intValues[1]);
textView43.setText(intValues[2]);
textView53.setText(intValues[3]);
textView63.setText(intValues[4]);
textView73.setText(intValues[5]);
textView83.setText(intValues[6]);
}

Trying to set textview

Here is my class it goes in to infinite loop please check where I am going wrong ... I am trying to get id's of image view making it random and then trying to set text view with imageview's description
public class Object {
int ObectIds[];
LinearLayout HUDDisplay;
int HudDisplayText[] = {R.id.HUD_Text_Element1,
R.id.HUD_Text_Element2,
R.id.HUD_Text_Element3,
R.id.HUD_Text_Element4,
R.id.HUD_Text_Element5,
R.id.HUD_Text_Element6,
R.id.HUD_Text_Element7};
TextView[] text;
View v;
Object(Context context,View vs) {
super();
ObectIds = new int[8];
HUDDisplay=(LinearLayout)vs.findViewById(R.id.HUD_Display);
for (int i = 0; i < 8; i++) {
ObectIds[i] = (R.id.imageView1) + i;
Log.d("ImageView", "Image Id's " + ObectIds[i]);
}
randomize(vs);
setTextView();
}
public void setTextView()
{
for(int i=0;i<8;++i)
{
text[i] =(TextView) HUDDisplay.findViewById(HudDisplayText[i]);
text[i].setText(v.getContentDescription());
}
}
public void randomize(View vs) {
for (int i = 0; i < 8; i++) {
while (true) {
shuffleArray(ObectIds);
v = vs.findViewById(ObectIds[i]);
Log.d("Image", "Image Id's " + v.getId());
if (!v.isClickable()) {
v.setClickable(true);
break;
}
}
}
}
static void shuffleArray(int[] ar) {
Random rnd = new Random();
for (int i = ar.length - 1; i >= 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
}
Hey man I observed your code & found error in code :
Please compare following code with your code... Constructor
for (int i = 0; i < 8; i++) {
ObectIds[i] = **HudDisplayText[i]**;
Log.d("ImageView", "Image Id's " + ObectIds[i]);
}
You have a while(true) loop that you break from only if v is not clickable. What happens if v is clickable? Nothing in your code ever sets v to not clickable, and views by default are not clickable.
I notice you're using the Object class. Object is basically the root of which all classes extend. If you call super() in the constructor, it will call the super class constructor, which is Object as well... That might be the problem.
Try looking for tutorials on how to start with Java/Android, since you are also using variables names that are not recommended. E.g. in Java,:
- a Class starts with a Capital
- a variable, starts with lowercase
- a function starts with lowercase:
public class Captial
{
private int anIntegerStartsWithLowerCase;
private void functionsAreLowerCaseAsWell()
{
}
}
Also take a look at your loop... It looks like it is never ending

Categories

Resources