Float Evaluation returns integer? - c#

I recently started with the euler Project for practice, I completed 1 and 2, but 3 is just not working. I used the same line of code for every solution to check if the number is decimal or not. This worked fine except this time, but I dont know why. This code
for (float i = 0; i < 1000; i++)
{
float temp = i / 3;
float temp2 = i/ 5;
if ((temp % 1) == 0 || (temp2 % 1) == 0)
{
num += i;
}
}
worked perfectly fine, but this one (which is basically the same)
float input = 600851475143;
for (float i = 0; i < 1000; i++)
{
float temp = input/i;
if ((temp % 1) == 0)
{
Console.Write(temp + ", ");
}
just returns every number. Then I tried this
float temp = 10/9;
Console.WriteLine(temp);
But it just returns 1. So i thought of an overflow or something like that, but my next approach didnt work either:
float temp = 10/9;
bool temp2 = (temp%1) == 0;
Console.WriteLine(temp2);
Return: True
I dont now what to do anymore, does someone know why this happens? Thanks in advance.

float in C# has precision of 6-9 digits according to docs, 600851475143 divided by maximum i in the loop (i.e. 999) will have more than 9 digits in mantissa so float will not be able to cover fractional part, so try switching to double or even decimal:
double input = 600851475143;
for (double i = 0; i < 1000; i++)
{
double temp = input / i;
if ((temp % 1) == 0)
{
Console.Write(temp + ", ");
}
}
Also I would say that you can use long's in this case:
long input = 600851475143;
for (long i = 1; i < 1000; i++)
{
var x = (input % i);
if (x == 0)
{
Console.WriteLine(input / i);
}
}
As for the last snippet - 10 and 9 in 10/9 are int's, so the result of 10/9 is an int and equals to 1, which give you the result you get.

Related

Sequence Iteration with Exponent and Factorial Produces Unexpected Results

I attempted a simple task during learning but I'm stuck and have some weird results.
I have to write a simple method with 2 parameters 'x' and 'a'. Result of this method has to be sum of all xk/k! where k starts from 0 and goes to infinity, and 'a' is a parameter of accuracy.
Here how it looks
But I have no idea why when I'm putting Console.Write inside of loop, numbers are acting crazy and I have this for x = 2 and x = 6
Also when I'm trying to put my code into method and use result there is nothing happening.
I have another method for mathematical factorial to calculate k!.
decimal wynik = 0;
int x = 2;
int a = 6;
for (int k = 1; k > 0; k++)
{
if (Algorithms.Factorial(k) > 0)
{
wynik += Math.Round(Convert.ToDecimal(Math.Pow(x, k)) /
Algorithms.Factorial(k), a);
Console.WriteLine(wynik);
}
}
Console.WriteLine(wynik);
and Factorial method
static public int Factorial(int n)
{
int wynik = 1;
for (int i = 1; i <= n; i++)
{
wynik *= i;
}
return wynik;
}
Of course I want to end with method and returning results but for practicing I'm working in Main method
Thanks a lot for help!
Factorial grows very fast, please, look
13! = 6227020800 > int.MaxValue
that's why returning int in static public int Factorial(int n) can well lead to integer overflow and strange results.
x ** k grows fast as well (though not that fast as factorial does). Let these large numbers cancel one another:
double x = 2;
double tolerance = 0.00001;
double result = 1.0; // x**0 / 0! == 1 / 1 == 1
double term = 1.0;
for (int k = 1; term > tolerance; ++k) {
// given term = (x ** (k - 1)) / (k - 1)!
// we can compute next term as
term = term * x / k;
result += term;
}
// Let's have a look (in fact, you have Taylor serie for exp)
Console.WriteLine($"actual : {result} (+/- {tolerance:0.##############})";
Console.WriteLine($"expected : {Math.Exp(x)}"");
Outcome:
actual : 7.3890545668323435 (+/- 0.00001)
expected : 7.38905609893065

How can I extract the places values of a number?

I'm working on a math game in the Unity game engine using C#, specifically a reusable component to teach the grid method for multiplication. For example, when given the numbers 34 and 13, it should generate a 3X3 grid (a header column and row for the multiplier and multiplicand place values and 2X2 for the number of places in the multiplier and multiplicand). Something that looks like this:
My issue is that I don't know the best way to extract the place values of the numbers (eg 34 -> 30 and 4). I was thinking of just converting it to a string, adding 0s to the higher place values based on its index, and converting it back to an int, but this seems like a bad solution. Is there a better way of doing this?
Note: I'll pretty much only be dealing with positive whole numbers, but the number of place values might vary.
Thanks to all who answered! Thought it might be helpful to post my Unity-specific solution that I constructed with all the replies:
List<int> GetPlaceValues(int num) {
List<int> placeValues = new List<int>();
while (num > 0) {
placeValues.Add(num % 10);
num /= 10;
}
for(int i = 0;i<placeValues.Count;i++) {
placeValues[i] *= (int)Mathf.Pow(10, i);
}
placeValues.Reverse();
return placeValues;
}
Take advantage of the way our number system works. Here's a basic example:
string test = "12034";
for (int i = 0; i < test.Length; ++i) {
int digit = test[test.Length - i - 1] - '0';
digit *= (int)Math.Pow(10, i);
Console.WriteLine("digit = " + digit);
}
Basically, it reads from the rightmost digit (assuming the input is an integer), and uses the convenient place value of the way our system works to calculate the meaning of the digit.
test.Length - i - 1 treats the rightmost as 0, and indexes positive to the left of there.
- '0' converts from the encoding value for '0' to an actual digit.
Play with the code
Perhaps you want something like this (ideone):
int n = 76302;
int mul = 1;
int cnt = 0;
int res[10];
while(n) {
res[cnt++] = (n % 10) * mul;
mul*=10;
cout << res[cnt-1] << " ";
n = n / 10;
}
output
2 0 300 6000 70000
My answer is incredibly crude, and could likely be improved by someone with better maths skills:
void Main()
{
GetMulGrid(34, 13).Dump();
}
int[,] GetMulGrid(int x, int y)
{
int[] GetPlaceValues(int num)
{
var numDigits = (int)Math.Floor(Math.Log10(num) + 1);
var digits = num.ToString().ToCharArray().Select(ch => Convert.ToInt32(ch.ToString())).ToArray();
var multiplied =
digits
.Select((d, i) =>
{
if (i != (numDigits - 1) && d == 0) d = 1;
return d * (int)Math.Pow(10, (numDigits - i) - 1);
})
.ToArray();
return multiplied;
}
var xComponents = GetPlaceValues(x);
var yComponents = GetPlaceValues(y);
var arr = new int[xComponents.Length + 1, yComponents.Length + 1];
for(var row = 0; row < yComponents.Length; row++)
{
for(var col = 0; col < xComponents.Length; col++)
{
arr[row + 1,col + 1] = xComponents[col] * yComponents[row];
if (row == 0)
{
arr[0, col + 1] = xComponents[col];
}
if (col == 0)
{
arr[row + 1, 0] = yComponents[row];
}
}
}
return arr;
}
For your example of 34 x 13 it produces:
And for 304 x 132 it produces:
It spits this out as an array, so how you consume and display the results will be up to you.
For two-digit numbers you can use modulo
int n = 34;
int x = n % 10; // 4
int y = n - x; // 30

Palindrome number product of two 3-digit numbers - Project Euler Solution 4 - Brute Force not working

I am using this code to solve problem. 4 on Project Euler. The problem reads:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
I have tried to solve the problem. My code returns a palindrome number and a fairly big one too. But it is incorrect. I have also found a correct solution at MathBlog.dk. But what is wrong with my code? --
long result = 0;
for (long x = 100; x <= 999; x++)
{
for (long y = 100; y <= 999; y++)
{
long num = x * y;
if (num.ToString() == StringFunctions.ReverseString(num.ToString()))
{
result = num;
}
}
}
Console.WriteLine("Result : {0}", result);
The StringFunctions.RevereString function is as follows:
public static class StringFunctions
{
public static string ReverseString(string s)
{
string result = "";
for (int i = s.Length - 1; i >= 0; i--)
{
result += s.Substring(i, 1);
}
return result;
}
}
My code returns 580085 but the correct answer is 906609. I don't want to know about better solutions. I just want to know why my code isn't working.
Any help will be much appreciated. Thanks in advance.
Your result is storing the last palindrome number obtained from the loop but not the largest one.
Both the variables X and Y iterate from 100 to 999
Imagine a case when (assuming all obtained numbers are palindromes) x = 500 and y = 500. It will execute earlier than x = 990 and y = 100. But in the earlier case the palindrome is larger but your code stores the smaller one. Use an if condition to get the largest palindrome:
long result = 0;
for (long x = 100; x <= 999; x++)
{
for (long y = 100; y <= 999; y++)
{
long num = x * y;
if (num.ToString() == StringFunctions.ReverseString(num.ToString()))
{
if(result < num)
{
result = num;
}
}
}
}
Console.WriteLine("Result : {0}", result);

C# - What's wrong with my conversion from double to int?

I keep getting this error:
"Cannot implicitly convert type 'double' to 'int'. An explicit
conversion exists (are you missing a cast?)"
Code:
Console.WriteLine("ISBN-Prüfziffer berechnen");
Console.WriteLine("=========================");
Console.WriteLine();
Console.Write("ISBN-Nummer ohne Prüfziffer: ");
string ISBNstring = Console.ReadLine();
int ISBN = Convert.ToInt32(ISBNstring);
int PZ;
int i;
double x = Math.Pow(3, (i + 1) % 2);
int y = (int)x;
for (i = 1; i <= 12; i++)
{
PZ = ((10-(PZ + ISBN * x) % 10) % 10);
}
Console.WriteLine(PZ);
Console.ReadLine();
Here is the new code:
Console.WriteLine("ISBN-Prüfziffer berechnen");
Console.WriteLine("=========================");
Console.WriteLine();
Console.Write("ISBN-Nummer ohne Prüfziffer: ");
string ISBNstring = Console.ReadLine();
long ISBN = Convert.ToInt32(ISBNstring);
long ISBN1 = (Int64)ISBN;
int PZ = 0;
int i;
for (i = 1; i <= 12; i++)
{
double x = Math.Pow(3, (i + 1) % 2);
long y = (double)x;
PZ = ((10 - (PZ + ISBN * y) % 10) % 10);
}
Console.WriteLine(PZ);
Console.ReadLine();
But I'm still getting a conversion error for double to long and long to int...
I think you meant to use your y variable here instead of x :
PZ = ((10-(PZ + ISBN * y) % 10) % 10);
As a side note, you'll get compilation errors on both PZ and i, you need to initialize their values before using them, e.g. int PZ = 0; and int i = 0;
And please, use meaningful names; PZ, i, x and y don't mean anything to someone reading your code, or even to you in a few weeks.
Okay, I've modified it a little...
Console.WriteLine("ISBN-Prüfziffer berechnen");
Console.WriteLine("=========================");
Console.WriteLine();
Console.Write("ISBN-Nummer ohne Prüfziffer: ");
string ISBNstring = Console.ReadLine();
int sum = 0;
for (int i = 0; i < 12; i++)
{
int digit = ISBNstring[i] - '0';
if (i % 2 == 1)
{
digit *= 3;
}
sum += digit;
}
int result = 10 - (sum%10);
Console.WriteLine(result);
Console.ReadLine();
Here's the changes :
- You can declare i directly in your for loop, it'll save you a line.
- Instead of putting the ISBN into a long, keep in in a string. Just iterate over each character one by one.
- Each digit can be obtained by taking the ASCII value, and removing the value of 0.
- The % 2 == 1 thing is basically "If the number is at an odd position", where you can apply the *3. This replaces your Math.Pow that wasn't very clear.

Sum of digits in C#

What's the fastest and easiest to read implementation of calculating the sum of digits?
I.e. Given the number: 17463 = 1 + 7 + 4 + 6 + 3 = 21
You could do it arithmetically, without using a string:
sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
I use
int result = 17463.ToString().Sum(c => c - '0');
It uses only 1 line of code.
For integer numbers, Greg Hewgill has most of the answer, but forgets to account for the n < 0. The sum of the digits of -1234 should still be 10, not -10.
n = Math.Abs(n);
sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
It the number is a floating point number, a different approach should be taken, and chaowman's solution will completely fail when it hits the decimal point.
public static int SumDigits(int value)
{
int sum = 0;
while (value != 0)
{
int rem;
value = Math.DivRem(value, 10, out rem);
sum += rem;
}
return sum;
}
int num = 12346;
int sum = 0;
for (int n = num; n > 0; sum += n % 10, n /= 10) ;
I like the chaowman's response, but would do one change
int result = 17463.ToString().Sum(c => Convert.ToInt32(c));
I'm not even sure the c - '0', syntax would work? (substracting two characters should give a character as a result I think?)
I think it's the most readable version (using of the word sum in combination with the lambda expression showing that you'll do it for every char). But indeed, I don't think it will be the fastest.
I thought I'd just post this for completion's sake:
If you need a recursive sum of digits, e.g: 17463 -> 1 + 7 + 4 + 6 + 3 = 21 -> 2 + 1 = 3
then the best solution would be
int result = input % 9;
return (result == 0 && input > 0) ? 9 : result;
int n = 17463; int sum = 0;
for (int i = n; i > 0; i = i / 10)
{
sum = sum + i % 10;
}
Console.WriteLine(sum);
Console.ReadLine();
I would suggest that the easiest to read implementation would be something like:
public int sum(int number)
{
int ret = 0;
foreach (char c in Math.Abs(number).ToString())
ret += c - '0';
return ret;
}
This works, and is quite easy to read. BTW: Convert.ToInt32('3') gives 51, not 3. Convert.ToInt32('3' - '0') gives 3.
I would assume that the fastest implementation is Greg Hewgill's arithmetric solution.
private static int getDigitSum(int ds)
{
int dssum = 0;
while (ds > 0)
{
dssum += ds % 10;
ds /= 10;
if (dssum > 9)
{
dssum -= 9;
}
}
return dssum;
}
This is to provide the sum of digits between 0-9
public static int SumDigits1(int n)
{
int sum = 0;
int rem;
while (n != 0)
{
n = Math.DivRem(n, 10, out rem);
sum += rem;
}
return sum;
}
public static int SumDigits2(int n)
{
int sum = 0;
int rem;
for (sum = 0; n != 0; sum += rem)
n = Math.DivRem(n, 10, out rem);
return sum;
}
public static int SumDigits3(int n)
{
int sum = 0;
while (n != 0)
{
sum += n % 10;
n /= 10;
}
return sum;
}
Complete code in: https://dotnetfiddle.net/lwKHyA
int j, k = 1234;
for(j=0;j+=k%10,k/=10;);
A while back, I had to find the digit sum of something. I used Muhammad Hasan Khan's code, however it kept returning the right number as a recurring decimal, i.e. when the digit sum was 4, i'd get 4.44444444444444 etc.
Hence I edited it, getting the digit sum correct each time with this code:
double a, n, sumD;
for (n = a; n > 0; sumD += n % 10, n /= 10);
int sumI = (int)Math.Floor(sumD);
where a is the number whose digit sum you want, n is a double used for this process, sumD is the digit sum in double and sumI is the digit sum in integer, so the correct digit sum.
static int SumOfDigits(int num)
{
string stringNum = num.ToString();
int sum = 0;
for (int i = 0; i < stringNum.Length; i++)
{
sum+= int.Parse(Convert.ToString(stringNum[i]));
}
return sum;
}
If one wants to perform specific operations like add odd numbers/even numbers only, add numbers with odd index/even index only, then following code suits best. In this example, I have added odd numbers from the input number.
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Please Input number");
Console.WriteLine(GetSum(Console.ReadLine()));
}
public static int GetSum(string num){
int summ = 0;
for(int i=0; i < num.Length; i++){
int currentNum;
if(int.TryParse(num[i].ToString(),out currentNum)){
if(currentNum % 2 == 1){
summ += currentNum;
}
}
}
return summ;
}
}
The simplest and easiest way would be using loops to find sum of digits.
int sum = 0;
int n = 1234;
while(n > 0)
{
sum += n%10;
n /= 10;
}
#include <stdio.h>
int main (void) {
int sum = 0;
int n;
printf("Enter ir num ");
scanf("%i", &n);
while (n > 0) {
sum += n % 10;
n /= 10;
}
printf("Sum of digits is %i\n", sum);
return 0;
}
Surprised nobody considered the Substring method. Don't know whether its more efficient or not. For anyone who knows how to use this method, its quite intuitive for cases like this.
string number = "17463";
int sum = 0;
String singleDigit = "";
for (int i = 0; i < number.Length; i++)
{
singleDigit = number.Substring(i, 1);
sum = sum + int.Parse(singleDigit);
}
Console.WriteLine(sum);
Console.ReadLine();

Categories