Wednesday, March 19, 2014

[UVa] 543 - Goldbache's Conjecture

#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
#define PMAX 1000010
bool ver[PMAX];
vector<int> plist;
int sieve(int lim) {
  int i, j, k;
  plist.clear();
  plist.push_back(2);
  for (int i=3 ; i<=lim ; i+=2) {
    if (ver[i]==false) {
      plist.push_back(i);
      for (int j=3 ; i*j<=lim ; j+=2) {
        ver[i*j] = true;
      }
    }
  }
}

int main() {
  sieve(PMAX);
  int n;

  while (scanf("%d",&n)==1) {
    if (n == 0) break; // Program terminated
    int a=-1, b=-1;
    bool found = false;
    for (int i=0 ; i<plist.size() && plist[i]<=n ; i++) {
      a = n-plist[i];
      b = plist[i];
      if (a>1 && b>1 && (a&1) && (b&1) && ver[a] == false && ver[b] == false) {
        found = true;
        break;
      }
    }
    if (found == true) {
      printf("%d = %d + %d\n",n,b,a); 
    } else {
      printf("Goldbach's conjecture is wrong.\n");
    }

  }
  return 0;
}

Wednesday, March 12, 2014

[Lubuntu]Workaround to compile BBCP in Lubuntu

BBCP is a useful program to speed things up when sending files between network machines. It can used multiple streams to make the magic happen. But recently, while trying to compile it (you can't find a prebuilt DEB) in Lubuntu, I faced with the error
*** No rule to make target `makeLinuxi686'.  Stop.
It meant there was to defined rule in the Makefile for my architecture (that I was using in Lubuntu). So, dirty the hands. Open the Makefile in a text editor and look for the line
Linux
Right after it you'll see a line similar to this.
makeLinuxx86_64:
        @make $(MKPARMS)  \
        CC=$(LNXCC) \
        BB=$(LNXcc) \
        CFLAGS="$(ENVCFLAGS) $(LNXOPT)" \
        BFLAGS="$(ENVCFLAGS) $(LNXOPT_B)" \
        INCLUDE="$(ENVINCLUDE)" \
        LIBS="$(LNXLIBS64)"
So, I just crossed my fingers (taking my chances) and cloned that part like this
makeLinuxi686:
        @make $(MKPARMS)  \
        CC=$(LNXCC) \
        BB=$(LNXcc) \
        CFLAGS="$(ENVCFLAGS) $(LNXOPT)" \
        BFLAGS="$(ENVCFLAGS) $(LNXOPT_B)" \
        INCLUDE="$(ENVINCLUDE)" \
        LIBS="$(LNXLIBS64)"
Saved the file
Then again followed the compilation instructions [like here] and VOILA!!! :D

Saturday, March 08, 2014

[Ubuntu] Streaming your PC / Laptop audio stream (alsa) to another LAN connected device

I constantly like to change where in the room I sit for some computing. I get bored too easily in one place so I keep switching places. But, sadly, my sound system isn't wireless. So, how to fix this issue? I wanna move around but still want all my songs keep sounding through my central surround sound? WiFi. Yep.
With the help of the amazing power of Linux, it's too much cute sound architecture ALSA and a little SSH, I stream my whole soundcard feed to another "Old, and slow" laptop that stays with my sound system. I tried Pulseaudio, but it didn't quite get me where I wanted to be. I just use it for selected which feed goes to the other laptop and which stays native.
Stictly, Ubuntu instructions
Requirements
For this work, you need
1. An extra Linux machine (Raspberry Pi/another Laptop/Netbook anything that can run ssh-servr)
2. WiFi network setup (Router, tethered from Laptop etc) 3. A little bug fixing here and there if this tutorial becomes too old

First of all, let's install the following packages
pulseaudio paprefs pavucontrol sox [on the machine that sends the sound data]
openssh-server [on the machine that receives sound data]
I'm assuming both laptops have working sound system setup.

Then, on the streamer (the machine from which we'll send the sound) run this command
modprobe snd-aloop index=0 pcm_substreams=1
This is a module you must load in order to get a loop back device in your soundcards list. What this device does is put all your audio data so that any device can pick that from there. In this case my Laptop connected to the speakers.
If you happen to fail to load this module, check for some instruction on the internet. I found some tutorials that may help you to resolve issues. These are two good tutorials
http://confoundedtech.blogspot.com/2012/08/building-installing-alsa-loopback.html
http://www.sm5bsz.com/linuxdsp/install/snd-aloop.htm
To be able to load this automatically on each boot just add this to
/etc/modules

Now, when your modules is up, you should be able to see a loop back device in your "Sound Settings" that is called "Analog Output, Loopback device"
Now open up ~/.asoundrc and add this to that, if the file doesn't exist create it.
pcm.!default {
  type dmix
  slave.pcm "hw:Loopback,0,0"
}
pcm.loop {
  type plug
  slave.pcm "hw:Loopback,1,0"
}
This basically puts all your sound data in the loopback device. If you want to select manually instead use this.
pcm.loop {
  type plug
  slave.pcm "hw:Loopback,1,0"
}
Save the file. Now, the configuration part is over. You just need to reboot your system. If after rebooting your system does not come up with the loopback device, just load the module again. Now to access the sound on the other device, log into it (or ssh into it from the source machine) and run this command.
ssh -C streamer_username@lan.ip.of.streamer sox -q -t alsa loop -t wav -b 24 -r 48k - | play -q -
This basically will, log in from the laptop that is connected to the sound system to my workspace machine and create a stream file that will be fed back to it via ssh. Then that stream file will be played out on that machine. Neat right?
Afterwards when you are playing something, just run the command
pavucontrol
And from the list of running sound feeds, redirect anyone (or all, your wish) to the "Loopback Audio" device via the menu on the right.

I researched a lot for this setup. Initially my searches kept telling me that I should use "pulseaudio" to expose sound cards to the network, from which the player machine can pick up it's feed and play it back through the system. That setup worked but had a lot of interceptions in the output. The problem might have been of pulseaudio alone. I had almost lost hope but I accidentally stumbled upon these two tutorials that saved me. Thanks a lot to them
http://ywwg.com/wordpress/?cat=13
http://plasmasturm.org/log/soundserverhack/
Both of them are amazing write ups.
Gotchas
Even though this system works flawlessly in quality of output, but it has a huge disadvantage of using up many resources and having a huge sync flaw with videos. But I resolved it by using VLC to adjust it, via the J and K keys.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...