JAVA
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class odiousNum
{
/*⚠️Challenge: how many odious numbers smaller than 10.000 can you find⚠️
odious numbers are non-negative numbers that have an odd number of ones in the binary expansion
# 21 = 10101 = 3
# 50 = 110010 = 3
Challenge: how many odious numbers smaller than 10.000 can you find?*/
public static void main(String[] args) {
int info = 0; //total odious numbers
for(int i = 0;i<10000;i++){
int b = 8192;//The highest binary for this challenge
int total_ones = 0;
int save = i;//This saves the for loop number incase it happens to be a solution
int num = i;//num is set to the for loops int i so that I can freely use it
while(num > 0){//This while is so that the number can be divided all the way down to zero
if(num >= b){//If the number is greater than the binary than we proceed to the following
num = num - b;//Subtracts the binary from the number to allow for further subtraction
total_ones++;
b = b/2;//Since binarys are just x2 i start with the highest binary and divide down each time and check for possible subtractions
}else b = b/2;//Same as above exept this doesnt add to the counter. This is incase the number is smaller than the binary
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run