Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:46:50+00:00 2026-06-17T22:46:50+00:00

I’m working on a very basic Android app, and I need to update a

  • 0

I’m working on a very basic Android app, and I need to update a progress bar in correspondence to the loops traversal to find if an entered number is prime. The entered number must be of long data type. The plan was to traverse a loop from i = 2 to sqrt(number), and divide the entered number by i each iteration. If i divides the number, then the loop ends, and we decide it is NOT a prime.

Otherwise, the loop keeps going, meanwhile updating the progress. Here is a snippet of the pertinent code:

protected Void doInBackground(Long... params) {
String message;
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);

long lim = (long)Math.sqrt(params[0]);

for (int i = 2; i <= lim; i++) {
        if (params[0] % i == 0) {
    message = params[0] + " is not prime!";
    int myProgressFinal = 100;
        publishProgress(myProgressFinal);
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
    return null;
        }
        else {              
            int myProgress = i/lim * 100;
            publishProgress(myProgress);

            if (isCancelled())
                return null;
        }
    }

message = params[0] + " is prime!";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
return null;
}

}

My problem spot is the else statement:

else {              
    int myProgress = i/lim * 100;
    publishProgress(myProgress);

I have an integer i for the loop, showing my progression through the loop, and to find the percentage that I’ve advanced, I would have to divide by the max amount of the loop (lim = sqrt(number)) and then multiply by 100 to get the proper percentage. However, I can’t do this b/c myProgress is an integer, but lim is a long, and cannot be changed to int. Even if I make lim an int, I still have integer division giving me a wrong value (0 * 100). It won’t let me typecast one of them to double b/c myProgress has to be int in order to be passed through publishProgress(). Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:46:51+00:00Added an answer on June 17, 2026 at 10:46 pm

    For a start, if ‘i’ is going to range all the way up to lim, ‘i’ will need to be a long as well. Otherwise, once ‘i’ hits Integer.MAX_VALUE, the next iteration is going to cause overflow.

    I believe you want to declare i as a long, and in your else statement do

    “int myProgress = (int)((((double)i)/lim) * 100)”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.