Multiplication of two 64 digit nos [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I multiply two 64 digit decimal numbers together in C# without overflowing?

If you need 64 decimal digit numbers, and if they are integers, you can use System.Numerics.BigInteger struct (needs a reference to System.Numerics.dll assembly).
BigInteger firstNumber = BigInteger.Parse("63518439492097324687235465876298368764576527346564625480");
BigInteger secondNumber = BigInteger.Parse("84890247648975285765484902890273086475254764765147643611");
BigInteger product = firstNumber * secondNumber;

You could always use decimal to store the result of multiplication of two long numbers.
long bigNumber = 12345678L;
long anotherBigNumber = 23456789L;
decimal result = bigNumber * anotherBigNumber;

I think you can do the calculation of any number (as long as you want to) IF first that number you convert into string (so two separate string) than get the last number (character) and multiply it with every number from the other character (just like you learned in school) and so on and you add that to you final score number (also as a string). I know this can be done and it would be king difficult logic at the beginning but once you create this function you can multiply any number you want :)

If they are integers, use the BigInteger class in System.Numerics (Reference System.Numerics.dll!)
If not, there is a BigRational class in Codeplex. It was meant to be in System.Numerics but it got kicked. Link:
http://bcl.codeplex.com/releases/view/42782

Related

Function that returns an array of integers with 1000 elements containing the values 1 to 1000 in random order [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Could someone do this for me please.
Write a function that returns an array of integers with 1000 elements containing the values 1 to 1000 in random order. No number can be repeated or ommitted.
Here is the hint to solve the above question: use Random rnd=new Random() to create a new instance of the random number generator and rnd.Next(1,1000) to get a random integer value between 1 and 1000.
Many Thanks.
I suspect your examiner is probably looking for a Fisher-Yates Shuffle, so the following answer is probably too concise.
Random r = new Random();
public int[] GetArr()
{
return Enumerable.Range(1, 1000).OrderBy(_ => r.Next()).ToArray();
}
did you try :
-generate a random number
-check if he is allready in the tab
-put it in your array,in first free position
the only problem will be that it will drastycally slow down on the end
You might want to try something where you create a class which contains an int and a bool, the bool can be "isused", then the int can be the index, then use a random number generator, get the number associated with the output, then check if that number is in use by an array of your class, if not then push it onto a list of ints, then once completed return the list as an array?

Character Shuffling and Re-Arranging in c# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
In my one application i want to shuffle string contents and in another application if i pass this shuffled string as input then that application has to return original string value.Is there any method is available to do this in dotnet plateform?
you can use Fisher–Yates shuffle algorithm.
it is an algorithm for generating a random permutation of a finite
set—in plain terms, for randomly shuffling the set.
The basic method given for generating a random permutation of the numbers 1–N goes as follows:
Write down the numbers from 1 to N.
Pick a random number k between one and the number of unstruck
numbers remaining (inclusive).
Counting from the low end, strike out the kth number not yet struck
out, and write it down elsewhere.
Repeat from step 2 until all the numbers have been struck out.
The sequence of numbers written down in step 3 is now a random
permutation of the original numbers.
there is an example : shuffle algorithm
and good one here
Definitely not out of the box, but create yourself a struct
struct ShuffleChar{
char c;
int index;
}
and when you shuffle your letters, assign each letter an index so you can put them together again. Remember, a string only makes sense if the letters are in the correct order, changing that order will destroy that information and with high probability you cannot restore it...

Solving project euler number 6 in C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to solve the Project Euler problem number 6 with the following program:
double counter = 1;
double sumsquare = 0;
double squaresum = 0;
while (counter <= 100)
{
sumsquare += Math.Pow(counter, 2);
squaresum += counter;
counter++;
}
Math.Pow(squaresum, 2);
Console.WriteLine("the sum is {0} ,the square is :{1}", squaresum.ToString(), sumsquare.ToString());
double diff = sumsquare - squaresum;
Console.WriteLine(diff.ToString());
Console.ReadLine();
However, the answer was wrong. Can anyone tell me what the problem is with my code?
You have an error in that you are computing a power and then discarding it.
More generally, you should avoid double when solving PE problems that involve integers. Doubles are only accurate to fifteen decimal places; after that, they round off.
Better types to use are long, which can represent integers up to 9,223,372,036,854,775,807 or decimal, which can represent integers up to 79,228,162,514,264,337,593,543,950,335, or BigInteger, which can represent arbitrarily large values.
Note that code typically gets slower as you use the larger and larger types.
Even more generally, now would be a good time to learn how to use a debugger. Figure out what the output of your program should be for a very small case that you can calculate by hand. Then step through your program line by line and see where it goes wrong. That is a far more efficient way to debug a problem than asking the internet.
You seem to think that Math.Pow(squaresum, 2); modifies squaresum. That's not so: it returns a square of squaresum, and you should assign it to squaresum if you want the variable to be modified.
squaresum = Math.Pow(squaresum, 2);
Also the idea of using double type here is not quite good, but it appears that you won't lose enough precision here to get a wrong result.

Is an int in C# 32 bits? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have just heard that the size of an int is machine dependant. I have always thought that the size of an int is 32 bits. Can someone explain this conundrum?
In C#, the int type is always an Int32, and is always 32 bits.
Some other languages have different rules, and int can be machine dependent. In certain languages, int is defined as having a minimum size, but a machine/implementation specific size at least that large. For example, in C++, the int datatype is not necessarily 32 bits. From fundimental data types:
the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always one byte in size.
However, .NET standardized this, so the types are actually specified as Int32, Int64, etc. In C#, int is an alias for System.Int32, and will always be 32 bits. This is guaranteed by section 4.1.5 Integral Types within the C# Language Specification, which states:
The int type represents signed 32-bit integers with values between –2147483648 and 2147483647.
MSDN states that int is 32 bits. Was the person talking about C++ perhaps?
In C++ size of integral types in bytes is unspecified by the standard
From the C# Language Specifications:
The int type represents signed 32-bit integers with values between
–2147483648 and 2147483647.
int is an alias of System.Int32. You also can use System.Int16, System.Int64 and BigInteger.

Creating long randoms [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
So I have this
Random random1 = new Random();
int intrandom1 = random1.Next();
I want to put a long after the .Next. How do I do that? It only accepts ints.
First idea: A long as 64 bit integer, is the combination of 2 32 bit integers, so you can use:
((long)random1.Next() << 32) | random1.Next()
Or perhaps
((long)random1.Next() <<< 32) | random1.Next()
if you use java (?) and need a unsigned shift
edit: does not look like Java. Java has random1.nextLong() for that. Perhaps C#? I do not know that
It can be generated arbitrary long number by using a simple linked list. Just imagine that every node from list can store a number random generated, and a function can be read that list like an unitary number. Using an algorithm like that you can obtain an arbitrary long random number.

Categories