i have to find a solution for this task :
"Supermarkets are increasingly equipped with automatic cash registers. Most of these funds only accept payment by credit card, although a significant proportion of consumers still pay cash (with banknotes and coins).
One of the problems encountered with the cash payment is the rendering of money: how to make a given sum optimally, ie with the minimum number of coins and notes? This is a problem for every one of us every day, let alone the automatic cash registers.
In this exercise, i m asked to try to find an optimal solution to make change in a specific case: when an automatic cash register contains only 2 € coins, 5 € and 10 € banknotes.
To simplify the problem, we will consider that all these coins and bills are available in unlimited quantities.
Here are some examples of currency:
The return of money is expressed by a Currency object. This item has 3 properties: piece2, billet5 and billet10 which respectively store the number of coins of 2 €, 5 € tickets and 10 € tickets.
For example, if we take example 2 of the table (6 €), we should get a Money object with:
Piece2 worth 3 (3 pieces of 2 €)
Ticket5 is worth 0 (no ticket of 5 €)
Ticket10 is worth 0 (no ticket of 10 €)
Task : Implement the MonnaieOptimale(int s) method that returns a Currency object containing the coins and notes whose sum is s. If it is impossible to return the currency (as in example 1), return null.
To get it best the solution will always have to make change when possible and with the minimum number of coins and tickets."
Data: s is always a strictly positive integer (long) less than or equal to 9223372036854775807
the general structure of the solution is the following:
Suggestions ??
Firstly, simple greedy approach (always get the largest i.e.10) is not working here, as the system is not canonical, consider s = 11, you will get "Impossible" if you use coin 10, but it is possible with { 5, 2, 2, 2 }.
I have a slightly different idea here, using the parity of odd /even numbers:
If s is even, do the greedy approach but never uses coin 5
If s is odd, keep using coin 5 until s becomes even, then do step 1.
For eg: s = 18, solution = { 10, 2, 2, 2, 2 }. s = 23, solution = { 5, 10, 2, 2, 2, 2 }
Why it works, is because one can notice, for coin system { 2, 10 }, you can make any even s using greedy approach (it is canonical to even domain).
Plus the fact that for any odd number minus another odd number will make a even number, so for odd s we can use one coin 5 to make s even to reduce the problem.
From this logic, I think at most one 5 coin would be used for any s, and only when s = 1 or 3 the answer is "Impossible"
i solve it mannually like this:
public static Monnaie MonnaieOptimale(long s)
{
if (s<10)
{
if (s%2 =0) {Monnaie.billet10 = 0 ; Monnaie.billet5 = 0 ; Monnaie.piece2 = s/2 }
else if (s == 5) {Monnaie.billet10 = 0 ; Monnaie.billet5 = 1 ; Monnaie.piece2=0 }
else if (s == 7) {Monnaie.billet10 = 0 ; Monnaie.billet5 = 1 ; Monnaie.piece2=1 }
else Monnaie.billet10 = 0 ; Monnaie.billet5 = 1 ; Monnaie.piece2= 2
}
else
{
if (s%2 =0)
{
Monnaie.billet10 = Math.Floor(s/10);
if ((Monnaie.billet10 * 10) != s) {Monnaie.billet5 = 0 ; Monnaie.piece2= (s%10)/2 }
}
else
{
private lastDigit = s%10;
if (lastDigit ==1){Monnaie.billet10 = ((s/10)-1) ; Monnaie.billet5 = 0 ; Monnaie.piece2=3 }
else if (lastDigit ==3){Monnaie.billet10 = ((s/10)-1) ; Monnaie.billet5 = 0 ; Monnaie.piece2=4 }
else if (lastDigit ==5){Monnaie.billet10 = (s/10) ; Monnaie.billet5 = 1 ; Monnaie.piece2=0 }
else if (lastDigit ==7){Monnaie.billet10 = (s/10) ; Monnaie.billet5 = 1 ; Monnaie.piece2=1}
else {Monnaie.billet10 = (s/10) ; Monnaie.billet5 = 1 ; Monnaie.piece2=2}
}
}
}
using System;
public class Program {
public static void Main() {
int re10 = 0, re5 = 0, re2 = 0, s = 197;
while (s / 2 != 0) {
while (s / 5 != 0) {
while (s / 10 != 0) {
if (s % 10 % 5 == 0) {
re10 = s / 10;
s = s % 10;
}
else {
if (s / 10 > 1) {
re10 = s / 10 - 1;
s = s % 10 + 10;
}
break;
}
}
if (s % 5 % 2 == 0) {
re5 = s / 5;
s = s % 5;
} else
if (s / 5 > 1) {
re5 = s / 5 - 1;
s = s % 5 + 5;
}
break;
}
re2 = s / 2;
s = s % 2;
}
if(re5==3){re10+=1;re5-=2;}
Console.WriteLine("billet 10 :" + re10);
Console.WriteLine("billet 5 :" + re5);
Console.WriteLine("billet 2 :" + re2);
Console.WriteLine("Total : "re2 * 2 + re10 * 10 + re5 * 5);
}
}
billet 10 :18
billet 5 :3
billet 2 :1
Total : 197
Related
I have been trying to get this to work for 3 days, and I feel like I'm using the wrong approach, if anyone can correct me I will wax your car. Background, client asked to me make a simple pyramid algorithm. I want to select add everything to a list of objects and make everything on the left side true and everything on the right side false. Every other line reads the line 2 lines prior and adds multiple entries. The first time it adds a number like 1 it's one time, then it adds two 1's for each 1 until there is 4. So the first time it enters a 1 on line 1, then on line 3 it adds a 1 two times, then on line 5 it reads from line 3 and adds each of those 1's 2 times.
Here is a visual representation.
|1|
|2| |3|
|1|1| |4|5|
|2|2|3|3| |6|7|8|9|
|1|1|1|1|4|4|5|5| |10|11|12|13|14|15|16|17|
|2|2|2|2|3|3|3|3|6|6|7|7|8|8|9|9| |18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33
The order this list would be is:
1|2|3|1|1|4|5|2|2|3|3|6|7|8|9|1|1|1|1|4|4|5|5|10|11|12|13|14|15|16|17...
I keep getting close, but it fails to generate the correct output. `
for (int i = 1; i < 50; i = i * 2)
{
Response.Write(i.ToString() + " - ");
var previousLevel = (i / 2 / 2);
foreach (var oc in infoRows.Where(x => x.level == previousLevel))
{
for (int p = i; p > 0; p--)
{
Response.Write(oc.id + "*");
}
}
while (level <= i)
{
for (int r = 1; r <= i; r++)
{
InfoRow tempInforow = new InfoRow();
tempInforow.customerCode = GenerateCustomerNumber(position);
tempInforow.id = customerId;
tempInforow.sendtoidnumber = level.ToString();
tempInforow.status = 0; // GetStatus(position, totalCount);
tempInforow.position = position;
tempInforow.level = i;
infoRows.Add(tempInforow);
customerId++;
position++;
Response.Write(tempInforow.id + "-");
level++;
}
}
}
`
Essentially this generates the following:
1 - 1-
2 - 2-3-
4 - 1*1*1*1*4-5-6-7-
8 - 2*2*2*2*2*2*2*2*3*3*3*3*3*3*3*3*8-9-10-11-12-13-14-15-
16 - 4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*4*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*6*6*6*6*6*6*6*6*6*6*6*6*6*6*6*6*7*7*7*7*7*7*7*7*7*7*7*7*7*7*7*7*16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-
32 -
I've tried 30 different ways with switch statements, while statements, for and foreach statements, the closest I can get to this working is level 4.
Can someone suggest another way. Maybe a multidimensional array or idk what. Thank you.
Let's write the sequence down and have a look on what's going on:
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
seq 1 2 3 1 1 4 5 2 2 3 3 6 7 8 9 1 1 1 1 4 4 5 5 10 ...
^ ^ ^ ^ ^ ^
| |
if # is power of 2 (e.g. 8 == 2**3)
we should copy and double # / 4 items (here 8 / 4 == 2 items)
starting from # / 4 item (here 8 / 4 == 2, starting from item #2)
Time to implement this algorithm
Code:
using System.Linq;
...
private static List<int> Pyramid(int size) {
if (size < 0)
throw new ArgumentOutOfRangeException(nameof(size));
if (size <= 3)
return Enumerable.Range(1, size).ToList();
List<int> result = new List<int>(size) { 1, 2, 3 };
for (int value = 4; result.Count < size; )
if (BitOperations.IsPow2(result.Count + 1)) {
int chunk = (result.Count + 1) / 4;
for (int i = 0; i < chunk && result.Count < size; ++i) {
result.Add(result[chunk - 1 + i]);
if (result.Count >= size)
return result;
result.Add(result[chunk - 1 + i]);
}
}
else
result.Add(value++);
return result;
}
Demo:
// First 31 items from the pyramid
Console.Write(string.Join("|", Pyramid(31)));
Output:
1|2|3|1|1|4|5|2|2|3|3|6|7|8|9|1|1|1|1|4|4|5|5|10|11|12|13|14|15|16|17
I was tasked to solve this challenge on a programming exam, but I'm not sure that I have properly solved it. I am inquisitive about what really is the solution to this task:
Make an application that will tell the user to input 3 numbers, identify the sequence, and provide the 4th integer. It should prompt if no relations is found
Note: Possible operators may include addition, subtraction, multiplication, division, exponent, and modulo division.
Sample Output:
2, 4, 6, 8
10, 9, 7, 4
1, 4, 9, 16
1, 1, 2, 6
120, 120, 60, 20
Here is my solution so far:
using System;
using System.IO;
namespace TestQuestionnaire
{
class Program
{
static void Main(string[] args)
{
var numbers = new int[3];
string answer;
bool exit = false;
while (!exit)
{
// Loop for 5 times
for (int n = 0; n <= 5; n++)
{
// User Input
Console.WriteLine("Please Enter 3 Numbers.");
for (var i = 0; i < numbers.Length; i++)
{
var error = false;
while (!error)
{
Console.Write("Enter Number: ");
error = int.TryParse(Console.ReadLine(), out numbers[i]);
if (!error) Console.WriteLine("Invalid Number! Enter a Valid Number and Try Again. ");
}
}
NextNumber(numbers);
}
// Prompt for exit
Console.WriteLine("Finished! Try Again? Y/N");
answer = Console.ReadLine()?.ToUpper();
if (answer == "N")
{
exit = true;
}
}
}
private static void NextNumber(int[] numbers)
{
int next = 0;
int interval_one = 0;
int interval_two = 0;
// determine if first two number is descending, ascending or equal (addition and subtraction)
if (numbers[0] < numbers[1])
{
interval_one = (numbers[1] - numbers[0]);
if (numbers[1] > numbers[2])
{
interval_two = (numbers[1] - numbers[2]);
}
else
{
interval_two = (numbers[2] - numbers[1]);
}
//determine if constant interval (eg, 2,4,6,8 or 1,2,3,4) otherwise evaluate more
if (interval_one == interval_two)
{
next = numbers[2] + interval_two;
}
else
{
next = numbers[2] + interval_two + (interval_two - interval_one);
}
}
else if (numbers[0] > numbers[1])
{
interval_one = numbers[0] - numbers[1];
if (numbers[1] > numbers[2])
{
interval_two = (numbers[1] - numbers[2]);
}
else
{
interval_two = (numbers[2] - numbers[1]);
}
if (interval_one == interval_two)
{
next = numbers[2] - interval_two;
}
else
{
next = numbers[2] - interval_two + (interval_one - interval_two);
}
}
else
{
if (numbers[0]/1 == numbers[1] && numbers[1]/2 == numbers [2])
{
next = numbers[2] / 3;
}
else if (numbers[0] * 1 == numbers[1] && numbers[1]*2 == numbers[2])
{
next = numbers[2] * 3;
}
}
Console.WriteLine("Predicted 4th Number: {0}", next);
}
}
}
Thanks!
I just have an Idea but I hope this can help
using +
2 4 6 8
2 2 2
10 9 7 4
1 2 3
1 1
1 4 9 16
3 5 7
2 2
diffrent way(*)
1 1 2 6
1 2 3# now +
1 1
use '/'
120 120 60 20
1 2 3# use +
1 1
There is no way to recognize all sequences of numbers, as each is defined by a very different relation. You would essentially have to hardcode each case as listed in the examples. And even then, there is no one correct answer. For your fourth example, I could see the sequence 1 1 2 2 3 3... being just as reasonable as 1 1 2 6.... In fact, the Online Encyclopedia of Integer Sequences finds 35,000 results for the the sequence 1 1 2 (see https://oeis.org/search?q=1%2C1%2C2&sort=&language=english&go=Search)
Putting that aside though, I'll give some pseudocode for the examples (I'm assuming, given that you are fine with Python or C#, that the language itself is not particularly important, and that you probably already know how to do the "getting input" and "returning results" portions.).
if the difference between each number is equal, add that difference to the last number and return it
if the difference between each number is the sequence 1 2 3..., return the last number plus the next difference (4 in this case)
If the numbers are the squares, return the next square (16 in this case)
If each number divided by the previous number is the sequence 1 2 3, return 4 * the last number
If each number divided by the next number is the sequence 1 2 3, return the last number divided by 4.
Of course, a more robust solution would account for negative sequences, or those that bounce back and forth between positive and negative. But again, it is an underdetermined problem.
So im writing a small little c# console app, which is supposed to imitate a shopping system of sorts.
All the products are sold in Packs (not individually), and the Pack sizes are like 4 items, 10 items or 15 items.
Given a Qty size of like 28, it should return a result of something like:
2 x 10 Packs + 2 x 4 Packs.
Or given a Qty size of like 25, it should return a result of something like:
1 x 15 Packs + 1 x 10 Packs
And the function needs to be efficient, so it returns the least amount of packs.
(obviously ignore scenerio's where it might be an Qty of 3 - as there are no pack sizes that small)
this is my code at the moment:
qty = 28, LargePack = 15, MediumPack = 10, SmallPack = 4
double total = 0.00;
int tmpQty = qty;
while (tmpQty != 0)
{
if ((tmpQty >= LargePack) && ((tmpQty % LargePack) % 1) == 0)
{
tmpQty -= LargePack;
lg += 1;
total =+ (lg * LargePrice);
}
else if ((tmpQty >= MediumPack))
{
tmpQty -= MediumPack;
md += 1;
total =+ (md * MediumPrice);
}
else if ((SmallPack !=0) && (tmpQty >= SmallPack) && (tmpQty < MediumPack))
{
tmpQty -= SmallPack;
sm += 1;
total =+ (sm * SmallPrice);
}
}
My idea here - because with a qty of like 28, i need it to skip the first IF statement, i thought i'd do a check of whether tmpQty (28) / LargePack (15) was an integer number - which it shouldn't be, then it would go to the 2nd IF statement. But for some reason the formula:
(tmpQty % LargePack) %1 == 0
always equals 0 - so its True... when it should be false.
Or if anyone has a better way to work out Pack sizes, im open to suggestions.
Note - Pack sizes aren't defined here, as various different products use the same sorting process.
Your biggest issue is you don't understand what % does. It doesn't return a decimal. It works like the "remainder" from gradeschool division. 7 % 5 will return 2. If you want to keep your same logic (expecting a percentage) then you need to divide but your variables will need to be doubles or floats. If not, C# will cast the result to an integer, which has no decimal. I hope that helps.
(Anything % 1) always returns 0. That's why your condition is not working.
Try this
(tmpQty % LargePack) >= 0
Use:
if ((tmpQty >= LargePack) && ((tmpQty % LargePack) == 0))
This will return true only for values that are exactly divisible by LargePack, so in this case 28 would fail and move on.
Here is a brute force approach. At first we need a method to generate all combinations of an array of sequences. It is called CartesianProduct, and an implementation is shown below:
public static IEnumerable<T[]> CartesianProduct<T>(IEnumerable<T>[] sequences)
{
IEnumerable<IEnumerable<T>> seed = new[] { new T[0] };
return sequences.Aggregate(
seed,
(accumulator, sequence) => accumulator
.SelectMany(_ => sequence, (product, i) => product.Append(i))
).Select(product => product.ToArray());
}
We will need this method because we need all possible combinations of pack sizes and quantities. For example for pack sizes [5, 8] and quantity 21, the combinations we want to produce are these:
0*5 + 0*8 = 0
0*5 + 1*8 = 8
0*5 + 2*8 = 16
1*5 + 0*8 = 5
1*5 + 1*8 = 13
1*5 + 2*8 = 21
2*5 + 0*8 = 10
2*5 + 1*8 = 18
2*5 + 2*8 = 26
3*5 + 0*8 = 15
3*5 + 1*8 = 23
3*5 + 2*8 = 31
4*5 + 0*8 = 20
4*5 + 1*8 = 28
4*5 + 2*8 = 36
The last step will be to select the best combination. Here is the method that makes all these to happen:
private static (int PackSize, int Quantity)[] GetBestCombination(
int[] packSizes, int quantity)
{
var sequences = packSizes
.Select(p => Enumerable.Range(0, (quantity / p) + 1)
.Select(q => p * q))
.ToArray();
var cartesianProduct = CartesianProduct(sequences);
int[] selected = null;
int maxValue = Int32.MinValue;
foreach (var combination in cartesianProduct)
{
int sum = combination.Sum();
if (sum > quantity) continue;
if (sum > maxValue)
{
maxValue = sum;
selected = combination;
}
}
if (selected == null) return null;
return selected.Zip(packSizes, (sum, p) => (p, sum / p)).ToArray();
}
Lets test it:
var bestCombo = GetBestCombination(new[] { 5, 8 }, 21);
foreach (var pair in bestCombo)
{
Console.WriteLine($"{pair.Quantity} x {pair.PackSize}");
}
Output:
1 x 5
2 x 8
I am trying to get the count (just a number, not a list) of binary numbers that contain exactly 3 ones and that are less than 1000000000 ie: 10011, 100000011 and so on.
The code below works for integers, but how can I make it work with binary?
static void Main(string[] args)
{
int con = 0;
for (int i = 0; i < 1000000000; i++)
{
string test = i.ToString();
int count = test.Split('1').Length - 1;
if (count == 3)
{
con++;
}
}
Console.WriteLine(con);
}
And for your continued education, here's another way to solve the problem. We wish to know how many binary strings there are with exactly on on bits and off off bits.
There are some easy problems to solve in there. N(on, off) is equal to one if on and off are both zero, because the only solution is the empty string. And if either is zero then the answer is one, because the string that is all zeros or all ones is unique.
Let's start tabulating this in a table.
on
0 1 2 3 4 5
+---------------------
o 0| 1 1 1 1 1 1
f 1| 1
f 2| 1
3| 1
4| 1
5| 1
6| 1
Now what should go at (1, 1)? Well, the number of binary strings that have one on and one off bit is equal to the number of such strings that start with one, plus the number of such strings that start with zero. So we have:
N(1, 1) = N(1, 0) + N(0, 1) = 2
What about N(2, 1) ? Same deal:
N(2, 1) = N(1, 1) + N(2, 0) = 3
And we can see that similarly N(x, 1) = N(1, x) = x + 1. Fill in the array:
on
0 1 2 3 4 5
+---------------------
o 0| 1 1 1 1 1 1
f 1| 1 2 3 4 5 6
f 2| 1 3
3| 1 4
4| 1 5
5| 1 6
6| 1 7
in general for on, off not zero:
N(on, off) = N(on - 1, off) + N(on, off - 1)
which means that we can fill in this entire array by repeatedly applying this rule. Can you write a program which does so?
Once you do, you can simply read your answer out of the array at [6, 3].
Have you seen this pattern in this array before? It has a name. Hint: you probably have not seen it laid out as a square.
The simplest way to alter your code would be:
static void Main(string[] args)
{
int con = 0;
for (int i = 0; i < 512; i++)
{
string test = Convert.ToString(i, 2);
int count = test.Split('1').Length - 1;
if (count == 3)
{
con++;
}
}
Console.WriteLine(con);
}
This could be done as a pure mathematical equation though:
9! / (6!*3!) = 84
For your amusement and education, consider the following:
static IEnumerable<string> Combinations(int on, int off)
{
if (on == 0 && off == 0)
yield return "";
if (on > 0)
foreach(var s in Combinations(on - 1, off))
yield return "1" + s;
if (off > 0)
foreach(var s in Combinations(on, off - 1))
yield return "0" + s;
}
Study this implementation: it yields a sequence of binary strings with on bits on and off bits off. Do you see how it does so?
Plainly calling .Count() on this thing solves your problem, though such a solution is enormously less efficient than simply doing the math.
I present this for your study because a recursive enumerator such as this one is a powerful tool when investigating permutations and combinations.
A solution without resorting to string representations:
static int CountBits(int i)
{
var current = i;
var bits = 0;
while (current != 0)
{
if ((current & 1) == 1)
{
bits += 1;
}
current >>= 1;
}
return bits;
}
With this helper method, the count is easy:
var count = Enumerable.Range(0, 0b1000000000)
.Count(i => CountBits(i) == 3);
And the answer is 84.
Long code (grumpy style)
var con = 0;
for (var i = 0; i < 10000; i++)
{
var test = i.ToString();
if (test.Count(x => x == '1') == 3)
{
con++;
}
}
Console.WriteLine(con);
Shorter (more mature)
var con = Enumerable.Range(0, 10000)
.Select(x => $"{x:00000}")
.Count(x =>
x.Count(c => c == '1') == 3
);
Console.WriteLine(con);
PS: They both seem to have the same performance.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to check if a number is a power of 2
I want to determine if a number is in
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
...
I tried this:
public static void Main(string[] args)
{
int result = 1;
for (int i = 0; i < 15; i++)
{
//Console.WriteLine(result);
Console.WriteLine(result % 2);
result *= 2;
}
}
As you can see it returns
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...
How should I efficiently make the above print to be 0 for all of them including 1?
The following expression should be true if i is in your sequence.
(i & (i-1)) == 0)
http://rextester.com/JRH41036
How about something like this?
bool IsInBinarySequence( int number ){
var numbertocheck = 1;
do{
if( number == numbertocheck ) return true;
numbertocheck *= 2;
}while( numbertocheck <= number );
return false;
}
This has no specific limit on the number to check, but makes sure it stops checking if the number to check grows larger than the actual number we're trying to decide if is in the binary sequence.
Since the first time result is odd, you will get 1, since right after that you multiply it by 2, you will always get 0.
You need to print result if you want to get the list of powers of 2.
Console.WriteLine(result);
A primitive way to do that will be:
public static void Main(string[] args)
{
int result = 1;
int numToCheck = 141234;
boolean found = false;
for (int i = 0; i < 15; i++)
{
if (numToCheck == result) {
found = true;
break;
}
result *= 2;
}
if(found) Console.WriteLine("Awesome");
}
You can determine if a number is a power of 2 (including 2^0) by using the following method:
public bool IsPowerOfTwo(int x) {
return (x > 0) && ((x & (x - 1)) == 0)
}
Over here you can read why and how this works.
It's a bit of a hack, but this works ...
static void Main()
{
for (int i = 0; i < 40; i++)
{
var str = Convert.ToString(i, 2);
var bitCount = str.Count(c => c == '1');
Console.ForegroundColor = bitCount == 1 ? ConsoleColor.White : ConsoleColor.DarkGray;
Console.WriteLine(i + ": " + (bitCount == 1));
}
}
it seems you're actually asking if only one bit in the binary representation of the number is a 1
What you is not a test whether the number is in the sequence BUT it is a generator for such numbers... only the print part is containing some sort of a test...
Try this code for a test:
public static void Main(string[] args)
{
int result = 0;
int numToTest = 0;
if ( int.TryParse (args[0], out numToTest) )
{
result = ((from c in Convert.ToString (numToTest, 2) where c == '1' select c).Count() == 1 ) ? 1 : 0;
}
Console.WriteLine(result);
}
The above code takes a commandline argument and tests it for being in the binary sequence according to the criterion you posted... if so it prints 1, otherwise it prints 0.
Thats correct. 1 0 0 0 0 0 is the correct sequence.
Result is 1 in the first loop. 1 % 2 is 1.
Then result *= 2 gives result the value 2. In the next loop run 2 % 2 = 0. Then result *= 2 is 4. 4%2 is 0. 4 *= 2 is 8. 8 %2 is 0. Since result is always multiplied with 2 it keeps to be in the powers of 2 row and thus als MOD operations with 2 result to 0. So all is fine with that code.
your code will print only Binary sequences. as you are applying MOD 2 . so either you will get 0 or 1 . so it will be print in Binary Sequence.
Boolean result = false;
Int32 numberToTest = 64;
Int32 limit = 15;
for (int i = 0; i < limit && !result; i++)
{
if (Math.Pow(2, i).Equals(numberToTest))
{
result = true;
}
}
Console.WriteLine(String.Format("Number {0} {1} a power of 2.", numberToTest, result ? "is" : "is not"));