Results 1 to 5 of 5
  1. #1
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default Checking whether an integer value has a certain integer in it.

    Say we have an array of integers 1 through 10000.
    We also have 3 instance variables; sum, sumWith8s, sumWithout8s.

    Sum is the sum of all the values 1 through 10000,
    sumWith8s is the sum of all the values 1 through 10000, which have 8 in them such as 8, 68, 801, 8888, etc...
    sumWithout8s is the sum of all the values that don't have 8s in them such as 1, 12, 242 etc...

    Any algorithms for this ?

  2. #2
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: Checking whether an integer value has a certain integer in it.

    One way you could do it is convert the int into a string and check if the string contains the number 8 (String.contains("8")). If it does contain 8 then add one to the sum8 variable otherwise add to the other one. Or when the loop is done take the sum of all numbers and subtract the sum of the numbers with 8's in them.

    It might not be the fastest or best way but it does the trick. (With this reply I'm expecting you to know how to get the sum of all the integers between 1 and 10000 if you don't post a reply).

    PS. I'm not a programming pro.

  3. #3
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default Re: Checking whether an integer value has a certain integer in it.

    yeah a simple for loop, thanks all i need to know

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,784
    Rep Power
    5

    Default Re: Checking whether an integer value has a certain integer in it.

    Iterative modular arithmetic mod 10 comes to mind.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Checking whether an integer value has a certain integer in it.

    Duh, you don't need a for loop for the first sub-problem: the sum of the number 1+2+3+ ... +n == n*(n+1)/2; if you subtract the sum of all numbers with an 8 in it from this number you get the sum of all numbers wihout an 8 in it ... with a bit of trickery you don't need any for loops at all.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2013, 07:49 PM
  2. Checking if String is added when integer required
    By wfsteadman in forum New To Java
    Replies: 4
    Last Post: 01-21-2013, 05:40 AM
  3. Replies: 2
    Last Post: 09-24-2012, 11:31 PM
  4. Integer Comparison, Outputting Largest Integer Not Working
    By killingthemonkey in forum New To Java
    Replies: 4
    Last Post: 10-16-2011, 08:59 PM
  5. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •