issues with Euler 23 [closed] - c++11

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
I've started solving Euler's problems and there is an error in Euler 23 that DOES bother me.
What's the problem?
#include <iostream>
using namespace std;
int main() {
long long res=0;
for (int answer=1;answer<=28123;answer++){
bool tri=false;
if (answer%6==0 || answer%20==0) continue;
for (int i=12;i<=answer;i++){
if (abs(answer-i)%20==0) tri=true;break;
}
if (!tri) res+=answer;
}
cout << res << endl;
return 0;
}
The logic behind my program is : every abundant number is either divisible by 20 or 6 so I see if I can writ a number with a number divisible by 6 and a number divisible by 20.At the top of code :
if (answer%6==0 || answer%20==0) continue;
I use continue if there was a number divisible by 20 or 6 I use continue.
What's the problem?
Is it because of my solution?

The intent of the line
if (abs(answer-i)%20==0) tri=true;break;
is not correctly expressed in code. It is equivalent to:
if (abs(answer-i)%20==0)
{
tri=true;
}
break;
I suspect that is not what you meant. It needs to be:
if (abs(answer-i)%20==0)
{
tri=true;
break;
}

Related

How many numbers get both ringed and crossed? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Jason rings every multiple of 13 less than 500. He then crosses every multiple of 17 less than 500. How many numbers get both ringed and crossed?
It is an entrance test question with the following answers.
10
0
1
4
As per the answer sheet the answer is 4 but we don't know how to get the mentioned answer.
Please refer the following table results. As per that following table results we could not cross any thing but we can round all the 13 multiples which is 38. However we couldn't even see that answer in the list.
It would be greatly helpful if you could help us get to get that answer?
May be a silly question but did we understand the question correctly?
A simple algorithmic solution could go something like this:
void PrintMultiplesOfXandYuptoZ(int x, int y, int limit)
{
int cx = x;
int cy = y;
while (cx < limit && cy < limit)
{
if (cx == cy)
{
Console.WriteLine(cx);
cx += x;
cy += y;
}
else if (cx > cy)
{
cy += y;
}
else
{
cx += x;
}
}
}

Finding subsequence using bit manipulation programming questions [closed]

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 4 years ago.
Improve this question
How to approach for finding solution to subsequence and subsets problems like finding the subsequence or subsets of an array which satisfy particular condition and should be solved using bits manipulation and what is the best time complexity to which it can be reduced to.
I have started practicing a lot of coding questions these days.
Need little help.
I expect some proper approach which should be followed to solve that's kind of problems.
Count from 0 to (2set.size() - 1) (inclusive). Retrieve the elements corresponding to 1 bits in the current count. The set will have to be ordered to retrieve elements by index, of course.
The only tricky part is pulling out the elements corresponding to the 1 bits in the current count. Here's pseudocode for one way to do that:
for (int pos = 0, mask = 1; mask <= currentCount; mask <<= 1; ++pos) {
if ((currentCount & mask) != 0) {
include element at pos in the current subset
}
}
Note that this assumes that the original set size is no more than the number of bits available for whatever integer type you are using for the count and mask.
Implementation in Java will look like this:
private static void findSubsets(int array[]) {
int numOfSubsets = 1 << array.length;
for (int i = 0; i < numOfSubsets; i++) {
int pos = array.length - 1;
int bitmask = i;
System.out.print("{");
while (bitmask > 0) {
if ((bitmask & 1) == 1)
System.out.print(array[pos] + ",");
bitmask >>= 1;
pos--;
}
System.out.print("}");
}
}

I want to check whether a string is palindrome [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
char s[100];
//char t[100];
int count = 1;
int j=0;
int x,i;
cin >>s;
x=strlen(s);
//cout <<x <<endl;
cout <<s[j] <<endl;
i=x-1;
cout <<s[i] <<endl;
for (int i = x-1; i <= 0; i--)
{
if (s[j] != s[i])
{
count = 0;
}
j++;
}
if ( count )
{
cout <<"YES";
}
else
{
cout <<"NO";
}
return 0;
I Want to whether a given string is palindrome or not. Whats wrong with this code? i am expecting it to print YES if a palindrome is being input and NO if the string is not a palindrome. But it always prints YES. There are no errors.
Your code is never entering that for loop since the condition i = x-1 and i <=0 wont be true well assuming your string is not empty, then there is no need to keep a count variable since as soon as the strings are not matching you can print NO and exit the code.
You can implement it like:
#include <iostream>
using namespace std;
int main() {
char s[100];
int x,i,j=0;
cin >>s;
x=strlen(s);
i = x-1;
cout <<s[0] <<endl;
cout <<s[i] <<endl;
for (int i = x-1; i >= 0; i--)
{
if (s[j] != s[i])
{
cout <<"NO";
return 0;
}
j ++;
}
cout <<"YES";
return 0;
}

How to increase performance of this code snippet by assembly? [closed]

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 6 years ago.
Improve this question
How can I increase performance of the following code snippet in assembly? Please suggest what ways I have in order to do this?
void changeDirection(char key) {
/*
W
A + D
S
*/
switch (key) {
case 'w':
if (direction != 2) direction = 0;
break;
case 'd':
if (direction != 3) direction = 1;
break;
case 's':
if (direction != 4) direction = 2;
break;
case 'a':
if (direction != 5) direction = 3;
break;
}
}/******increase performance*****/
Thanks
Actually in the end the 2-5 range vs 0-3 range turned out to be simple to abuse (although I'm afraid it's not what you wanted).
Plus normal games allow for key redefinition, which would break this completely. So this is more like "joke" than serious answer. But your question is on the brink of "joke" too, I mean: you have more serious problems if you really believe that the thing in your post is a problem.
// I expect "direction" to be int, otherwise change wantDir vars
void changeDirection2(char key) {
// a s d w
constexpr static int wantDir[] = { ~0, 3, ~0, 2, 1, ~0, ~0, 0 };
int wantedDir = wantDir[key&7];
if (wantedDir+2 == direction) return;
direction = wantedDir;
}
Plus this will react to many more (all of them) keys than a,w,s,d. It's up to caller call only with correct ones.
version 2, without LUT (still hardcoded to "awsd" and mangling any other key into some number):
void changeDirection3(char key) {
int wantedDir = (~key>>1)&3;
if (wantedDir+2 == direction) return;
direction = wantedDir;
}

Returning a specific output on basis of specific set of input [closed]

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 6 years ago.
Improve this question
I had been asked to provide the java implementation of the following pseudo code:
function 
{
Input n; //can have either of the two values : 10 or 20
if n == 10
  return 20
  else
  return 10
}
I tried following :
int function(int n){
if (n == 10){
return 20;
}
return 10;
}
Now the next one:
int function (int n){
return n == 10 ? 20 : 10;
}
Here is another one (an extreme one-liner):
int function (int n){
return 30 - n;
}
But, the question asker had some more technique (in a single line), in his mind and I wonder what that might be!
Any idea?
You could use bit-shift operators :
return n==10?n<<1:n>>1;
or multiplication/division :
return n==10?n*2:n/2;

Resources