CheckEmoji Community · the emoji forum
🏠 Home 🆕 What's new ❓ Unanswered 🔥 Popular 📡 RSS Members 👥 0 online log in · register
Home › IT › IT Support › Algorithms help!

Algorithms help!

Started by wiredfalcon94 · · 👁 4 views · 8 replies

📡 Subscribe to replies

Participants wiredfalcon94William Richardson12Andrew Peterson2ironcyclist58Jack HayesJamie Green5
wiredfalcon94 wiredfalcon94 NewcomerOP
1 message
joined Sep 2017
#1 ·
So, I have an exam tomorrow. Based on what I've heard, they’ll probably throw one of these three problems at me, which should be enough to pass. If anyone here knows how to tackle these, I’d REALLY appreciate the help!

1. Print the first 10 numbers up to 10,000 where the sum of the digits is greater than 10
2. Print the first 10 numbers up to 10,000 where the sum of the digits is less than 10
3. Print the first 10 numbers up to 10,000 where the sum of the digits equals 10

🙄🙄🙄
William Richardson12 William Richardson12 Regular
301 messages
joined Aug 2014
#2 ·
It’s pretty straightforward... just loop through the digits, sum them up, and check if it hits the >,
Andrew Peterson2 Andrew Peterson2 Active Member
148 messages
joined May 2013
#3 ·
You could've helped him more—honestly, you probably would've just done the whole thing yourself if it were actually that simple anyway...

Sent from my ASUS_Z00AD via Reddit
ironcyclist58 ironcyclist58 Regular
863 messages
joined Feb 2020
#4 ·
Glad he specified which language we’re working with here...
William Richardson12 William Richardson12 Regular
301 messages
joined Aug 2014
#5 ·
Andrew Peterson2 said:You could've helped him more—honestly, you probably would've just done the whole thing yourself if it were actually that simple anyway...

Sent from my ASUS_Z00AD via Reddit

If learning how to code means you've mastered elementary math... then sure. The logic behind this whole thing is basically middle school level stuff. ☕
ironcyclist58 ironcyclist58 Regular
863 messages
joined Feb 2020
#6 ·
William Richardson12 said:It’s pretty straightforward... just loop through the digits, sum them up, and check if it hits the >,

If you really want to push yourself, try rewriting that digit summation function using recursion instead... 😉
ironcyclist58 ironcyclist58 Regular
863 messages
joined Feb 2020
#7 ·
Andrew Peterson2 said:You could've helped him more—honestly, you probably would've just done the whole thing yourself if it were actually that simple anyway...

Sent from my ASUS_Z00AD via Reddit

Alright, here’s the breakdown:

1. Write a function to sum up the digits (looping or recursion—whatever works). Pro tip: lean on the modulo operator.
2. In your main block, set up a loop that starts at 10,000 and just keeps going.
3. For every iteration, call that digit-sum function and check if the result is greater than, less than, or equal to 10 (just follow whatever the specific prompt asks for).
4. If it hits that condition, bump your counter up by one and print the number.
5. Once that counter hits 10, kill the loop...
Jack Hayes Jack Hayes Newcomer
7 messages
joined Nov 2008
#8 ·
Here's some C# code—it starts at 10,000, and for every number after that, it adds up the digits. It sorts them into three groups: exactly 10, more than 10, or less than 10. The loop stops once we've found 10 numbers for each category or if we hit 1,000 iterations 🙂. Finally, it prints out three lines containing those numbers
. // digit sum
private static void DigitSumTask()
{
int loopCounter = 0;
bool shouldExit = false;
int sum = 0;
int startingNumber = 10000;
int indexIs10 = 0;
int indexGreater10 = 0;
int indexLess10 = 0;
int[] sumEquals10 = new int[10];
int[] sumGreaterThan10 = new int[10];
int[] sumLessThan10 = new int[10];

// loop
while (!shouldExit) {
startingNumber++;
// sum digits
string numberStr = startingNumber.ToString();
sum = 0;
for (int i=0; i < numberStr.Length; i++) {
sum += int.Parse(numberStr.Substring(i, 1));
}
// sum is 10
if (sum == 10) {
if (indexIs10 < 10) {
sumEquals10[indexIs10] = startingNumber;
indexIs10++;
}
}
// sum greater than 10
if (sum > 10) {
if (indexGreater10 < 10) {
sumGreaterThan10[indexGreater10] = startingNumber;
indexGreater10++;
}
}
// sum less than 10
if (sum < 10) {
if (indexLess10 < 10) {
sumLessThan10[indexLess10] = startingNumber;
indexLess10++;
}
}
loopCounter++;
// exit if all found or 1000 iterations passed
if ((indexIs10 == 10 && indexGreater10 == 10 && indexLess10 == 10) || loopCounter >= 1000)
{
shouldExit = true;
}
}

// print results
string line1 = "";
string line2 = "";
string line3 = "";
for (int j=0; j < 10; j++) {
line1 += sumEquals10[j] + ", ";
line2 += sumGreaterThan10[j] + ", ";
line3 += sumLessThan10[j] + ", ";
}
Console.Character Limit = 140;
Console.WriteLine("Sum equals 10 for: " + line1);
Console.WriteLine("Sum is greater than 10 for: " + line2);
Console.WriteLine("Sum is less than 10 for: " + line3);
Console.Enter();
}

The main thing you need to focus on is summing those digits, then saving the number and counting until you hit 10☕.
Jamie Green5 Jamie Green5 Member
17 messages
joined Sep 2017
#9 ·
He’s got a midterm tomorrow and he's actually asking me for help. Well, not even really help—he's basically begging me to just finish his assignments so he can copy the answers down during the exam. It makes you wonder if anyone is actually learning anything meaningful in our public school system anymore.

You must log in or register to reply here.

Log in Register

🔗 Similar threads