I am a beginner, trying to solve Project Euler problem 1:
"If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.", but, you probably know this problem.
So I get a correct answer but my program lists all sums up to a final sum. But how do I manage to just print final sum?
int x = 0;
for (int i = 1; i < 1000; i ++)
{
if (i % 3 == 0 || i % 5 == 0)
{
Console.WriteLine(x += i);
You have a Console.WriteLine inside a loop, so every time the if is true, a print out to the console will occur. Move the Console.WriteLine so it is outside of the loop
I think you would have quite quickly discovered this problem if you had used the debugger to step through the code line by line. Do you know how to use the debugger? If not, drop a comment and I'll write some introductory lines
You should keep console.WriteLine() outside of 'for' loop,
Your code should go look like this-
int x=0;
for (int i = 1; i < 1000; i ++){
if (i%3==0 || i%5==0){
x+=i;
}}
console.WriteLine(x);
good start bro, you can just make the Console.WriteLine(x) after the for loop
You don't need to write every time you add i to x. You can just add it and then print after you ran the for loop.
int x = 0;
for (int i = 0; i < 1000; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
x += i;
}
}
Console.WriteLine(x);
If you want to print only the final result, move the Console.WriteLine command out of the iteration and place it after it. This way it will execute only once.
int x = 0;
for (int i = 1; i < 1000; i ++)
{
if (i % 3 == 0 || i % 5 == 0)
{
x += i;
}
}
Console.WriteLine(x);
Related
I'm new at programming in general and learning C# right now.
I just wrote a little programm where I have to step through an int[] in a specific pattern. The pattern is as follows:
Start at the last entry of the array (int i)
form the sum of i and (if avaible) the three entrys above (e.g. i += i-1 ... i += i-3)
Change i to i -= 4 (if avaible)
Repeat from step 2 until i = 0;
Therefore i wrote the following loop:
for (int i = intArray.Length - 1; i >= 0; i -= 4)
{
for (int a = 1; a <= 3; a++)
{
if (i - a >= 0)
{
intArray[i] += intArray[i - a];
intArray[i - a] = 0;
}
}
}
Now my new assignment is to change my code to only use 1 loop with the help of modulo-operations. I do understand what modulo does, but i can't figure out how to use it to get rid of the second loop.
Maybe somebody explain it to me? Thank you very much in advance.
While iterating over the array, the idea would be to use the modulo 4 operation to calculate the next index to which you will add the current value.
This should work with any array lengths:
for (int i = 0; i < intArray.Length; i++){
// we check how far away we are from next index which stores the sum
var offset = (intArray.Length - 1 - i) % 4;
if (offset == 0) continue;
intArray[i+offset] += intArray[i];
intArray[i] = 0;
}
iterating the array reversely makes it a bit more complicated, but what you wanted to get rid of inner loop with modulo is probably this:
var intArray = Enumerable.Range(1, 15).ToArray();
for (int i = 1; i < intArray.Length - 1; i ++)
{
intArray[i - (i % 4)] += intArray[i];
intArray[i] = 0;
}
For example:
for (int i = 0; i < 9; i++ && (a specific condition to the incremented variable))
This would be use to stall a loop in a fake infinity until the said condition would be reached.
This point of this would be to have the loop still continue to do it's operation while stopping the increment of the i variable until a condition is met. For example if('another varible' < 9) then i can have it's normal increment. In other case if the condition is not met the i would not increment but the loop would still proceed the instruction one by one.
You can implement
for (int i = 0; i < 9; i++ && (a specific condition to the incremented variable))
as
for (int i = 0; i < 9; i = (a specific condition to the incremented variable) ? i + 1 : i)
but this goes against the general purpose and intention of a for loop. It would just confuse your testers, reviewers and fellow coders.
And, not unimprortant, it would make the CLR optimizer give up and slow down myArray[i] by adding a range check each time.
I think what you're looking at is something like this:
var counter = 0;
var otherCondition = 15;
while (counter < 9)
{
if (otherCondition < 9)
{
counter++;
}
// Do other stuff
}
You're starting the counter at 0 just like in the for loop, and terminates when it reaches 9. But increments only when the other condition is met.
I think the appropriate flow of control structure is a nested loop, with the inner loop "stalling" to infinity.
for (int i = 0; i < 9; i++ )
{
do
{
Foo();
} while (!condition)
}
What about i += (true/false) ? 1 : 0?
for (int i = 0; i < 9; i += (a specific condition to the incremented variable) ? 1 : 0)
For example:
var n = 0;
for (int i = 0; i < 9; i += (n++%2==0) ? 1 : 0) {
Console.WriteLine(i);
}
Having said that it makes the code unnecessarily harder to read and to understand your intention.
The following two examples are much clearer.
int met = 0;
while(true)
{
if(a specific condition to the incremented variable) met++;
if(met >= 9) break;
}
or
var met = 0;
while( met < 9 )
{
if(a specific condition to the incremented variable) met++;
}
I think while will be more helpful in this case:
int i = 0;
while (i < 9)
{
if (condition)
i++;
}
I'm having a problem with the first coding challenge on the Project Euler website. This is what you have to do:
"If we list all the natural numbers below 10 that are multiples of 3
or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000."
Here's my code (c#):
total = 0;
for (int i = 0; i <= (999 - (999 % 3)) / 3; i++)
{
total += 3 * i;
}
for (int i = 0; i <= (999 - (999 % 5)) / 5; i++)
{
total += 5 * i;
}
When I return total it gives me '266333' while it should be '233168'. I've been staring at it for about an hour and I have no clue where my code is going wrong. Sorry if this is a stupid question but google isn't helping me and I feel really dumb.
You can do in single for loop as :
int total = 0;
for (int i = 1; i < 1000; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
total += i;
}
}
for (int i = 0; i < 100; i = (i%10))
{
age = (age +10);
int_year = (int_year + 10);
So I am trying to use the Mod function for looping in C#, the requirement is that it the int_year and age loop 100 times but only every 10th increment is kept. Thanks in advance :)
Why not just mod 10 within the loop to determine if this is the 10th iteration:
for (int i = 0; i < 100; i++)
{
if (i % 10 == 0)
{
// Every 10th iteration
}
}
You can use step equal to 10:
for (int i = 0; i < 100; i += 10)
{
// Every 10th iteration
}
I think it's more efficient than using mod.
I have a code here and I would like that it will display the first 10 and if I click on that, it will display again the second batch. I tried this first with my first for-code and it work now I'm working with arrays it seems it didn't accept it
The one I commented dont work? is this wrong?
Thanks
long [] potenzen = new long[32];
potenzen[0] = 1;
for (int i = 1; i < potenzen.Length; ++i)
{
potenzen[i] = potenzen[i-1] * 2;
//if (potenzen % 10 == 0)
// Console.ReadLine();
}
foreach (long elem in potenzen)
{
Console.WriteLine(" " + elem);
}
long [] potenzen = new long[32];
potenzen[0] = 1;
for (int i = 1; i < potenzen.Length; ++i)
{
potenzen[i]=potenzen[i-1]*2;
Console.WriteLine(potenzen[i-1]);
if (i % 10 == 0)
Console.ReadLine();
}
is more in line with what you want. An improvement would be to separate your data-manipulation logic from your data display logic.
long [] potenzen = new long[32];
potenzen[0] = 1;
for (int i = 1; i < potenzen.Length; ++i)
potenzen[i]=potenzen[i-1]*2;
for (int i = 0; i < potenzen.Length; ++i)
{
Console.WriteLine(potenzen[i]);
if (i % 10 == 0)
Console.ReadLine();
}
Of course, you could do this without an array
long potenzen = 1;
for (int i = 1; i < 32; ++i)
{
Console.WriteLine(potenzen);
potenzen = potenzen * 2;
if (i % 10 == 0)
Console.ReadLine();
}
You need:
if (i % 10 == 0)
and not:
if (potenzen % 10 == 0)
Applying the modulus operator to an array of longs is dubious.
potenzen is an array so you maybe try
if (i % 10 == 0)
or maybe
if (potenzen[i] % 10 == 0)
You're taking an array mod 10 -- at best, in an unsafe language, you'd be doing the modulo operation on a memory address.
This should work fine if you just change the line to:
// if you don't want to pause the first time you run it, replace with:
// if (i > 0 && i % 10 == 0) {
if (i % 10 == 0) {
Console.ReadLine();
}
Try changing it to:
long [] potenzen = new long[32];
potenzen[0] = 1;
Console.WriteLine(potenzen[0]);
for (int i = 1; i < potenzen.Length; ++i)
{
potenzen[i]=potenzen[i-1]*2;
Console.WriteLine(potenzen[i]);
if (i % 10 == 0)
{
var s = Console.ReadLine();
// break if s == some escape condition???
}
}
Right now, you're never printing, unless you completely finish your first for loop. My guess is that you're not allowing the full 32 elements to complete, so you're never seeing your results -
This will print them as they go.