Good excuse! Lol.
Anyone want to help me with a quick program? Iām trying to write a program in C that would add up and display all the sequences of consecutive intergers that add up to 1,000,000. I got the idea of how to write a loop and display the numbers, but Im not understanding how to write the program know when to skip the numbers that add to that wont help it add to 1,000,000
int targetNum = 1000000;
for(int i = 1; i < targetNum/2; i++)
{
int sum = i;
for(int j = i + 1; j < targetNum/2; j++)
{
sum += j;
if(sum == targetNum)
//output here all ints from i to j, add up to 1,000,000
else if (sum > targetNum)
break;
}
}
something like this?
Logically, you only need to calculate up to half of your target num, as 1/2 of your target num + 1 more than that would be over your target sum, so that cuts your range in half.
If you hit your target num, the code can output all the values from i to j
If you go over your target num, break out of the inner loop, let the outer loop increment, and it starts again with the next number
Assuming Iām understanding the problem correctly
That sounds like an incredibly impractical program. Did a teacher ask you to write it or are you doing it for something else?
We are learning to do that for Data and File Structures as a way of understanding how to make efficient code.
It sounds similar to what we looked at in class, which is given a sequence of numbers, whatās the greatest subsequence sum, allowing negative numbers in the sequence.
Though in his case, its only positive numbers. His problem is a bit more trivial without negatives though.
It seems like there should be some clever mathematics to cut down the problem, though I havenāt quite gotten there.
The sum of the numbers 1 through n is (n)(n-1)/2, so the sum of the numbers m through n is (n)(n+1)/2 - (m-1)(m-1+1)/2. So if you have a good way of estimating m based on n youāre in good shape, and if you can do better than estimate youāre money. I think n-m ā 1,000,000/((n+m)/2) should be a good start, which yields m = sqrt(nĀ²+2*1,000,000). (Or perhaps substitute m with m-1? Those results look better.) Maybe itās enough to just check whether thatās an integer, or maybe you just restrict your search to numbers in the vicinity. If you have a pair of m and n that you know gives the right solution it should be pretty easy to figure out (?).
(Note that n=5100 is giving me an integer m, but not the right sum, so I seem to be slightly off.)
Although if I remembered more from disco (and learned it better in the first place) thereās probably some funky way to arrive at an exact solution.
How icy is going about it seems like the better approach. Of course, math is always best solved by math. Iāve personally never come across a file/directory based algorithm involving such a brute force method of solving.
I could envision something along the lines of āplease give me 50 MiB to store a file and try not to fragment things up too much.ā They probably have clever algorithms for that sort of thing, though Iād think they tend to give good solutions quickly rather than brute-force and find a perfect solution.
But the problem does sound more like an exercise in control statements to me. That or a setup for comparing the brute-force approach to a mathematical solution.
I always assume if somebody is asking for help in answering such an impractical problem, itās usually an assignment so the answer should be tailored accordingly
And I remember learning about some of the data storage algorithms in some of my classes, theyād show us the drive and what blocks are free, and we had to write different ways to store them, and compare how efficient each one was. Stuff like take the first available space, take the first space with the proper size, etc. Was interesting because sometimes the overhead of calculating where to put something actually made some of the dumber algorithms work better in the real world. I miss conceptual stuff like that sometimes
The one thing Iām more curious is, are you actually doing this in plain old C? Or C++?
started taking some programming classes and i donāt know the first thing about it.
is programming really that hard? iām feeling a little overwhelmed.
Of course itās hard, thereās a reason that itās a highly paid position. Even within people who know how to program, there are various tiers of programming skills. Thereās also a reason the graduation rate for Computer Science degrees is so low, people struggle and drop out eventually. But itās not impossible, and once it clicks, you should be good to go.
The thing to remember, is itās all logic and problem solving. Before you even think about writing a line of code, you just have to think logistically about how to solve the problem. Break it down into achievable chunks. Then start coding each chunk.
Early programming assignments are small enough to just be one chunk, so if youāre struggling with this then I suggest just keep practicing. But unless you actually enjoy the act of writing code, I wouldnāt suggest getting into it as a career. I personally love it, which is why I answer peoples programming questions off the cuff in these threads. Itās not for everyone
Programmingās not hard, some people just donāt want to put the time into learning it. Then again, when youāre genuinely interested in something, the difficulty curve tends to be transparent.
Iād recommend looking into the underlying rules. Logic branches, syntax, variable declarations, variable usage, etc. Once you start to see patterns in how programming works, creating algorithms to solve āproblemsā becomes a piece of cake.
How strong are you in math or logic in general?
You might improve those skills too, while you are trying this.
If itās hard for you, then itās hard. Will it get easier? Maybeā¦
When it comes to algorithms, thereās a degree of āIāve seen this trick before,ā but for the basics, if you just donāt āgetā it I donāt know what to say. That probably makes me a bad teacher, but Iāve seen mechanical engineersāsmart people to be sureājust struggling so hard over something like a for loop.
Is it worth learning? Sure. A little bit of scripting can make your life a whole lot easierāand you donāt need to be a master programmer to do that.
But if you find that you donāt enjoy it then I would look elsewhere for a career.
āMost people seem to be born without the part of the brain that understands pointer.ā -Joel Spolsky
thanks for the feedback well iām pretty good at math and logic.
while i donāt feel super passionate about it, i am willing to learn.
idk iāll see how it turns out at the end of the semester.
iāll be visiting this thread a lot more often.
There was a significant amount of grunt work before I thought computer science became both fun and interesting, much like Math classes. A lot of times it was tedious, other times it was a fun learning experience, other times it was really fucking hard.
One of the biggest turning points was when I was learning a lot more about computer architecture (how does a computer really run a program, hardware+OS) and analysis of algorithms (math). Iāll skip over the details, but it took me to the 5th and 6th quarter of University to really get there. I was enjoying programming, but I wasnāt gaining a fundamental understanding of what I was doing until that point.
One thing I learned during these classes thatās been very useful:
I pretend to explain out loud to someone, what my code is supposed to be doing. Be as detailed as possible. I have an imaginary conversation in my head as if I was explaining to the Teaching Assistant what each line was meant to do, what each function was meant to do. This is a way to make sure you understand what the fuck you just wrote down.
Architecture and Algorithms is what Iāve been doing this semester and last, respectively. They are much more fun to learn about, though we arenāt too far in Architecture just yet, than just what what does what in java. Also learning C, in vi no less.
Also that last paragraph sounds like a really awesome practice. Iām going to have to steal that. On another note I finally figured out my stupid SQL injection project so I donāt have to worry about that for the rest of the week so more FTL tonight!!
If you ask me, an understanding of what is required for programming to work before actually programming would help students in the long run. Otherwise their mindset walking into class is that words make magic happen and specific words perform better magic.
Problem is, people were dropping out of programs in the first few semesters back when C/C++ were the starting languages, because it was too boring. You can learn cin, cout, etc but all youāre doing is making stuff appear in a console to start. People dropped out.
So now most intro classes teach Java, and jump right into making cooler stuff happen off the bat (alpha image overlays) that is more flashy. Benefit is attendance and enrollment increased. Downside is now people donāt understand getting in there and actually doing stuff by yourself, relying on libraries. Instead of 4 years of C++ with 1-2 Java classes, now its 3-4 years of java with 1 C++ class. And then people canāt understand pointers.
Fine for me professionally, since that means less C++ guys to compete with. At least this is what I gathered from talking with some of my old professors.
So the downside is, no time for fundamentals, you have to wow them off the bat and keep them interested in the subject