sorting - Translating the following C++ code into Nim -


i'm trying learn nim converting different pieces of code, , i've stumbled upon i've never seen before.

#include<bits/stdc++.h> ... for(int t=q&1?u+x:u+x>>1;t>1;)t/=p[++cnt]=sieve[t]; ... sort(p+1,p+cnt+1); 

i understand ternary operator , how works, don't quite what's going on variables "t" , "cnt" (both integers) , array "p" (an array of integers). how using increment index of "p" work?

then there's sort function, in gave because couldn't find documentation on (the fact it's taking integer added array doesn't help).

lets first start of making code little more readable. little bit of whitespace never hurt anybody.

for(int t = (q & 1? u + x: u + x >> 1); t > 1;) {     t /= p[++cnt] = sieve[t]; } 

what's going on variables "t" , "cnt" (both integers) , array "p" (an array of integers)

so t being set either u + x or u + x >> 1 depending on q & 1 is. inside loop dividing t whatever value of sieve @ index of t is. assign value p array @ position of ++cnt. ++cnt using pre increment operator increase value of cnt 1 , using value index of p.

then there's sort function, in gave because couldn't find documentation on does

for assuming using std::sort() function. when dealing arrays name of array treated pointer first element of array. when see sort(p+1,p+cnt+1); can translate sort(one begining of array, cnt + 1 elements begining of array);. going sort of elements in array 1 begining of array 1 less cnt + 1 elements begining of array.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -