Advertisement
Frumkin

Untitled

Jul 20th, 2023
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Prime_Factorization
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int Prime = 1;
  14.             double doublePrime = 0;
  15.             int TimesDividingPrimes = 0;
  16.             bool CheckPrime = true;
  17.             int PrimeDivide = 1;
  18.             bool StopLoop1 = false;
  19.             int PowerCount = 0;
  20.             int WhileUserNumber = 0;
  21.             bool StopAll = false;
  22.  
  23.             Console.WriteLine("Enter the number you would like to see factorized");
  24.             string UserNumber = Console.ReadLine();
  25.             WhileUserNumber = int.Parse(UserNumber);
  26.  
  27.             while (StopAll == false)
  28.             {
  29.                 ++Prime;
  30.                 doublePrime = Math.Sqrt(Prime);
  31.                 TimesDividingPrimes = (int)doublePrime;
  32.                 for (int PrimeLoop2 = 1; PrimeLoop2 < TimesDividingPrimes; ++PrimeLoop2)
  33.                 {
  34.                     ++PrimeDivide;
  35.                     if (Prime % PrimeDivide == 0 && Prime != 2)
  36.                     {
  37.                         CheckPrime = false;
  38.                     }
  39.                     if (CheckPrime == false)
  40.                     {
  41.                         PrimeLoop2 = TimesDividingPrimes;
  42.                     }
  43.                 }
  44.                 PrimeDivide = 1;
  45.  
  46.                 if (CheckPrime == true)
  47.                 {
  48.                     while (StopLoop1 == false)
  49.                     {
  50.                         if (WhileUserNumber % Prime == 0 && WhileUserNumber != 1)
  51.                         {
  52.                             ++PowerCount;
  53.                             WhileUserNumber = (WhileUserNumber / Prime);
  54.                         }
  55.                         else
  56.                         {
  57.                             StopLoop1 = true;
  58.                         }
  59.                         if (WhileUserNumber == 1)
  60.                         {
  61.                             StopLoop1 = true;
  62.                             StopAll = true;
  63.                         }
  64.                     }
  65.                     if (PowerCount == 1)
  66.                     {
  67.                         Console.WriteLine(Prime);
  68.                     }
  69.                     else if (PowerCount > 1)
  70.                     {
  71.                         Console.WriteLine(Prime + "^" + PowerCount);
  72.                     }
  73.                     PowerCount = 0;
  74.                 }
  75.                 CheckPrime = true;
  76.                 StopLoop1 = false;
  77.  
  78.  
  79.  
  80.  
  81.  
  82.             }
  83.             Console.ReadLine();
  84.  
  85.         }
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement