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

• 10-30-2013, 02:44 PM
rang3tastic
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 ?
• 10-30-2013, 03:28 PM
totalspelnerd
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.
• 10-30-2013, 03:40 PM
rang3tastic
Re: Checking whether an integer value has a certain integer in it.
yeah a simple for loop, thanks all i need to know
• 10-30-2013, 04:16 PM
jim829
Re: Checking whether an integer value has a certain integer in it.
Iterative modular arithmetic mod 10 comes to mind.

Regards,
Jim
• 10-30-2013, 04:25 PM
JosAH
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