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

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;
}
}
}

Related

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("}");
}
}

issues with Euler 23 [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 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;
}

Algorithm to get indexes in order [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 5 years ago.
Improve this question
I want to assign indexes, in order, starting from 0, to objects. When an object is destroyed, the index should be freed for another use, eg.:
allocate_index() returns 0
allocate_index() returns 1
allocate_index() returns 2
allocate_index() returns 3
allocate_index() returns 4
release_index(1)
release_index(3)
allocate_index() returns 1
allocate_index() returns 3
allocate_index() returns 5
This is similar to allocating file descriptor in a unix with open() and releasing with close().
But there is no bound fixed in advance.
Is there an efficient algorithm/data structure for allocating/freeing those indices?
Adding to #Deimos's answer.
Only the released indices need to be stored in a heap data structure as opposed to all available indexes and use an additional variable to keep track of the currentIndex. That way if the next_index is the consecutive next and not from one of the released index it can be accessed in O(1) time as opposed to having the next_index pull from an heap of available indices and inserting back again the next available index. Both of which are O(log N) operations.
A sample code could look like this
#include <bits/stdc++.h>
class MaintainIndex
{
private:
int currentIndex;
set<int> releasedIndex;
public:
MaintainIndex()
{
currentIndex = 1;
}
MaintainIndex(int start)
{
currentIndex = start;
}
int get_index()
{
int nextIndex;
// No released indices available. Return the next consecutive one
if(releasedIndex.size() == 0)
{
nextIndex = currentIndex;
currentIndex += 1;
}
else //Return the smallest released index
{
nextIndex = *releasedIndex.begin();
releasedIndex.erase(releasedIndex.begin());
}
return nextIndex;
}
void release_index(int index)
{
releasedIndex.insert(index);
}
};
I'll go with a binary heap of available indexes. Thanks for everybody's help + downvoting + closing votes: always a pleasure to ask a question on Stakoverflow!

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