-1

I have tried using the following methods to allow myself to enter extremely large values such as 10^15000 into my console application. This application must compute values large enough to be timed using a Stopwatch object and land in under 1 second. So far I can only get the maximum number of input chars allowed when using Console.SetIn(new StreamReader(Console.OpenStandardInput(8192)));

Here is my custom ReadLine() method:

static string ReadLine()
{
    StringBuilder sb = new StringBuilder();
    while(true){
        char ch = Convert.ToChar(Console.Read());
        sb.Append(ch);
        if(ch == '\n'){
            break;
        }
    }

    return sb.ToString();
}

Here is my entire program, which is used specifically for algorithm time testing:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
using System.Diagnostics;
using System.IO;

namespace LabGrowthOfFunctions
{
    class Program
    {        
        static void Main(string[] args)
        {
            int userNum;

            Console.Write("Please enter a number: ");
            Console.SetIn(new StreamReader(Console.OpenStandardInput(8192))); 

            string tempString = ReadLine();  
            int.TryParse(tempString, out userNum);

            Stopwatch sw = Stopwatch.StartNew();
            //nsquaredgrowthversion1(userNum);
            //ncubedfunction(userNum);
            ntothefourthpowerfunction(userNum);
            //nfunction(userNum);
            //nlognfunction(userNum);
            sw.Stop();

            Console.WriteLine("Time used: {0} secs", sw.Elapsed.TotalMilliseconds / 1000);
            Console.ReadLine();
        }

        private static void nsquaredgrowthversion1(int n)
        {
            int sum = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    sum++;
        }

        private static void ncubedfunction(int n)
        {
            int sum = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    for (int k = 0; k < n; k++)
                        sum++;
        }

        private static void ntothefourthpowerfunction(int n)
        {
            int sum = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    for (int k = 0; k < n; k++)
                        for (int l = 0; l < n; l++)
                            sum++;
        }

        private static void nfunction(int n)
        {
            int sum = 0;

            for (int i = 0; i < n; i++)
                sum++;
        }

        private static void nlognfunction(BigInteger n)
        {
            double sum = 0;
            double result = 0.0;
            result = Math.Log((double)n,2);
            sum = (double)n * result; 
        }



        //ReadLine recreated for large input sizes.
        static string ReadLine()
        {
            StringBuilder sb = new StringBuilder();
            while(true){
                char ch = Convert.ToChar(Console.Read());
                sb.Append(ch);
                if(ch == '\n'){
                    break;
                }
            }

            return sb.ToString();
        }

    }//end Main
}//end namespace LabGrowthOfFunctions
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
Chisx
  • 1,976
  • 4
  • 25
  • 53
  • 3
    `int`s are 32-bit - you need `BigInteger`. you also need to read in chunks. – Daniel A. White Feb 03 '15 at 20:58
  • I tried using BigInteger but it still limited the size number that I could paste into console.. using BigInteger also froze my console? How might I read in chunks? – Chisx Feb 03 '15 at 20:59
  • Related: http://stackoverflow.com/questions/28245018/increase-buffer-for-console-readline/28245149#28245149 – Matt Burland Feb 03 '15 at 21:05
  • Thank you Matt Burland, I did not find that answer.. I only found answers similar to my method of approach – Chisx Feb 03 '15 at 21:07
  • Please be specific. Are you saying that you want to enter text which is the digit `1` followed by 15,000 `0` digits? When you do, what _specifically_ happens, and in what way _specifically_ is that different from what you want? See http://stackoverflow.com/help/how-to-ask. Also, please trim your code example so that it show _only_ [the minimum code needed to illustrate this _exact_ problem](http://stackoverflow.com/help/mcve). – Peter Duniho Feb 04 '15 at 06:58

1 Answers1

0

You could use the ReadKey() method instead. Read each character as it entered, append to a string, then convert the String to an Int.

Tomas Schier
  • 407
  • 5
  • 16