Tag Archive for 'log'

Project Euler : n Digit Integers That Are Also nth Power

Problem number 63 of Project Euler gives the expression a = b^c and imposes the condition that the number of digits in a is equal to c. An example would be 16807 = 7^5. ab and c are all positive integers. The problem asks one to find the number of such possibilities.

Solving this problem made me happy. So, I’ll go into the details.

Given the problem, the first thing that comes to mind is the special relationship between the log of a number and the number of digits. The hint is to use logarithms to simplify the problem.

a = b^c

\Rightarrow \log(a) = c \log(b)

But \log(a) = c - f, where f \in (0, 1]

\Rightarrow c - f = c \log(b)

\Rightarrow c \log(\frac{10}{b}) = f

\Rightarrow 0 < \log(\frac{10}{b}) \leq \frac{1}{c}, dividing by c \in Z^+

That’s it! Our problem has been solved. I hope you have already noted that b \in [1, 9]. This means that we can precompute the values of \log(\frac{10}{b}) and store the results in an array. Then for all c \geq 1, just count how many of the precomputed values are smaller than or equal to \frac{1}{c}. The iteration completes when the smallest precomputed value is greater than \frac{1}{c}.

Below is the piece of code I wrote in Java to solve the task.

    public int getCount() {
        m_precomputedValues = new double[9];
        for(int i = 1; i < 10; i++) {
            m_precomputedValues[i-1] = Math.log10(10.0 / i);
        }

        int count = 0;

        int numberOfDigits = 1;
        double val = 1.0 / numberOfDigits;
        while (val > m_precomputedValues[8]) {
            for(int idx = 8; idx >= 0 &amp;amp;&amp;amp; m_precomputedValues[idx] <= val; idx--) count++;
            val = 1.0 / ++numberOfDigits;
        }

        return count;
    }

Popularity: 4% [?]

Downloading All GTalk Chats

There might be various reasons why one would want to download all of their gtalk/gmail chats. Google does not provide any api to do so. So quite naturally, I googled my problem and found a workaround.

Log into your gmail account from Google Chrome. Create a filter that labels all the chats.

Click on the Create Filter link which can be found beside the Search Mail box. For the search criteria, use Chat With text in the Subject text area. For the action set, you may just choose to have all these filtered mails to be labeled as LOGS. Make sure to have the filter applied to all the previous mails as well.

Now that we have filtered out all the chats, we need to have them downloaded. Go to Settings->Offline and toggle on the option. Since we are only interested in chats, choose only the LOGS label for downloading. Wait for all the chats to get downloaded. It may take a while.

Google chrome has a local folder where all the gears files are stored. The location at which our file can be found is:

%AppData%\Local\Google\Chrome\User Data\Default\Plugin Data\Google Gears\mail.google.com\https_443

We are only interested in the main database file. You can identify it by the following name:

.com-GoogleMail#database

Copy this file to a convenient location, say the Desktop. Rename the file to candorz.

Next a couple of scripts need to be downloaded. They can be found at extract.7z and sql4.7z. Extract these two files to the same location as above. Double click on the sql4 script and watch as a new folder called msgs is created and populated with all the chats. The extracted chats are in xml format.

Popularity: 3% [?]

A Logger In C

Now I am working on this fair scheduling for my B Tech Project (BTP). Already having written a 1000 lines of code, it is a great pain to debug it. More so if you do not know which function might have caused the error. I needed a logger which could log all the events. The events include entering a function, any important calculation/decision it makes, any return values, so that if my program breaks down I can exactly pinpoint the source of error simply looking at the log file generated.

I have no idea if any inbuilt logging functionality exists for C programs or not. Either way, I did not have much time to google up and learn how to use it. I decided to make my own logger, a simple one, which would serve my purpose. It hardly took me 10 minutes to get over with it.

One of the important functionalities that I wanted my logger to have is indentation. The log should be properly indented, just the same way as we indent the code. When the program control enters a new function, the logging should get indented to right, and when it leaves the function, the logging should get indented by the same amount to the left. So I defined two macros, $$INDENT$$ and $$OUTDENT$$. To make them work, I defined a global variable – $$logIndent$$. The $$logIndent$$ would at any given time contain the amount of space by which the log has to be indented. Obviously, I initialized it with 0. Also defined is a global variable called $$indentVal$$ which is the amount of spaces with which the indentation should take place. I like the value to be 2. The two macros are

#define INDENT logIndent += indentVal
#define OUTDENT logIndent -= indentVal

int indentVal = 2;
int logIndent = 0;

I defined another macro called $$SPACES$$ which would print the number of spaces to produce the required indentation.

int tempIndent;
FILE *logFile;
#define SPACES for(tempIndent = 0; tempIndent < logIndent; tempIndent++) fprintf(logFile, " ");

Obviously $$tempIndent$$ is a globally declared temporary variable and $$logFile$$ is the file pointer to the file where the log has to be written. I initialize the $$logFile$$ pointer in the $$main()$$ part of my program.

And finally coming to the logger, I created a custom $$printf()$$ statement using the $$vprintf()$$ command. I call my logger as $$LogThis()$$. Its declaration is as follows

void LogThis(const char *format, ...);

It is constructed as follows

void LogThis(const char *format, ...) {
  va_list args;
  va_start(args, format);
  SPACES;
  vfprintf(logFile, format, args);
  va_end(args);
}

That’s it ! To use this logger, I have to do the following in my function :

#include <stdio .h>
#include <stdarg .h>
void TestLogger() {
  int returnVal;
  float retVal;

  LogThis("<testlogger>\n");
  INDENT;

  /**
   *  Inside the function do whatever you want to
   */

  LogThis("Returns: %d, %f\n", retrunVal, retVal)

  OUTDENT;
  LogThis("</testlogger>\n");
}

Done ! A well indented log will be produced. This has really helped me debug the code. I do not have to gdb from the last point that worked in my program. Instead, I know which function the program control was in when it threw an error. I need to gdb from that function onwards only, thus reducing the debuging time considerably. Also, in case of errors, I can increase the debugging data for that particular function, thus eliminating the need to use gdb at all.

Popularity: 3% [?]