radimitrov / CSharpShellApp

89 stars 18 forks source link

Uniter #222

Closed Profmaxmonteiro closed 2 years ago

Profmaxmonteiro commented 2 years ago

using System; using System.Linq; using System.Collections.Generic; using System.Threading.Tasks; using System.Globalization;

namespace exercicio_02 { class Program { static void Main(string[] args) {
try { String entrada; int entradaConvertida = 0; do { Console.Write("Digite os dois últimos números de seu RU: "); entrada = Console.ReadLine();

                try 
                {
                    entradaConvertida = Convert.ToInt32(entrada);
                }
                catch 
                {
                    entradaConvertida = 0;
                }
                if(entradaConvertida > 0)
                {
                    break;

                }
                else
                {
                    Console.WriteLine("entrada inválida!! ");
                }
            }

        }while (true) ;
        Console.WriteLine("Números Primos: ");

        int qtdThreads = entradaConvertida <=10 ? 1: Decimal.ToInt32(Decimal.Ceiling(entradaConvertida/10))+1;
        Console.WriteLine(qtdThreads);
        for (var i = 1; i <= qtdThreads; i++)
        {
            int inicial = i == 1 ? 1: ((i - 1)* 10 + 1);
            int final = i==1 ? (entradaConvertida <= 10 ? entradaConvertida :i*10) : (i*10 < entradaConvertida ? i*10:entradaConvertida);

           Thread t1 = new Thread(() =>
           {
               Primo(inicial, final, entradaConvertida);
           });
           t1.Start();
           }
    }       
            catch
            {
                Console.WriteLine("Erro de execurção");
            }

    static void primo(int inicial, int final, int n) {
        List<int> numPrimo = new List<int>();

    for (var i = inicial; i <= final; i++)
    }   
        bool numEPrimos = true;

        for (var j = 2; j <= n; j++)
        {
            if((i%j) == 0 && i != j)
            {
                numEPrimos = false; break;
            }
            if (numEPrimo == true)
            }
                numPrimos.Add(i);
            }
        }
}
        foreach (var numEPrimo in numPrimos)
        {
            Console.WriteLine(numEPrimo);
        }
        }

    }
}

}

Profmaxmonteiro commented 2 years ago

Hgf

radimitrov commented 2 years ago

I suppose this code is for checking prime numbers? You're trying to make it work in parallel? Creating pure threads essentially results in a random order of execution, but they might still print out in the app. However that is due to a limitation in the app's runtime. Any threads you start can't be terminated by the app, so they will continue after your program exits. And your program exits immediately after creating the threads - on Windows it would just close.

Also C# is case sensitive when naming stuff so "primo" != "Primo". And some of the "{" and "}" braces seem wrong. There are more errors in variable names.

This code can't be made parallel without rewriting it almost completely. And your current implementation wouldn't make it more effective without some modifications to avoid recalculations because it is parallel. See the Parallel class.

However multiple threads won't make the calculation itself faster. It is a simple mathematical operation and threading itself is a costly operation, especially when iterating. Single thread will be faster. Something like this.