I'm attempting to do Project Euler problem #4 in C#. The problem I'm having is that when the code runs a console window briefly appears and then goes away. I don't know what the problem could be as I'm relatively new to programming.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for (int i = 1000; i > 100; i--)
for (int j = 1000; j > 100; j--)
PalCheck(i * j);
}
static void PalCheck(int original)
{
var reversed = new string(Convert.ToString(original).ToCharArray().Reverse().ToArray());
if (Convert.ToString(original) == reversed)
Console.WriteLine(original);
Console.ReadKey();
}
}
}
The code seems to be stuck at the line
Console.ReadKey() as at this line of code, the program is waiting for some input key.
Since you have not used any message before ReadKey(), you don't realize that the program is waiting for some input and not stuck.
Move Console.ReadKey() after PalCheck(i * j) and you should see the output on the console screen.
Related
Where exactly is the problem? "System.ArgumentNullException: 'Value cannot be null.
Parameter name: path'" why could this be? Is there anyone who can help? Could the question have something to do with "Textwriter"?
here is my code
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Result
{
/*
* Complete the 'aVeryBigSum' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts LONG_INTEGER_ARRAY ar as parameter.
*/
public static long aVeryBigSum(List<long> ar)
{
long sum=0;
for(int i=0; i<= ar.Count;i++) {
sum += ar[i];
}
return sum;
}
}
class Solution
{
public static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(#System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int arCount = Convert.ToInt32(Console.ReadLine().Trim());
List<long> ar = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arTemp => Convert.ToInt64(arTemp)).ToList();
long result = Result.aVeryBigSum(ar);
textWriter.WriteLine(result);
textWriter.Flush();
textWriter.Close();
}
}
I just ran your solution directly on Hackerrank and it does not throw that exception, so I would assume that you are running it locally.
When you run those solutions locally you need to be careful with the Environment Variables.
In this case the program expects an Environment Variable called OUTPUT_PATH which you probably did not set on your machine, but it is set on Hackerrank.
According to Microsoft, Environment.GetEnvironmentVariable returns:
The value of the environment variable specified by variable, or null if the environment variable is not found.
ı solved the problem. change the function like this:
public static long aVeryBigSum(List<long> ar)
{
long sum=0;
foreach(long item in ar) {
sum = sum +item;
}
return sum;
}
I want to learn how to measure the running time of pieces of code, but judging by the information on the Internet, there are a lot of subtleties that need to be taken into account, so I'm trying to figure out BenchmarkDotNet (only the running time, the rest of the features do not care yet).
As you can see from the picture, the time is not shown (NA), although I did everything according to the templates. Actually, what's wrong?
It will be great if someone writes what exactly needs to be added/removed, and not just gives general recommendations. I'm learning the basics of C#, and haven't even gotten to OOP, so it's hard for me to understand you without your code.
Thank you Google for translating from Russian to English.
using System;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace ConsoleApp2
{
public class Program
{
[Benchmark]
public void NotePad()
{
string path = #"..\..\..\first_million_prime_number\primes1.txt";
var sr = new StreamReader(path, Encoding.Default);
List<int> numbers = new List<int>();
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length == 0) continue;
for (int i = 0; i < 80; i += 10)
numbers.Add(Convert.ToInt32(line.Substring(i, 10)));
}
sr.Close();
}
static void Main(string[] args)
{
BenchmarkRunner.Run<Program>();
}
}
}
I have a simple program written in C# and I ran it in two laptops same specs but one has Windows 8 and the other has Windows 10 and I measure the time in milliseconds and get the difference between the start and the end but in windows 8 it takes approx 1k milliseconds while in windows 10 it takes approx 15k milliseconds. Why this huge difference?
This is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
Console.WriteLine(milliseconds);
for(int i = 0; i < 100000000; i++)
{
if(i%10000==0)
Console.WriteLine(i);
}
long milliseconds2 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
Console.WriteLine(milliseconds2);
Console.WriteLine("Diff " + (milliseconds2-milliseconds));
Console.ReadLine();
}
}
}
I want to create a c# program that will look in a folder for files. If the files are found then I want to start a program. If the file is not there then I want to program to sleep for 30 minutes and look in the folder again. I want to keep doing it maybe 10 times and if it still doesn’t find the file then exit the program. I wrote the if part but I need help on the else part. This is what I have so far.
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Diagnostics;
class Program
{
static void Main()
{
// See if this file exists in the SAME DIRECTORY.
if (File.Exists(#"C:\name.txt"))
{
Process.Start(#"C:\bulkload.bat");
}
else
{
}
}
}
Untested, for guidance purpose only.
for (int i = 0; i < 10; i++)
{
if (File.Exists(#"C:\name.txt"))
{
Process.Start(#"C:\bulkload.bat");
return;
}
else //no need of else block really.
{
Thread.Sleep(30 * 60 * 1000);
}
}
I'm having a problem trying to make a small app to solve Project Euler Problem #1.
Whenever I attempt to run this, it returns as 0, instead of 233168.
I'm not necessarily looking for an absolute answer, just some hints, I'm trying to learn.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x;
List<int> listOne = new List<int>();
for (x = 1; x < 1000; ++x)
{
if(x%3 == 0 || x%5 == 0)
{
listOne.Add(x);
}
Console.WriteLine(listOne.Sum());
Console.ReadLine();
}
}
}
}
In the interests of helping you learn, I'm not going to provide the exact answer.
Have a look at the scoping of your Console.WriteLine() statement. My guess is that it's not running when you think it should be.