Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Question 1.

In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers.

For any positive integer n, define d(n) to be n plus the sum of the digits of n.

For example, d(75) = 75 + 7 + 5 = 87.

The number n is called a generator of d(n).

Some numbers have more than one generator: for example, 101 has two generators, 91 and 100.

A number with no generators is a self-number.

There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97.

- Write a program to sum of all self-numbers which are bigger than 0 and smaller than 5000.

Answer :

<?php

// preparing the array variable for the numbers.

$notSelfNumbers = array();

$allNumbers = array();

// let's loop the numbers between 1 to 5000

for($i=1; $i<=5000; $i++){

$b=$i;

$b= (string)$b ;

$allNumbers[] = $i;

$number = strlen($i); // define the length of integer

if ($number == 1){ // 1 number

$sumOfThisNumbers = $i+$b[0];

$notSelfNumbers[] = $sumOfThisNumbers; // sum of this 1 number

} elseif ($number == 2){ // 2 number

$sumOfThisNumbers = $i+$b[0]+$b[1];

$notSelfNumbers[] = $sumOfThisNumbers; // sum of this 2 number

} elseif ($number == 3){ // 3 number

$sumOfThisNumbers = $i+$b[0]+$b[1]+$b[2];
$notSelfNumbers[] = $sumOfThisNumbers; // sum of this 3 number

} elseif ($number == 4){ // 4 number

$sumOfThisNumbers = $i+$b[0]+$b[1]+$b[2]+$b[3];

$notSelfNumbers[] = $sumOfThisNumbers; // sum of this 4 number

// let's compare the numbers and find what we only need to show.

$selfNumbers = array_diff($allNumbers, $notSelfNumbers);

$sumSelfNumbers = 0;

$numbering = 1;

foreach($selfNumbers as $eachSelfNumber) {

echo "<pre>". $numbering++ .". ".$eachSelfNumber;

$sumSelfNumbers+= $eachSelfNumber; // count it up in this loop yeah..

// here is the final result and enjoy my coffee again :)

echo "<pre>Sum of all self-numbers from bigger than 1 and smaller than 5000 is: <strong>" .
number_format($sumSelfNumbers)."</strong>";

?>

So with this code, I will show which are the "self-numbers" and SUM of them (bigger than 0 and
smaller than 5000)

- The sum is: 1,227,365

- There are 494 self-numbers


Question 2.

There is a room with n rows and n columns, and there are gunmen in the room.

A gunman can see vertical or horizontal way, and cannot see over the wall or diagonal.

If a gunman see another gunman, they will shoot each other and only one man will survive.

We want all gunmen to be alive.

■: Wall

□: Empty space

♂: Gunman

For example, there is a room with 4 rows and 4 columns,

■■■□

□□□□

□■□□

■■■□

we can place 3 gunmen like below.

■■■□

□□□♂

♂■♂□

■■■□

However, we cannot place gunmen like below, because they are in the same line horizontally, one
man will die.

■■■□

□♂□♂

□■□□

■■■□
We can place maximum 4 gunmen in this room, and there are 2 ways to achieve this.

■■■♂

□♂□□

♂■♂□

■■■□

■■■□

□♂□□

♂■♂□

■■■♂

- Write a program to calculate

- Answer :

max_num_gunmen = 0

len_board = 0

def place_a_gunman(position, board):

global len_board

# place a gunman

row, col = position

board[row][col] = 1

# fill the shooting range as the wall

for delta in range(1, len_board):

# direction of positive row

if row + delta < len_board:

if board[row + delta][col] == 1:

break
board[row + delta][col] = 1

for delta in range(1, len_board):

# direction of negative row

if row - delta > 0:

if board[row - delta][col] == 1:

break

board[row - delta][col] = 1

for delta in range(1, len_board):

# direction of positive col

if col + delta < len_board:

if board[row][col + delta] == 1:

break

board[row][col + delta] = 1

for delta in range(1, len_board):

# direction of positive col

if col - delta > 0:

if board[row][col - delta] == 1:

break

board[row][col - delta] = 1

def recursion(board, num_gunmen, board_stack):

global max_num_gunmen

global len_board

for i in range(len_board):

for j in range(len_board):

if board[i][j] == 1:

continue
# store gunman position and board before place a gunman

board_stack.append(((i, j), copy.deepcopy(board)))

# place a gunman

place_a_gunman((i, j), board)

num_gunmen += 1

max_num_gunmen = max(max_num_gunmen, num_gunmen)

else:

if not board_stack:

return

if all([all(row) for row in board]):

# backtracking

(i, j), board = board_stack.pop()

board[i][j] = 1

num_gunmen -= 1

recursion(board, num_gunmen, board_stack)

def solution(board):

global len_board

for i in range(len_board):

for j in range(len_board):

if board[i][j] == 1:

continue

recursion(copy.deepcopy(board), 0, []) # copy of board, number of gunmen, empty stack

board[i][j] = 1 # mark as the wall


room = "\

□■□■\n\

■□■□\n\

□■□■\n\

■□■□\

"

# change room to gunmen_board

room = room.replace('□', '0').replace('■', '1').splitlines()

gunmen_board = []

for row in room:

gunmen_board.append([int(cell) for cell in row])

# assert board length

assert len(gunmen_board) == len(gunmen_board[0])

len_board = len(gunmen_board)

solution(gunmen_board)

print(max_num_gunmen)

1. The maximum number of gunmen we can place in the room below.

(FYI, 14 is not the answer)

 Answer : 13

2. (Let's assume the answer of the question 1 is XX)

The maximum number of ways to place XX gunmen.


Let's assume the answer of Question 2.1 is 10 (it is just example). Then how many ways to place 10
gunmen? You have to calculate all possible ways.

 Answer : 3 ways

1. ♂■♂■♂■♂■

□□□□♂■□♂

■♂■□□■♂■

□□□♂□□□□

□□♂■□□□□

□□□□□■♂■

□■♂□□□□□

□□□♂■□■□

2. ♂■□■□■♂■

□□♂□□■□♂

■□■♂□■□■

□♂□□□□□□

□□□■□□□□

□□♂□□■♂■

□■□□□♂□□

□□□□■♂■□

3. □■♂■♂■♂■

♂□□□□■♂□

■♂■♂□■□■

□□□□□□□♂

□□□■♂□□□

□□□□♂■□■

♂■□□□□□□

□□□♂■□■♂

(You should answer both question 1 and 2)


□■□■□■□■

□□□□□■□□

■□■□□■□■

□□□□□□□□

□□□■□□□□

□□□□□■□■

□■□□□□□□

□□□□■□■□

You might also like