Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
The scope is to output the numbers from 1 to (input number) with the multiples of 3 being a * each. E.g. for 8 as input: 1,2,*,4,5,*,7,8. I tried to do this with a for loop but it has no output for some reason.
using System;
using System.Collections.Generic;
namespace HelpPleaseStackOverFlow
{
class Program
{
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
int x;
for (x = 1; x <= number; )
if (x % 3 == 0)
{
Console.WriteLine("*");
}
else;
{
Console.WriteLine(x);
x = x++;
}
}
}
}
I tried converting the x variable (integer) to a string and then making it the * that should be printed out, in the first half of the IF statement and the last part with the ELSE basically the same.
I also tried to make the for statement like this:
for (x = 1; x <= number; x++)
with the x++ at the end but this just game me the result of ,,8. With an input of 7; 8 should not be part of this and I have no idea why it put 8 as an output.
most of the erroros are that you are lacking some {} and ()
String input = Console.ReadLine();
int number;
int.TryParse(input, out number);
for (int x = 1; x <= number; ++x)
{
if ((x % 3) == 0)
{
Console.WriteLine("*");
}
else
{
Console.WriteLine(x);
}
}
Console.ReadLine();
Hope it helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
For a given input n, the task is to find the largest integer that is <= n and has the highest digit sum.
For example:
solve(100) = 99. Digit Sum for 99 = 9 + 9 = 18. No other number <= 100 has a higher digit sum.
solve(10) = 9
solve(48) = 48. Note that 39 is also an option, but 48 is larger.
Input range is 0 < n < 1e11
What have I tried?
I tried 2 methods. Firstly, I tried getting each digit with Math operations like this:
public static long solve(long n)
{
var answer = 0;
var highestSum = 0;
for (var i = 1; i <= n; i++)
{
var temp = i;
var sum = 0;
while (temp > 0)
{
sum += temp % 10;
temp /= 10;
}
if (sum >= highestSum)
{
highestSum = sum;
answer = i;
}
}
return answer;
}
My second try, I tried using Linq extensions, like this:
public static long solve(long n)
{
var answer = 0;
var highestSum = 0;
for (var i = 1; i <= n; i++)
{
var sum = i.ToString().Sum(x => x - '0');
if (sum >= highestSum)
{
highestSum = sum;
answer = i;
}
}
return answer;
}
Both of my solutions seem to return the correct value and work for smaller values, but for larger input, they seem to take a very long time to execute. How to make it run through numbers faster? Is there a specific algorithm for this task, or am I doing something else wrong?
We can achieve this O(number of digits in n)
We can achieve this if we iteratively reduce a digit and change all other digits on its right to 9.
Let n be our current number.
We can find next number using the below :
b is a power of 10 to represent position of current digit. After every iteration we reduce n to n/10 and change b to b*10.
We use (n – 1) * b + (b – 1);
For eg, if the number is n = 521 and b = 1, then
(521 – 1) * 1 + (1-1) which gives you 520, which is the thing we need to do, reduce the position number by 1 and replace all other numbers to the right by 9.
After n /= 10 gives you n as 52 and b*=10 gives you b as 10, which is again executed as (52-1)*(10) + 9 which gives you 519, which is what we have to do, reduce the current index by 1 and increase all other rights by 9.
static int findMax(int x)
{
int b = 1, ans = x;
while (x!=0)
{
int cur = (x - 1) * b + (b - 1);
if (sumOfDigits(cur) >= sumOfDigits(ans) && cur > ans))
ans = cur;
x /= 10;
b *= 10;
}
return ans;
}
int sumOfDigits(int a)
{
int sum = 0;
while (a)
{
sum += a % 10;
a /= 10;
}
return sum;
}
The accepted answer is brilliant, but I was dead-set on figuring out a way to determine the correct answer without actually summing the digits and comparing the sums to each other.
I tried a few things (as you can see if you look at the edit history), but I couldn't find the formula. In desperation, I wrote a utility to show me all the numbers from 1 to 9999999 that did not have a smaller number with a larger sum to see what pattern I was missing by not looking on a large enough scale.
I was somewhat surprised that only 253 numbers out of the first 10 million have the largest sum compared to their lessers! Somehow I thought that number would be bigger.
Also, it turns out that there is an obvious pattern that appears fairly quickly, and it remained constant for 10 million iterations, so I think it's a good one.
Here's a small sample of some blocks of consecutive output:
0,1,2,3,4,5,6,7,8,9,
18,19,28,29,38,39,48,49,
58,59,68,69,78,79,88,89,98,99,189,198
8899,8989,8998,8999,
9899,9989,9998,9999,
18999,19899,19989,19998,19999
98999,99899,99989,99998,99999,
189999,198999,199899,199989,199998,199999
7899999,7989999,7998999,7999899,7999989,7999998,7999999,
8899999,8989999,8998999,8999899,8999989,8999998,8999999,
9899999,9989999,9998999,9999899,9999989,9999998,9999999
It's so obviously clear!
If the number is one digit, then it's the highest.
If all but the first digit are either all 9's or all 9's with a single 8, then it's sum is the highest.
Otherwise the highest number is the one whose first digit is one less than the original, followed by all 9's.
Here's a code implementation:
public static long Solve(long n)
{
if (HasValidSuffix(n)) return n;
long firstDigit;
int numDigits;
// Loop to determine the first digit and number of digits in the input
for (firstDigit = n, numDigits = 1; firstDigit > 9; firstDigit /= 10, numDigits++) ;
return Enumerable.Range(0, numDigits - 1)
.Aggregate(firstDigit - 1, (accumulator, next) => accumulator * 10 + 9);
}
// Returns true for positive numbers less than 10 or
// numbers that end in either all 9's or all 9's and one 8
public static bool HasValidSuffix(long input)
{
var foundAnEight = false;
for (var n = input; n > 9; n /= 10)
{
var lastDigit = n % 10;
if (lastDigit < 8) return false;
if (lastDigit == 9) continue;
if (foundAnEight) return false;
foundAnEight = true;
}
return true;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
int arraySum (int [] a, int n)
{
int sum = 0;
n = a.size();
for (int i = 1; i < n; i++)
sum += a[i];
return sum;
}
I want to convert this code from iterative to recursive.
C# Version:
int arraySum ( int [] a, int sum = 0, int i = 0 ) /*i = 0, technically means this code is logically different from yours, however it will count every element and is just a default :)*/
{
if( i < a.Length )
return arraySum( a, sum + a[i], ++i );
return sum;
}
You need:
1- Recursive definition like: sum(n) = n + sum(n-1)
2- You need to specify where should you stop so the recursion does not last forever.
for example: if (n == 0) return 0;
based on this you can code at any language.
C++ Example:
int arraySum (int a[], int n)
{
if(n==1)
return a[n-1];
else
return a[n-1] + arraySum (a, n-1);
}
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 5 years ago.
Improve this question
How would you express this loop in C# as a mathematical expression?
private string FormatBytes(long bytes)
{
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = bytes;
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}
return String.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
}
You can calculate this mathematically by first working out the nearest smaller power of 1024 to the number:
int power = (int) Math.Log(bytes, 1024)
Then you can limit that number to the number of suffixes so you don't go past the end of the array:
int power = Math.Min(Suffix.Length-1, (int) Math.Log(bytes, 1024));
Then you work out what you should divide the original number by based on that power:
double div = Math.Pow(1024, power);
Then you can format the string using the suffix for the specified power of 1024:
return string.Format("{0:f1}{1}", bytes / div, Suffix[power]);
Putting this all together (and throwing in "PB" for petabytes):
private string FormatBytes(long bytes)
{
string[] Suffix = { "B", "KB", "MB", "GB", "TB", "PB" };
int power = Math.Min(Suffix.Length-1, (int) Math.Log(bytes, 1024));
double div = Math.Pow(1024, power);
return string.Format("{0:f1}{1}", bytes / div, Suffix[power]);
}
Et voila! Calculated mathematically without using a loop.
(I bet this isn't measurably faster than the loop though...)
If you wanted to you could extend the suffix array to include "exobyte" and then it would work nicely all the way to int.MaxValue, which is 8.0EB.
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 8 years ago.
Improve this question
How can I improve the below algorithm for the following problem?
A stream of numbers is received.
At some point in the stream, numbers begin arriving which have the decimal point in the wrong place
After a period of time the error disappears and correct values enter the stream.
How can I correct the numbers with the decimal point in the wrong place?
Note that the incorrect numbers are NOT out by exactly a multiple of ten. Here are example streams:
It can be assumed that numbers will always be greater than zero and that they differ by less than 50% from one value to the next.
11.111
11.112
11.113
1111.4 // wrong
1111.5 // wrong
11.116
11.117
0.000011111
0.000011112
0.000011113
111.14 // wrong
111.15 // wrong
0.000011116
0.000011117
0.000011111
0.000011112
0.000011113
0.0011114 // wrong
0.0011115 // wrong
0.000011116
0.000011117
Here's the code I would like to improve:
static double lastValue = -1;
static void OnValue(double value) {
if (value <= 0)
return;
if (lastValue != -1) {
var tooHigh = lastValue * 9.5;
var tooLow = lastValue * 0.15;
while (value >= tooHigh)
value /= 10;
while (value <= tooLow)
value *= 10;
}
lastValue = value;
// process value
}
Assuming that the source is
IEnumerable<Double> source = ...
You can use Linq to check and correct decimal point:
// Power we're looking for
int power = 0;
var result = source.Select((v, index) => {
Double p;
if (index <= 0) {
if (v == 0)
power = 1; // <- if 0 is the first number I assume x.yz... is the answer
else {
p = Math.Log10(Math.Abs(v));
power = p < 0 ? (int) (p - 0.5) : (int) (p + 0.5);
}
return v;
}
if (v == 0) // <- Nothing can be done with zero...
return v;
p = Math.Log10(Math.Abs(v));
var actual = p < 0 ? (int) (p - 0.5) : (int) (p + 0.5);
if (actual == power)
return v;
else
return v * Math.Pow(10.0, power - actual);
});