Sorting sequence based on greatest common division [closed] - c++

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 6 years ago.
Improve this question
I have a question. I write this comparator:
bool cmp(int a, int b)
{
return __gcd(a, b) > 1;
}
and, for example:
if I have these numbers:
2 5 6 7 8 12 15 19 20
my code outputs:
20 15 12 8 6 2 5 7 19
it's okay..
but for example:
1 2 3 4 5 6 7 8 9
my code outputs
1 2 3 4 5 6 7 8 9
How can I do this?
this sequence should be something like this:
9 6 3 (...)

Your comparator does not establish strict weak ordering, so results are undefined.
To make proper comparator you must make sure that following is true:
cmp(a, a) == false — your comparator does not pass the test on cmp(2, 2)
cmp(a, b) == true → cmp(b, a) == false — your comparator does not pass the test on cmp(2, 4)
cmp(a, b) == true and cmp(b, c) == true → cmp(a, c) == true — your comparator does not pass the test on cmp(2, 6) and cmp(6, 3)

Related

Check for primality number [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 last month.
The community is reviewing whether to reopen this question as of 2 days ago.
Improve this question
I had come across a code solution to check whether any number if prime or not.
int isPrime(int N){
if(N<=1){
return 0;
}
if(N==2 || N==3){
return 1;
}
if(N%2==0 or N%3==0){
return 0;
}
for(int i=5;i*i<=N;i=i+6){
if(N%i==0 || N%(i+2) ==0){
return 0;
}
}
return 1;
}
Can anyone explain, why we are increasing i=i+6 and condition N%(i+2)==0??
We are checking N%i==0 then why we are checking it for i+2?
Starting from 4 every second number is not prime. Starting from 6 every third number is not prime. Now consider what is left
4 5 6 7 8 9 10 11 12 13 14 15 16 17
2+x*2 x x x x x x x
3+y*3 x x x x
others 5 7 11 13 17 19 ...
The pattern continues and has a length of 2*3 = 6.

Any other way to write a loop for multiplication table to take run time values? [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 3 years ago.
Improve this question
To print multiplication tables, I'm using a for loop which has fixed values up to 10th times table only. I need multiplication table as run time values. Is there an alternate loop to take the values at run time? Here is the code which i tried:
scanf(&num);
for(i=num;i<=num;i++)
for (j=1;j<=10;j++)
This is not a Python For loop
In python,you can use the Range function to have a nice multiplication table.
def table_choice(my_choice=None):
for a in range(10):
if my_choice != None:
print('{0} x {1} = {2}'.format(my_choice, a, my_choice * a))
else:
for b in range(10):
print('{0} x {1} = {2}'.format(a, b, a * b))
table_choice(my_choice = 7)
OUTPUT:
7 x 0 = 0
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
In case you execute table_choice() you will get the full table
See the Range documentation in : https://docs.python.org/3/library/functions.html#func-range

sas calculation in table [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 6 years ago.
Improve this question
I have a dataset like this:
data a;
input a b $ c;
cards;
3 + 5
4 + 4
5 - 2
6 * 5
7 / 2
;run;
I want add a new column D that calculates its value from columns A, B, and C:
A B C D
=== === === ===
3 + 5 8
4 + 4 8
5 * 2 10
6 - 5 1
7 / 2 3.5
How about this.
data a;
input a b $ c;
select(b);
when('+') d=a+c;
when('-') d=a-c;
when('*') d=a*c;
when('/') d=a/c;
otherwise;
end;
cards;
3 + 5
4 + 4
5 - 2
6 * 5
7 / 2
;;;;
run;

Return the lines in which the numbers are [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 7 years ago.
Improve this question
I have next file with thousands of lines
1 2 3 4
1 4 5
6
5 6 8 9
5
2 4 7 9
5 8 10
1 3 5 9
.......
Each line (the line number represents the regions) has maximum 4 values (represent labels) and these values are between 0 and 1000.
And I want to know in which lines (regions) are the values (labels) and order them, for that I will need a loop but I do not how.
For instance for the label number 1 is found in line (region) 1,2 and 8 so the output must be 1: 1 2 8
I want this output:
1: 1 2 8
2: 1 6
3: 1 8
4: 1 2 6
5: 2 4 5 7 8
6: 3 4
7: 6
8: 4 7
9: 4 6 8
10: 7
....
The output is the sorted labels from 0 to 1000 and the regions in which they are found.
I was thinking in a loop in Linux or c++ any idea?
You can have a std::map<int, std::list<int>> that maps int to list<int>.
For each value use:
auto iter = map.find(row);
if (iter != m.end() )
{
(iter->second).push_back(val);
} else {
map[row] = std::list<int>(1, val);
}
Then use an iterator and print all the key,value pairs at the end.
Perl to the rescue:
perl -lne 'push #{ $h{$_} }, $. for split
}{
print "$_: #{ $h{$_} }" for sort { $a <=> $b } keys %h
' < input.txt
$. is the line number, you just add it to the hash table for each of the numbers on the line. At the end, you sort the keys of the hash table numerically and print the corresponding arrays.

how do you implement allocating strategy with c++ [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 8 years ago.
Improve this question
I have two workers.If I have configured a strategy that 60% tasks are allocate to A worker and the rest to B worker.
how do you implement with c++.
what is your suggestion?
map<string,float> m_percent;
m_percent.insert(make_pair("countA",0.6));
m_percent.insert(make_pair("countB",0.1));
m_percent.insert(make_pair("countC",0.3));
map<string,int> m_count;
m_count.insert(make_pair("total",0));
map<string,int>::iterator it = m_count.find("countA");
map<string,int>::iterator itc =m_count.find("total");
map<string,float>::iterator itp=m_percent.find("countA");
if(it== m_count.end())//use countA
{
m_count.insert(make_pair("countA",1));
}
else
{
int &c = it->second;
if(itc!=m_count.end()&&itp!=m_percent.end())
{
float f=(c+1)*100/(itc->second+1)*100.0
if (f<=itp->second)
{
it->second=it->second+1;
}
}
}
if(itc!=m_count.end())
{
itc->second=itc->second+1;
}
If you're talking about number of tasks with no regard to complexity, simply keep a count of how many jobs have been allocated to each. Let's call these counts countA for the jobs allocated to A, and count for the total number of jobs (for simplicity in calculations), and initialise them to zero.
Then, when a job comes in, allocate as per the following:
if count is equal to zero, allocate it to A and increment both countA and count.
otherwise, if countA / count is less than 0.6, allocate it to A and increment both countA and count.
otherwise allocate it to B and just increment count.
This will, over the long term, tend to average out allocations so that A gets 60%:
countA count countA/count allocateTo
------ ----- ------------ ----------
0 0 ? A
1 1 1.000 B
1 2 0.500 A
2 3 0.667 B
2 4 0.500 A
3 5 0.600 B
3 6 0.500 A
4 7 0.571 A
5 8 0.625 B
5 9 0.556 A
6 10 0.600 B
6 11 0.545 A
7 12 0.583 A
8 13 0.615 B
8 14 0.571 A
9 15 0.600 B
9 16 0.563 A
10 17 0.588 A
11 18 0.611 B
11 19 0.579 A
12 20 0.600
... and so on.