Dividing BigInteger in C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to divide BigIntegers.
this is my code so far:
BigInteger x = BigInteger.Parse("2697841949839130393229424953460693359375000000");
BigInteger y = BigInteger.Parse("2");
for (int i = 0; i < 300; i++) {
double result = Math.Exp(BigInteger.Log(x) / BigInteger.Log(y));
x = result;
Console.WriteLine(result);
}
It does the divide. it does it only once. I want to divide the code to minimum it has. it should be 6.
I know that this:
for (int i = 0; i < 300; i++){}
is not the right way to do it. does any one know any other way how to?

I remember from your other question that you are trying to decode Godel numbers.
You shouldn't stick the result in a double. It's too big. You're looking for BigInteger's Divide method. For your application DivRem is better.
Here's some sample code. It computes 6. This should get you going for the first character. You'll also need to build a list of primes beyond '2' to continue on to the rest of the string.
using System;
using System.Numerics;
public class Program
{
public static void Main()
{
BigInteger x = BigInteger.Parse("2697841949839130393229424953460693359375000000");
BigInteger y = BigInteger.Parse("2");
int counter = 0;
BigInteger remainder;
do{
BigInteger result = BigInteger.DivRem(x, y, out remainder);
if(!remainder.IsZero)
break;
x = result;
counter++;
} while (true);
Console.WriteLine(counter);
}
}

Related

Sort float numbers List from small to large [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to sort float numbers List from small to large. I tried to use the array.Sort () method but it doesn't work.
Below is my code.
List<float> array = new List<float>();
array.Add(x);
listBox1.Items.Add(x);
Just Sort():
List<float> array = new List<float>();
array.Add(x);
array.Sort();
When array (quite a strange name for List<float>) is sorted, we can insert x somewhere in the middle of listBox1.Items. Asuming that listBox1.Items contains ordered float items only, we can put
// Here's we have WinForms ListBox
int at = listBox1.Items.Count;
for (int i = 0; i < listBox1.Items.Count; ++i) {
if (x < (float) (listBox1.Items[i])) {
at = i;
break;
}
}
listBox1.Items.Insert(at, x);

How do I shorten that specific code in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am a beginner C# learner. I am trying to learn through an app called SoloLearn.
In the app, it wants me to replace 3 and its multiples in a series of numbers with "*".
For example; the input is n number, let's say 7, the output should go "12 * 45 * 7" (without spaces)
I tried this, it worked but these two "ifs" at the end make my eyes bleed.
using System;
using System.Collections.Generic;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
//your code goes here
for (int x = 1; x <= number; x++)
{
if (x%3==0)
Console.Write("*");
if (x%3==0)
continue;
{
Console.Write(x);
}
}
}
}
}
I played around it a little but can't seem to find any way around it.
How would you shorten these only with the content in this code given?
You don't need a continue statement there. Instead of two ifs you could use an if and an else, e.g.:
for (int x = 1; x <= number; x++)
{
if (x % 3 == 0)
{
Console.Write("*");
}
else
{
Console.Write(x);
}
}
For fun:
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
var seq = Enumerable.Range(1, number).Select(x=>x%3==0?"*":x.ToString());
Console.WriteLine(string.Join("", seq));
}
And I could, of course, shorten it further to a single line, but that just seems excessive. There's probably also a way to get an implicit ToString() conversion.
The slightly shorter version of the for loop is this:
for (int x = 1; x <= number; x++)
{
Console.Write(x % 3 == 0 ? "*" : $"{x}");
}

How to count the sum of the numeric found in a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to count sum of the numbers found in a string, not digits. For example, there are string = "abc12df34", and the answer must be 46 (12+34), not 10. Also maybe negative numbers for string = "abc10gf-5h1" answer must be 6. I can not understand how to implement this.
RegEx approach:
string input = "abc10gf-5h1";
int result = Regex.Matches(input, "-?[0-9]+").Cast<Match>().Sum(x =>int.Parse(x.Value));
While the above answer is elegant, it's not really something to understand what to do or how to approach the problem. Here is another, more explicit solution.
In words, you iterate through your string, collect digits as long as there are any, if you find a nondigit, your number is finished, you convert the number to integer and sum it up, clear the number string. The same you do if you find the end of the string. On the next found digit you start collecting digits again.
This algorithm will fail on any number larger than than 10 digits in multiple ways (as the other answer will also), but this is just for demonstration anyway.
string input = "abc10gf-5-1h1";
var number = new char[10];
int numberlength = 0;
int pos = 0;
int sum = 0;
while (pos < input.Length)
{
char c = input[pos++];
if (char.IsDigit(c))
{
number[numberlength++]=c;
}
else
{
if (numberlength > 0)
{
sum += int.Parse(new String(number, 0, numberlength));
numberlength = 0;
}
if (c=='-')
number[numberlength++]=c;
}
}
if (numberlength > 0)
sum += int.Parse(new String(number, 0, numberlength));
Console.WriteLine(sum);

Proper syntax for changing the value of an array in a 'for' loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Okay, I just need to make sure I'm coding this correctly. Please review the following when you have a moment:
int [] Counter_Event = new int [46];
for (int xCount = 0; xCount < Counter_Event.Length; xCount++)
{ Counter_Event[xCount] = Math.Round(xCount * 10000);}
With the above, it's tossing back a compilation error. I'm probably not using proper syntax, but any perspective would help.
Math.Round() requires a demical or a double as a parameter.
Something like this would work:
int[] Counter_Event = new int[46];
for (int xCount = 0; xCount < Counter_Event.Length; xCount++)
{ Counter_Event[xCount] = (int)Math.Round((double)xCount * 10000); }
You need to typecast the parameter of Math.Round() to either to Double or Decimal. Also, since your array Counter_Event is of type int, so again you have to cast the result of Math.Round() to int as Math.Round() return type is either Decimal or Double.
int [] Counter_Event = new int [46];
for (int xCount = 0; xCount < Counter_Event.Length; xCount++)
{
Counter_Event[xCount] = (int)Math.Round((double)(xCount * 10000));
}

I'm trying to do a Prime Number finder but cant see why it's not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to do a Prime Number finder but cant see why it's not working.
When i run the debug test nothing show in the console. Could someone check it and tell me what i do wrong?
List<int> primes = new List<int>();
primes.Add(2);
primes.Add(3);
int maxPrime = 11; //The maximum found Primes
int primeCount = primes.Count; //Current Number of Primes
int num = 4; //Current Number
int x = 0; //
int curPrime = primes[x];
while (primeCount < maxPrime)
{
if (x != primeCount)
{
if (num % primes[x] == 0)
{
num++;
x = 0;
}
else
{
x++;
}
}
else
{
primes.Add(num);
primeCount=primes.Count;
x = 0;
}
}
primes.ForEach(i => Console.Write("{0}\t", i));
You have an infinite loop.
Since you never modify primeCount or maxPrime, this will always be true:
while (primeCount < maxPrime)
In order to end that loop, you need to modify one of those two values in such a way that the condition will evaluate to false.
(Note: There appear to also be other bugs/problems in the code aside from this. For example, num = num++; doesn't do what you probably think it does.)

Categories

Resources