0
What is with while in my program? Pls help my with my C# code
I'm a new in codding and I wanted today started ProjectEuler with the first proplem I wanted use loop while (I know about loop for for this task) to complete this As a result a wrote this code: https://www.sololearn.com/compiler-playground/cn8R790J5M1D And I get a wrong answer 345388, right answer is 233168 And also I get strange first position - 61300 201 (first is sum, second is i; you'll understand it if open my code) Thank you if you'll help me
8 Antworten
+ 4
Demmarc
you made it more complicated than it needs to be.
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
int i = 1;
//the while loop:
while (i < 1000){
//you can omit the brackets here
if(i%3==0 || i%5==0)
sum += i;
//don't forget to increment
i++;
}
Console.WriteLine(sum);
}
}
}
+ 2
OK Demmarc let's see the code from the code-playground
https://code.sololearn.com/cn8R790J5M1D
+ 2
That's a really weird way of getting the answer. I can't make the connection between your method and i%3==0 || i%5==0. and you're adding magic numbers in the process, as well as doing -=...
It somewhat feels like that the sum was being brute-forced into the correct answer.
Maybe you could provide a mathematical explanation on why this method should return the correct sum of numbers evenly divisible by 3 or 5?
(not being critical or dismissing your method, but more like fascinated by the mysterious method that feels like a magic trick...)
+ 1
Demmarc
if you want i and sum for each iteration, maybe something like:
using System;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
//while loop
int sum=0, i=1;
while(i<1000){
if(i%3==0 || i%5==0){
Console.Write(quot;i = {i} ");
sum += i;
Console.Write(quot; sum = {sum}\n");
}
i++;
}
}
}
}
+ 1
Demmarc
or if you want it to be more elaborate:
using System;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
//while loop
int sum=0, i=1;
while(i<1000){
if(i%3==0 && i%5==0){
Console.Write(
quot;(mod 3&5) i = {i} ");
sum += i;
Console.Write(
quot; sum = {sum}\n");
}
else if(i%3==0){
Console.Write(
quot;(mod 3) i = {i} ");
sum += i;
Console.Write(
quot; sum = {sum}\n");
}
else if(i%5==0){
Console.Write(
quot;(mod 5) i = {i} ");
sum += i;
Console.Write(
quot; sum = {sum}\n");
}
i++;
}
}
}
}
+ 1
Bob_Li
I returned my old errorcode and fixed it myself. I know that it little stupid, sorre btw and thank for your help
int i = 1;
int sum = 0;
while (i*3 < 999) {
this is my mistake (<) right is >
|
if (i*5 < 999) {
sum += i*3;
i++;
continue;
}
sum+=i*8; //i*3+i*5=i*8
i++;
Console.WriteLine(quot;{sum} {i}");
}
Because of this mistake I was getting this https://imgur.com/a/f1j9o3n
Thank you for help
0
Bob_Li
Yes, it is
But I actually don't understand why its write this strange first output?
Wrong final output because some double values like 15 30 and other (as I understand). Can it be solved in my code?
0
Demmarc I can't see your original erroneous code, but my guess is you're adding some value of i twice. Most probably when i%3==0 && i %5==0. like 15,30,45...