How to write a program to solve project euler problem 10 ?

// Louis Casillas, oxaric@gmail.com 
// Further Edits: Fahad Uddin
// Modified a little by Jeffrin Jose T 
// Euler Problem #10
// Find the sum of all the primes below two million.

#include 
#include 
char isPrime( int num )
{
   if ((num % 3) == 0 )
   {
      return 0;
   }

   int i = 0;
   int r = floor ( sqrt( num ) );

   int f = 5;
   while (f <= r)
   {
      if ((num % f) == 0)
      {
         return 0;
      }
      if ((num % (f + 2)) == 0)
      {
         return 0;
      }
      f += 6;
   }
   return 1;
}
#define UPTO 2000000
int main(void)
{
   int i = 5;
   double sum_of_primes = 5;
   while (i < UPTO)
   {
      if ( isPrime( i ) )
      {     
         sum_of_primes += i;
      }
      i += 2;
   }
   printf( "The sum of the primes is: %f \n", sum_of_primes );
   return 0;
}

How to write a program to solve project euler problem 9 ?

# There exists exactly one Pythagorean triplet for which a + b + c = 1000.                                                    
# Find the product abc.                                                                                                       
# This program is copied from http://code.jasonbhill.com/python/project-euler-problem-9/                                      


import time

def prod_triplet_w_sum(n):
    for i in range(1,n,1):
        for j in range(1,n-i,1):
            k = n-i-j
            if i**2+j**2==k**2:
                return i*j*k
    return 0

start = time.time()
product = prod_triplet_w_sum(1000)
elapsed = (time.time() - start)

print "found %s in %s seconds" % (product,elapsed)

How to write a program to solve related to euler problem 8 ?

# This code related is taken from http://code.jasonbhill.com/python/project-euler-problem-8/
import time

start = time.time()

L = []
L.append("73167176531330624919225119674426574742355349194934")
L.append("96983520312774506326239578318016984801869478851843")
L.append("85861560789112949495459501737958331952853208805511")
L.append("12540698747158523863050715693290963295227443043557")
L.append("66896648950445244523161731856403098711121722383113")
L.append("62229893423380308135336276614282806444486645238749")
L.append("30358907296290491560440772390713810515859307960866")
L.append("70172427121883998797908792274921901699720888093776")
L.append("65727333001053367881220235421809751254540594752243")
L.append("52584907711670556013604839586446706324415722155397")
L.append("53697817977846174064955149290862569321978468622482")
L.append("83972241375657056057490261407972968652414535100474")
L.append("82166370484403199890008895243450658541227588666881")
L.append("16427171479924442928230863465674813919123162824586")
L.append("17866458359124566529476545682848912883142607690042")
L.append("24219022671055626321111109370544217506941658960408")
L.append("07198403850962455444362981230987879927244284909188")
L.append("84580156166097919133875499200524063689912560717606")
L.append("05886116467109405077541002256983155200055935729725")
L.append("71636269561882670428252483600823257530420752963450")
M = []
for s in L:
for t in list(s): M.append(t)
prod = 0
for i in range(len(M)-4):
p = int(M[i])*int(M[i+1])*int(M[i+2])*int(M[i+3])*int(M[i+4])
if p > prod: prod = p
print prod

elapsed = (time.time() - start)
print "time: %s seconds" % elapsed

A program to find 10001st prime number ?

ABOUT PRIME NUMBER

A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself. However, 6 is composite because it is the product of two numbers (2 × 3) that are both smaller than 6. Primes are central in number theory because of the fundamental theorem of arithmetic: every natural number greater than 1 is either a prime itself or can be factorized as a product of primes that is unique up to their order.

TYPICAL SOURCE CODE RELATED
[c]
/*
This code is worked on from http://notmyfaultsblog.blogspot.in/2010/06/project-euler-problem-7-in-c.html
Modified a little for standards by Jeffrin Jose T ahiliation@yahoo.co.in

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?

*/

#include <stdio.h>
#include <math.h>

int isPrime(int test)
{
int i;
int calculateTo = (int) sqrt(test);

for(i = 3; i<=calculateTo; i+=2)
{
if(test%i==0)
return 0;
}

return 1;
}

int main()
{
int howHigh=10001;
int counter=1;
int i;

while (1)
{
for(i=3 ; ; i+=2)
{
if( isPrime(i) )
counter++;

if(counter==howHigh)
{
printf("%d\n", i);
return 0;
}
}

}

return 1;
}
[/c]
TYPICAL OUTPUT SESSION RELATED
[bash]
$gcc -lm tpn.c -o tpn
$./tpn
104743
$

[/bash]
LINKS
https://en.wikipedia.org/wiki/Prime_number
https://en.wikipedia.org/wiki/Factorization

How to write a program to solve project euler problem 6 ?

/*
* Project Name: Problem Six
* Solution Name: Problem Six
* Original creation date: 06/07/2011
* Edit date: 18/01/2013
* Programmer name: Jamie Taylor (aka "GaProgMan")
* File name: ProblemSix.c
*
* Purpose of this project:
* Problem Six, from Project Euler.
* URL: http://projecteuler.net/index.php?section=problems&id=6
* The sum of the squares of the first ten natural numbers is,
* 1^2 + 2^2 + ... + 10^2 = 385
* The square of the sum of the first ten natural numbers is,
* (1 + 2 + ... + 10)^2 = 55^2 = 3025
* Hence the difference between the sum of the squares of the first
* ten natural numbers and the square of the sum is 3025 - 385 = 2640.
* Find the difference between the sum of the squares of the first
* one hundred natural numbers and the square of the sum.
*
* GNU Copyright information
* Copyright 2011 Jamie Taylor <jamie@taylorj.org.uk>
*
* This program is free software; you can redistribute
* it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
*Foundation; either version 2 of the License, or (at
*your option) any later version.
*
*This program is distributed in the hope that it will
*be useful, but WITHOUT ANY WARRANTY; without even the
*implied warranty of MERCHANTABILITY or FITNESS FOR A
*PARTICULAR PURPOSE. See the GNU General Public
*License for more details.
*
*You should have received a copy of the GNU General
*Public License along with this program; if not, write
*to the Free Software Foundation, Inc., 51 Franklin
*Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <iostream>
#include <math.h>

using namespace std;

int main () {
unsigned long long sumOfSquares = 0;
unsigned long long squareOfSums = 0;
long long difference = 0;
int i = 1;
do {
sumOfSquares += pow((double)i, 2);
squareOfSums += i;
i++;
} while ( i < 101 );

squareOfSums = pow ((double)squareOfSums, 2);
difference = squareOfSums - sumOfSquares;

cout << "Sum of all squares: " << sumOfSquares << endl;
cout << "Square of all sums: " << squareOfSums << endl;

cout << "The difference between the sum of all squares and the square "
<< "of all sums is:\n" << difference << endl;
char ch;
/* cin >> ch; */
return 0;
}

/* Small changes to this file by <ahiliation@yahoo.co.in> Jeffrin Jose T. */
/* The pow() function returns the value of x raised to the power of y. */

How to write a program to solve project euler problem 5 ?

/* This file is worked ( mostly copy and paste ) from from http://www.programminglogic.com/solution-to-project-euler-5/
Notice that if the number is evenly divisible by all numbers from 11 to 20 it's also divisible by all numbers
from 2 to 10.
*/

#include <stdio.h>

int main(){
int i,j,counter;
for (i=10;i<1000000000;i++){
counter=0;
for (j=11;j<21;j++){
if (i%j==0)
counter++;
}
if (counter==10){
printf("%d \n",i);
break;
}
}
return 0;
}

gdb backtrace for a chromium hangup

(gdb) bt
#0  pthread_cond_timedwait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:238
#1  0x00007f9d69721343 in base::ConditionVariable::TimedWait (this=0x7fffdb12b308, max_time=...)
    at ../../base/synchronization/condition_variable_posix.cc:102
#2  0x00007f9d696fde2c in base::WaitableEvent::TimedWait (this=0x7f9d705c0540, max_time=...)
    at ../../base/synchronization/waitable_event_posix.cc:214
#3  0x00007f9d696dcc16 in base::MessagePumpDefault::Run (this=0x7f9d705c0530, delegate=0x7fffdb12b570)
    at ../../base/message_loop/message_pump_default.cc:56
#4  0x00007f9d696edc51 in base::RunLoop::Run (this=0x7fffdb12b3e0) at ../../base/run_loop.cc:49
#5  0x00007f9d696da157 in base::MessageLoop::Run (this=) at ../../base/message_loop/message_loop.cc:308
#6  0x00007f9d6c310ffc in content::RendererMain (parameters=...) at ../../content/renderer/renderer_main.cc:227
#7  0x00007f9d696a31de in content::RunZygote (main_function_params=..., delegate=)
    at ../../content/app/content_main_runner.cc:339
#8  0x00007f9d696a4071 in content::ContentMainRunnerImpl::Run (this=0x7f9d7059a270)
    at ../../content/app/content_main_runner.cc:764
#9  0x00007f9d696a2db0 in content::ContentMain (params=...) at ../../content/app/content_main.cc:19
#10 0x00007f9d69101ec8 in ChromeMain (argc=4, argv=0x7fffdb12bbd8) at ../../chrome/app/chrome_main.cc:57
#11 0x00007f9d60e91b45 in __libc_start_main (main=0x7f9d69101e70 , argc=4, argv=0x7fffdb12bbd8, 
    init=, fini=, rtld_fini=, stack_end=0x7fffdb12bbc8) at libc-start.c:287
#12 0x00007f9d69101d6f in _start ()
(gdb) 

How to manipulate packages with dpkg (Enabling Muilti-Arch in Multi-Arch support) ?

$dpkg --print-architecture
amd64
$dpkg --print-foreign-architectures
$dpkg --add-architecture i386
dpkg: error: unable to create new file '/var/lib/dpkg/arch-new': Permission denied
$sudo dpkg --add-architecture i386
[sudo] password for jeffrin: 
$dpkg --print-foreign-architectures
i386
$sudo dpkg --add-architecture itest386
$dpkg --print-foreign-architectures
i386
itest386
$dpkg --remove-architecture itest386
dpkg: error: unable to create new file '/var/lib/dpkg/arch-new': Permission denied
$sudo dpkg --remove-architecture itest386
$dpkg --print-foreign-architectures
i386
$sudo dpkg --remove-architecture i386
$dpkg --print-foreign-architectures
$

How to write a program to solve project Euler problem 4

'''
This file is worked on from http://www.s-anand.net/euler.html , Solution of Problem 4 

A palindromic number reads the same both ways. The largest palindrome made from the product 
of two 2-digit numbers is 9009 = 91 x 99.

Find the largest palindrome made from the product of two 3-digit numbers.

'''

n = 0

for a in xrange(999,100,-1):
    for b in xrange(a,100,-1):
        x = a * b
        if ( x > n ):
            s = str( a * b )
            if s == s[::-1]:
                n = a * b
print n

Hacking with runit and sv ( sv – control and manage services monitored by runsv(8) ) commands

ABOUT runit

runit is a cross-platform Unix init scheme with service supervision, a replacement for sysvinit, and other init schemes. It runs on GNU/Linux, *BSD, MacOSX, Solaris, and can easily be adapted to other Unix operating systems

ABOUT sv

The sv program reports the current status and controls the state of services monitored by the runsv(8) supervisor.
services consists of one or more arguments, each argument naming a directory service used by runsv(8). If service doesn’t start with a dot or slash and doesn’t end with a slash, it is searched in the default services directory /service/, otherwise relative to the current directory.

A TYPICAL SHELL EXPOSURE
[bash]
$pwd
/etc/service
$ls
hello.sh
$mkdir new
mkdir: cannot create directory ‘new’: Permission denied
$sudo mkdir new
$mv hello.sh new/run
mv: cannot move ‘hello.sh’ to ‘new/run’: Permission denied
$sudo mv hello.sh new/run
$ls
new
$cd new/
$ls
run supervise
$ls -l
total 8
-rw-r–r– 1 root root 11 Oct 24 18:55 run
drwx—— 2 root root 4096 Oct 24 18:57 supervise
$cd supervise/
bash: cd: supervise/: Permission denied
$sudo cd supervise/
sudo: cd: command not found
$sudo sv start /etc/service/new
timeout: down: /etc/service/new: 1s, normally up, want up
$sudo sv status /etc/service/new
down: /etc/service/new: 0s, normally up, want up
$sudo sv restart /etc/service/new
timeout: down: /etc/service/new: 0s, normally up, want up
$sudo emacs /etc/service/new/run
$sudo sv restart /etc/service/new
timeout: down: /etc/service/new: 0s, normally up, want up
$sudo sv stop /etc/service/new
ok: down: /etc/service/new: 0s, normally up, want up
$sudo chmod +x /etc/service/new/run
$sudo sv start /etc/service/new
timeout: down: /etc/service/new: 0s, normally up, want up
$sudo sv start /etc/service/new/
timeout: down: /etc/service/new/: 1s, normally up, want up
$su
Password:
root>sv start /etc/service/new/
run run~ supervise/
root>sv start /etc/service/new/run
fail: /etc/service/new/run: unable to change to service directory: not a directory
root>sv start /etc/service/new/
timeout: down: /etc/service/new/: 1s, normally up, want up
root>exit
$echo $?
1
$sudo sv stop /etc/service/new/
ok: down: /etc/service/new/: 1s, normally up, want up
$echo $?
0
$sudo runit
– runit: fatal: must be run as process no 1.
$sudo runit enable
– runit: fatal: must be run as process no 1.
$

[/bash]
TYPICAL SOURCE CODE EXPOSURE
[c]

int main(int argc, char **argv) {
unsigned int i, done;
char *x;

progname =*argv;
for (i =str_len(*argv); i; –i) if ((*argv)[i -1] == ‘/’) break;
*argv +=i;
optprogname =progname =*argv;
service =argv;
services =1;
lsb =(str_diff(progname, "sv"));
if ((x =env_get("SVDIR"))) varservice =x;
if ((x =env_get("SVWAIT"))) scan_ulong(x, &wait);
while ((i =getopt(argc, (const char* const*)argv, "w:vV")) != opteof) {
switch(i) {
case ‘w’: scan_ulong(optarg, &wait);
case ‘v’: verbose =1; break;
case ‘V’: strerr_warn1(VERSION, 0);
case ‘?’: usage();
}
}
argv +=optind; argc -=optind;
if (!(action =*argv++)) usage(); –argc;
if (!lsb) { service =argv; services =argc; }
if (!*service) usage();

taia_now(&tnow); tstart =tnow;
if ((curdir =open_read(".")) == -1)
fatal("unable to open current directory");

act =&control; acts ="s";
if (verbose) cbk =&check;
switch (*action) {
case ‘x’: case ‘e’:
acts ="x"; break;
case ‘X’: case ‘E’:
acts ="x"; kll =1; cbk =&check; break;
case ‘D’:
acts ="d"; kll =1; cbk =&check; break;
case ‘T’:
acts ="tc"; kll =1; cbk =&check; break;
case ‘t’:
if (!str_diff(action, "try-restart")) { acts ="tc"; cbk =&check; break; }
case ‘c’:
if (!str_diff(action, "check")) { act =0; acts ="C"; cbk =&check; break; }
case ‘u’: case ‘d’: case ‘o’: case ‘p’: case ‘h’:
case ‘a’: case ‘i’: case ‘k’: case ‘q’: case ‘1’: case ‘2’:
action[1] =0; acts =action; break;
case ‘s’:
if (!str_diff(action, "shutdown")) { acts ="x"; cbk =&check; break; }
if (!str_diff(action, "start")) { acts ="u"; cbk =&check; break; }
if (!str_diff(action, "stop")) { acts ="d"; cbk =&check; break; }
if (lsb && str_diff(action, "status")) usage();
act =&status; cbk =0; break;
case ‘r’:
if (!str_diff(action, "restart")) { acts ="tcu"; cbk =&check; break; }
if (!str_diff(action, "reload")) { acts ="h"; cbk =&check; break; }
usage();
case ‘f’:
if (!str_diff(action, "force-reload"))
{ acts ="tc"; kll =1; cbk =&check; break; }
if (!str_diff(action, "force-restart"))
{ acts ="tcu"; kll =1; cbk =&check; break; }
if (!str_diff(action, "force-shutdown"))
{ acts ="x"; kll =1; cbk =&check; break; }
if (!str_diff(action, "force-stop"))
{ acts ="d"; kll =1; cbk =&check; break; }
default:
usage();
}

servicex =service;
for (i =0; i < services; ++i) {
if ((**service != ‘/’) && (**service != ‘.’) && **service &&
((*service)[str_len(*service) -1] != ‘/’)) {
if ((chdir(varservice) == -1) || (chdir(*service) == -1)) {
fail("unable to change to service directory");
*service =0;
}
}
else
if (chdir(*service) == -1) {
fail("unable to change to service directory");
*service =0;
}
if (*service) if (act && (act(acts) == -1)) *service =0;
if (fchdir(curdir) == -1) fatal("unable to change to original directory");
service++;
}

[/c]

SOURCE CODE TAKEN FROM DEBIAN SOURCE PACKAGE NAMED runit

RELATED LINKS
http://smarden.org/runit/
http://smarden.org/runit/sv.8.html
https://en.wikipedia.org/wiki/Runit
https://docs.ansible.com/ansible/2.5/modules/runit_module.html