n-th prime number problem, need to speed it up a bit - c#

There is simple cipher that translates number to series of . ( )
In order to encrypt a number (0 .. 2147483647) to this representation, I (think I) need:
prime factorization
for given p (p is Prime), order sequence of p (ie. PrimeOrd(2) == 0, PrimeOrd(227) == 49)
Some examples
0 . 6 (()())
1 () 7 (...())
2 (()) 8 ((.()))
3 (.()) 9 (.(()))
4 ((())) 10 (().())
5 (..()) 11 (....())
227 (................................................())
2147483648 ((..........()))
My source code for the problem
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
static class P
{
static List<int> _list = new List<int>();
public static int Nth(int n)
{
if (_list.Count == 0 || _list.Count < n)
Primes().Take(n + 1);
return _list[n];
}
public static int PrimeOrd(int prime)
{
if (_list.Count == 0 || _list.Last() < prime)
Primes().First(p => p >= prime);
return (_list.Contains(prime)) ? _list.FindIndex(p => p == prime) : -1;
}
public static List<int> Factor(int N)
{
List<int> ret = new List<int>();
for (int i = 2; i ≤ N; i++)
while (N % i == 0)
{
N /= i;
ret.Add(i);
}
return ret;
}
public static IEnumerable<int> Primes()
{
_list = new List<int>();
_list.Add(2);
yield return 2;
Func<int, bool> IsPrime = n => _list.TakeWhile(p => p ≤ (int)Math.Sqrt(n)).FirstOrDefault(p => n % p == 0) == 0;
for (int i = 3; i < Int32.MaxValue; i += 2)
{
if (IsPrime(i))
{
_list.Add(i);
yield return i;
}
}
}
public static string Convert(int n)
{
if (n == 0) return ".";
if (n == 1) return "()";
StringBuilder sb = new StringBuilder();
var p = Factor(n);
var max = PrimeOrd(p.Last());
for (int i = 0; i ≤ max; i++)
{
var power = p.FindAll((x) => x == Nth(i)).Count;
sb.Append(Convert(power));
}
return "(" + sb.ToString() + ")";
}
}
class Program
{
static void Main(string[] args)
{
string line = Console.ReadLine();
try
{
int num = int.Parse(line);
Console.WriteLine("{0}: '{1}'", num, P.Convert(num));
}
catch
{
Console.WriteLine("You didn't entered number!");
}
}
}
The problem is SLOWNESS of procedure PrimeOrd. Do you know some FASTER solution for finding out order of prime in primes?
Heading
If You know how to speed-up finding order of prime number, please, suggest something. :-)
Thank You.
P.S. The biggest prime less than 2,147,483,648 is 2,147,483,647 and it's 105,097,565th prime. There is no need to expect bigger number than 2^31.

This is not something you should be doing at run-time. A better option is to pre-calculate all these primes and then put them in your program somehow (a static array, or a file to be read in). The slow code is then run as part of the development process (which is slow anyway :-), not at the point where you need your speed.
Then it's just a matter of a lookup of some sort rather than calculating them every time you need them.

Please see SO questions:
http://www.google.com/search?q=site%3Astackoverflow.com+prime+number&btnG=Search
Finding prime numbers with the Sieve of Eratosthenes (Originally: Is there a better way to prepare this array?)
Prime number calculation fun
How can I find prime numbers through bit operations in C++?
prime numbers c#
Finding composite numbers
Prime numbers program
If you need a list of known primes, have a look here

You should cache the primes to _list and then use it for both Factor and PrimeOrd. Additionally avoid operators LINQ operators like TakeWhile that create values that you throw away.
Here's an optimized version:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
static class P
{
private static List<int> _list = new List<int>();
public static int Nth(int n)
{
if (_list.Count == 0 || _list.Count <= n)
{
GenerateNextPrimes().First(p => _list.Count >= n);
}
return _list[n];
}
public static int PrimeOrd(int prime)
{
var primes = GrowPrimesTo(prime);
return primes.IndexOf(prime);
}
public static List<int> Factor(int N)
{
List<int> ret = new List<int>();
GrowPrimesTo(N);
for (int ixDivisor = 0; ixDivisor < _list.Count; ixDivisor++)
{
int currentDivisor = _list[ixDivisor];
while (N % currentDivisor == 0)
{
N /= currentDivisor;
ret.Add(currentDivisor);
}
if (N <= 1)
{
break;
}
}
return ret;
}
private static List<int> GrowPrimesTo(int max)
{
if (_list.LastOrDefault() >= max)
{
return _list;
}
GenerateNextPrimes().First(prime => prime >= max);
return _list;
}
private static IEnumerable<int> GenerateNextPrimes()
{
if (_list.Count == 0)
{
_list.Add(2);
yield return 2;
}
Func<int, bool> IsPrime =
n =>
{
// cache upperBound
int upperBound = (int)Math.Sqrt(n);
for (int ixPrime = 0; ixPrime < _list.Count; ixPrime++)
{
int currentDivisor = _list[ixPrime];
if (currentDivisor > upperBound)
{
return true;
}
if ((n % currentDivisor) == 0)
{
return false;
}
}
return true;
};
// Always start on next odd number
int startNum = _list.Count == 1 ? 3 : _list[_list.Count - 1] + 2;
for (int i = startNum; i < Int32.MaxValue; i += 2)
{
if (IsPrime(i))
{
_list.Add(i);
yield return i;
}
}
}
public static string Convert(int n)
{
if (n == 0) return ".";
if (n == 1) return "()";
StringBuilder sb = new StringBuilder();
var p = Factor(n);
var max = PrimeOrd(p.Last());
for (int i = 0; i <= max; i++)
{
var power = p.FindAll(x => x == Nth(i)).Count;
sb.Append(Convert(power));
}
return "(" + sb.ToString() + ")";
}
}
class Program
{
static void Main(string[] args)
{
string line = Console.ReadLine();
int num;
if(int.TryParse(line, out num))
{
Console.WriteLine("{0}: '{1}'", num, P.Convert(num));
}
else
{
Console.WriteLine("You didn't entered number!");
}
}
}

Related

C# Collatz - Does anyone know a fix?

Hey does anyone have a fix for this? I don't know why i keep getting an error that the main-cs and compilation fails.
using System;
class MainClass {
public static void Main (string[] args) {
Console.WriteLine ("Length of Collatz Row");
int cn = Console.ReadLine();
CollatzListLength(cn);
}
public int CollatzListLength(n){
int number;
List<int> numbers = new List<int>();
while(n != 1){
if(n % 2 == 0){
number = n/2;
}
if(n%2 ==1){
number = n*3 + 1;
}
n = number;
numbers.Add(number);
}
return numbers.Count;
}
Console.ReadLine() returns a string, not an int.
CollatzListLength needs to be static and the parameter needs to be declared as int
n = number; doesn't work because number may never be assigned a value. Use else instead of if (n % 2 == 1) as it just checks the other possible condition anyway.
In total:
static void Main(string[] args)
{
Console.WriteLine("Length of Collatz Row");
if (int.TryParse(Console.ReadLine(), out int cn))
CollatzListLength(cn);
else
Console.WriteLine("Needs a number");
}
public static int CollatzListLength(int n)
{
int number;
List<int> numbers = new List<int>();
while (n != 1)
{
if (n % 2 == 0)
{
number = n / 2;
}
else
{
number = n * 3 + 1;
}
n = number;
numbers.Add(number);
}
return numbers.Count;
}
I haven't actually checked the sanity of the code though.
An alternative, repaired to compile without error messages:
using System;
using System.Collections.Generic;
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Length of Collatz Row");
int cn = int.Parse(Console.ReadLine());
int len = CollatzListLength(cn);
Console.WriteLine($"len {len}");
}
public static int CollatzListLength(int n)
{
int number = 0;
List<int> numbers = new List<int>();
while (n != 1)
{
if (n % 2 == 0)
{
number = n / 2;
}
if (n % 2 == 1)
{
number = n * 3 + 1;
}
n = number;
numbers.Add(number);
}
return numbers.Count;
}
}

Function to check numbers are prime numbers

I need to be able to check that two numbers entered by the user (p and q) are prime numbers, and if they aren't prime numbers then the user is asked to re-enter a prime number until a prime number has been entered for both p and q. If I enter a number which isn't a prime number, I am asked to enter another number, if I enter a prime number the second time round it tells me the prime number isn't a prime number. How can I correct this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Math;
using System.Numerics;
namespace primenumbers
{
class Program
{
int Check_Prime(BigInteger p)
{
if (p <= 1) return 0;
for (int i = 2; i <= p / 2; i++)
{
if (p % i == 0)
{
return 0; //not a prime number
}
}
return 1;
}
static void result (BigInteger p)
{
int result = Check_Prime(p);
if (Check_Prime(p) != 0)
{
Console.WriteLine(" is a prime number");
}
else do
{
Console.WriteLine(" is not a prime number");
Console.WriteLine("Please enter a prime number");
p = BigInteger.Parse(Console.ReadLine());
} while (result == 0);
}
static int Check_Prime_Q(BigInteger q)
{
if (q <= 1) return 0;
for (int i = 2; i <= q / 2; i++)
{
if (q % i == 0)
{
return 0; //not a prime number
}
}
return 1;
}
static void resultq(BigInteger q)
{
int result = Check_Prime_Q(q);
if (Check_Prime_Q(q) != 0)
{
Console.WriteLine(" is a prime number");
}
else do
{
Console.WriteLine(" is not a prime number");
Console.WriteLine("Please enter a prime number");
q = BigInteger.Parse(Console.ReadLine());
} while (result == 0);
}
static void Main(string[] args)
{
BigInteger p; // = 61; //value of p
Console.WriteLine("Enter a prime number for p");
p = BigInteger.Parse(Console.ReadLine());
result(p);
BigInteger q; //53; //value of q
Console.WriteLine("Ener a prime number for q");
q = BigInteger.Parse(Console.ReadLine());
resultq(q);
result(p);
BigInteger n = p * q; // calculation for n
Console.WriteLine("p = " + p);
Console.WriteLine();
Console.WriteLine("q = " + q);
Console.WriteLine();
Console.WriteLine("n = " + n);
}
}
}
Apart from the fact that many things in the code could be improved (like naming, return values, p and q mistaken in Main) the main problem lays here. You are incrementing i to p/2 and right after that you are checking if (i == p) which is never going to be true (except for p = 0)
static int Check_Prime(BigInteger p)
{
int i;
for (i = 2; i <= p /2; i++)
{
if (p % i == 0)
{
return 0; //not a prime number
}
}
if (i == p) // never true except p == 0
{
return 1;
}
return 0; // always returns 0
}
Simply remove the if statement and return 1.
static int Check_Prime(BigInteger p)
{
if (p <= 1) return 0;
for (int i = 2; i <= p /2; i++)
{
if (p % i == 0)
{
return 0; //not a prime number
}
}
return 1;
}

how to list roots of number in c#

I am trying to write a code that lists roots of given number.
This is what I did so far. The result I get is 2*2*5*5 which is true but I want to get this instead: 2^2*5^2.
public partial class Form1 : Form
{
List<int> divisor;
public Form1()
{
InitializeComponent();
}
private void list_Click(object sender, EventArgs e)
{
int number;
divisor = new List<int>();
showroot.Text = "";
number = Int32.Parse(usernum.Text);
for (int i = 2; i <= number; i++)
{
if (number % i == 0)
{
divisor.Add(i);
number = number / i;
i = 1;
}
}
for (int i = 0; i < divisor.Count; i++)
{
print(""+ divisor[i]);
}
}
private void print(String text)
{
if (showroot.Text != "")
{
showroot.Text = showroot.Text + "*" + text;
}
else
{
showroot.Text = text;
}
}
}
I tried to check how much same root and count them by two for statements nested but that brings another errors within.
for (int i = 0; i < divisor.Count; i++) {
for (int a = 0; i < divisor.Count; a++) {
if (i == a) {
base[i]++;
}
}
}
What to do?
Split the task into easy to implement portions, extract methods:
First of all, let's collect all prime divisors (divisors can repeat):
private static IEnumerable<int> AllPrimeDivisors(int value) {
if (value <= 1)
yield break;
for (; value % 2 == 0; value /= 2)
yield return 2;
int n = (int)(Math.Sqrt(value) + 0.5);
for (int d = 3; d <= n; d += 2) {
while (value % d == 0) {
yield return d;
value /= d;
n = (int)(Math.Sqrt(value) + 0.5);
}
}
if (value > 1)
yield return value;
}
Then combine them in required format (we should GroupBy the same - repeating - divisors and represent them either in divisor or in divisor^power format)
private static string Solve(int value) {
var terms = AllPrimeDivisors(value)
.GroupBy(divisor => divisor)
.Select(group => group.Count() == 1
? $"{group.Key}"
: $"{group.Key}^{group.Count()}");
return string.Join("*", terms);
}
Finally add UI:
private void list_Click(object sender, EventArgs e) {
if (int.TryParse(usernum.Text, out var number))
showroot.Text = Solve(number);
else
showroot.Text = "Incorrect Input, Syntax Error";
}
Tests:
int[] tests = new int[] {
3, 5, 9, 12, 16, 41, 81, 100,
};
var result = tests
.Select(item => $"{item,3} == {Solve(item)}");
Console.Write(string.Join(Environment.NewLine, result));
Outcome:
3 == 3
5 == 5
9 == 3^2
12 == 2^2*3
16 == 2^4
41 == 41
81 == 3^4
100 == 2^2*5^2
A naive implementation would be by changing your for to this:
for (int i = 2; i <= number; i++)
{
count = 0;
while (number % i == 0)
{
number = number / i;
count++;
}
if (count > 0)
{
divisor.Add(i);
powers.Add(count);
}
}
However a lot of optimizations can be done.

Issue with divisors finding function. Expected is <System.Int32[2]>

I have an issue with the following one :
Create a function named divisors/Divisors that takes an integer and returns an array with all of the integer's divisors(except for 1 and the number itself). If the number is prime return the string '(integer) is prime' (null in C#) (use Either String a in Haskell and Result, String> in Rust).
My code:
static int[] divisors(int a)
{
int[] array = new int[a];
int x = 0;
for( int i =2; i<a; i++)
{
if(a % i == 0)
{
array[x] = i;
x++;
}
}
if(array.Length == 0)
{
return null;
}
else
{
return array;
}
}
When I try to run it, it throws :
"Expected is <System.Int32[2]>, actual is <System.Int32[15]>
Values differ at index [2]
Extra: < 0, 0, 0... >"
Not sure what to do with this one.
I'd really appreciate some help.
The sollution :
using System.Collections.Generic;
public class Kata
{
public static int[] Divisors(int n)
{
List<int> numbers = new List<int>();
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
numbers.Add (i);
}
}
if (numbers.Count == 0)
{
return null;
}
else
{
int[] array = new int[numbers.Count];
array =numbers.ToArray();
return array;
}
}
}
I did not get that error with your code; however, your function will not return null because you have determined the Length of your array as a. So the length will never be zero. You get all those zeros because value types initialize for you. Your int array filled up with zeros. I think this excercise should prove to you that a different collection type would be more suited to data that needs to be dynamicly resized but if you are required to stick to arrays, here is some code that does.
static int[] divisors(int a)
{
int x = 0;
int[] array = new int[x];
for (int i = 2; i < a; i++)
{
if (a % i == 0)
{
x++;
Array.Resize<int>(ref array, x);
array[x-1] = i;
}
}
if (array.Length == 0)
return null;
else
return array;
}
private void button1_Click(object sender, EventArgs e)
{
int b = Int32.Parse(textBox1.Text);
int[] a = divisors(b);
if (a == null)
MessageBox.Show($"{b} is a prime number.");
else
{
foreach (int x in a)
{
Debug.Print(x.ToString());
}
}
}

Find combination of numbers with backtrack

I'm looking for a backtrack algorithm in C# that will search the correct numbers from a List<int> where the sum of these numbers is closest to X.
e.g: list={5,1,3,5}, X = 10 the output should be (5,5) (5+5 is the closest to 10)
it cant be (3,3,3,1) because I can't use a number more than once from the List. (if we have two piece from number 3 than we can use two times)
e.g.2: list={4,1,3,4}, X=10 the output should be {4,1,3} and {1,3,4}.
I got this kind of code to start, but i cant do it;
(I know there are wikipedia about backtracking, and knapsack, but it doesn't help me)
static void BackTrack(int lvl, bool Van, int[] E)
{
int i = -1;
do
{
i++;
if (ft(lvl, i))
{
int k = 0;
while (k < szint && fk(E[i], E[k]))
{
k++;
}
if (k == szint)
{
E[k] = R[lvl,i];
if (lvl == E.Length - 1)
{
}
else
{
BackTrack(lvl + 1, Van, E);
}
}
}
}
while (i < E.Length - 1);
}
static bool fk(int nr, int nr2)
{
return (nr + nr2 <= 10);
}
static bool ft(int lvl, int nr)
{
return true;
}
From what i am reading, this example:
e.g.2: list={4,1,3,4}, X=10 the output should be {4,1,3} and {1,3,4}.
output should be {4,1,4} 9 is closer then 8.
Here is what i did. it works with the two examples you gave.
public List<int> highest(List<int> list, int number)
{
//probably a better way to do this
IEnumerable<int> orderedList = list.OrderByDescending(item => item);
var currentNumber = 0;
List<int> combinationResult = new List<int>();
foreach (var item in orderedList)
{
var temp = currentNumber + item;
if (temp <= number)
{
combinationResult.Add(item);
currentNumber = temp;
}
}
return combinationResult;
}

Categories

Resources