Let the reverse of a positive integer n , denoted R ( n ) , be the result when the digits of the number are written backwards; for example, R ( 1 9 0 ) = 0 9 1 , or just 9 1 .
Call a positive integer n brilliant if n + R ( n )
is a multiple of 13. Let B be the 1 0 0 0 0 th brilliant number. Compute the last three digits of B .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Python:
1 2 3 4 5 6 |
|
The three digit number 10000 will be print 1 3 0 2 4 1
Log in to reply
These are java inbuilt classes "io" refers to inputoutput if you dont use it u cant use the print statement
Moreover I only know C and Java, I don't know Python so cant understand anything above except part of the logic
Log in to reply
No, you definitely don't need that import; you've already sufficiently qualified the method when you type System.out.println(), because System comes from java.lang.System which is automatically imported into every program.
Well, Santanu I too did it using Java but I didn't import the the I/O package since I don't think there will be any need of it. Also java.math package isn't required.
Here 's my C++ solution.
Log in to reply
String handling functions for reversing along with Standard library functions like atoi and itoa considerably reduce that code to 7-8 lines.
I started learning Haskell a few weeks ago and it turns out there's a really simple one-liner Haskell solution to this problem. Since no one posted a Haskell solution here, I'm posting it as a comment here:
1 |
|
The amazing fact, I found, in such brilliant numbers i is that most of the i + R ( i ) are palindromes :)
Python solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Can you provide any insight into the classification of brilliant numbers?
Log in to reply
2-digits: If n = a b = 1 0 a + b is brilliant, then n + r ( n ) = 1 1 a + 1 1 b = 1 1 ( a + b ) must be divisible by 13, so the sum of the digits must be equal to 13.
3-digits: If n = a b c = 1 0 0 a + 1 0 b + c is brilliant, then n + r ( n ) = 1 0 1 a + 2 0 b + 1 0 1 c is divisible by 13. Taking mod 13, we have 1 0 a + 7 b + 1 0 c is divisible by 13.
Anybody want to generalise? (Or does there even exist such a generalisation)
Log in to reply
Let n = a d a d − 1 a d − 2 … a 2 a 1 a 0 .
Then n + R ( n ) = k = 0 ∑ d ( 1 0 k + 1 0 d − k ) a k ) = : k = 0 ∑ d f d , k a k . To determine whether a number is "brilliant" we only need to know the f d , k modulo 13. Here is a table (where each f d , k is reduced to a remainder between − 6 and 6 ):
d , k 0 1 2 3 4 5 6 7 8 9 0 2 − 2 − 3 0 4 5 2 − 2 − 3 0 1 − 2 − 6 6 − 4 0 1 − 2 − 6 6 2 − 3 6 5 − 5 − 1 0 − 3 6 3 0 − 4 − 5 − 2 2 3 0 4 1 0 − 1 2 6 − 6 5 5 1 0 3 − 6 6 2 − 2 − 3 0 7 − 2 − 6 6 8 − 3 6 9 0
In fact, each of the rows in the table may be multiplied or divided by a factor ≡ 0 mod 13. For instance, we can simplify to
d , k 0 1 2 3 4 5 6 7 8 9 0 1 1 1 0 − 1 1 2 1 1 0 1 1 2 1 1 0 1 1 2 1 2 1 1 2 − 1 − 1 0 1 1 3 0 1 − 1 − 2 − 1 − 1 0 4 − 1 0 − 1 − 1 − 2 − 1 5 1 1 0 − 1 − 1 6 2 1 1 0 7 1 2 1 8 1 1 9 0
For example, a ten-digit number is brilliant if 1 3 ∣ ( a 1 + a 2 − a 4 − a 5 + a 7 + a 8 ) .
There is obviously some symmetry in the table. Each row is naturally its own reverse. Also, each row repeats itself with period six. This has to do with the fact that 1 0 6 ≡ 1 mod 13. It follows that f d , k + 6 = 1 0 k + 6 + 1 0 d − k − 6 ≡ 1 0 k + 1 0 d − k = f d , k . Also, there is also a repetition of period six in the columns: f d + 6 , k = 1 0 k + 1 0 d + 6 − k ≡ 1 0 k + 1 0 d − k = f d , k
d ′ , k ′ 0 1 2 3 4 5 0 2 1 1 0 − 1 1 1 1 1 2 1 1 0 2 − 1 0 1 1 2 − 1 3 − 2 − 1 − 1 0 1 − 1 4 − 1 − 1 − 2 − 1 − 1 0 5 1 0 − 1 − 1 − 2 1
For a number with d + 1 digits, the coefficient of digit a k is the value in the table at ( d ′ , k ′ ) = ( d mod 6 , k mod 6 ) .
Final example: if 130241 brilliant?
Yes, because (according to row 5 of the table, read right-to-left) 1 ⋅ 1 + 0 ⋅ 4 + ( − 1 ) ⋅ 2 + ( − 1 ) ⋅ 0 + 0 ⋅ 3 + 1 ⋅ 1 = 1 − 2 + 1 = 0 is a multiple of 13.
I didn't make the problem with any sort of mathematical meaning in mind, but perhaps Calvin knows something we don't...?
Good job! I love python because the r(n) function is so simple - in Java (AFAIK) you need at least 4 lines!
Here is my creative solution :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Explanation: If the number has d + 1 digits then n + R ( n ) = ( 1 0 d + 1 ) a 0 + ( 1 0 d − 1 + 1 0 ) a 1 + ( 1 0 d − 2 + 1 0 0 ) a 2 + ⋯ + ( 1 + 1 0 d ) a d . We write this as n + R ( n ) ≡ f d , 0 a 0 + f d , 1 a 1 + f d , 2 a 2 + ⋯ + f d , d a d mod 1 3 . The table of f s is pre-programmed. Numbers are generated by increasing digits and updating the variable r e m ≡ n + R ( n ) mod 13.
Output: 2 4 1 .
As for run-time, the computer reported 0 milliseconds. When I extended the program to include the first 100,000 brilliant numbers, it was 15 ms.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
output
130241!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Output:
1 |
|
1 2 3 4 5 6 7 8 9 10 |
|
This is also a rather easy problem, but it needs a clarification: Sometimes zero is considered a positive integer; however, this assumption must not be made here, as the result will then be deemed erroneous. In this task, 1 is the first positive integer. With this assumption, the result will be accepted. Here's my code in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Just curious - is there any standard reference or text where 0 is considered positive? I've heard of differing conventions on the "natural numbers" (with or without 0) but never that 0 could be a "positive integer". Thanks!
The problem with itoa(); is that it's not standard. Many compilers like G++ doesn't support it. It's best to avoid using it. You can formulate a simpler solution by making your own reverse function by using basic math.h functions. If you wish, you can see my solution in C++ (although, this can be made shorter).
Here's a ruby solution:
def r(n)
return n.to_s.reverse.to_i
end
def main()
i=0
n=0
while i<10000 do
n=n+1
if((n+r(n)) % 13 == 0)
i=i+1
end
end
puts n
end
main()
Solution in Java. Runtime of 100-110 ms.
public class ReversedMultiples
{
public static void main(String[] args)
{
long startTime = System.currentTimeMillis();
int brillNum = 0;
long last = 0;
for (long i = 13; brillNum < 10000; i++)
{
if (isBrilliant("" + i))
{
brillNum++;
}
if (brillNum == 10000)
last = i;
}
System.out.println("10000th brill num: " + last);
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
System.out.println("Runtime: " + elapsedTime + " ms");
}
public static boolean isBrilliant(String n)
{
char[] array = new char[n.length()];
for (int i = 0; i < n.length(); i++)
{
array[i] = n.charAt(n.length() - i - 1);
}
String output = new String(array);
return ((Integer.parseInt(n) + Integer.parseInt(output)) % 13 == 0);
}
}
Output:
10000th brill num: 130241
Runtime: 98 ms
Python Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Output: 1 3 0 2 4 1
Here is a code snippet in C# that does the work...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
C implementation without strings.
#include <stdio.h>
#include <inttypes.h>
uint64_t r(uint64_t x) {
uint64_t ans = 0;
while (x > 0) {
ans = ans*10 + (x%10);
x /= 10;
}
return ans;
}
int main(void) {
uint64_t n = 0;
for (int i = 10000; i > 0;) {
n++;
if ((n + r(n)) % 13 == 0) i--;
}
printf("the 10000th brilliant number: %" PRIu64 "\n", n);
return 0;
}
The following Python code solves the problem:
1 2 3 4 5 6 |
|
Hence, the answer is 2 4 1
My perl solution:
my $num=1;
my @brilliants = ();
while ( scalar @brilliants < 10000 ) {
my $revnum = reverse $num;
my $potbrill = $num + $revnum;
if ( $potbrill % 13 == 0 ) {
push @brilliants, $num;
}
$num ++;
}
print "10000th: $brilliants[9999]\n";
1 2 3 4 5 6 7 8 9 |
|
In Haskell:
(`mod` 1000) $ (!!9999) $ filter (\n -> (n + rev n) `mod` 13 == 0) [1..]
where rev = read . reverse . show :: Int -> Int
If you are using VB.NET, you could try this:
Sub Main()
Dim bnos(10001) As Integer
Dim ctr As Integer = 0
Dim no As Integer = 0
Dim rno As Integer
While ctr <> 10001
no = no + 1
rno = Integer.Parse(StrReverse(no.ToString()))
If (no + rno) Mod 13 = 0 Then
bnos(ctr) = no
ctr = ctr + 1
End If
End While
Console.WriteLine(bnos(9999))
Console.ReadLine()
End Sub
Below is a Python Code for the question.
1 2 3 4 5 6 7 8 9 10 |
|
This will give you 1 3 0 2 4 1 in the last line of output. Please be patient; use this Python Interpreter . Hope it helped!
Python is like this One :
def rev(x) : x = str(x) x = x[ : :-1] x = int(x) return x n = 0 for i in range(1,1000000): if (i + rev(i))%13 == 0 : n += 1 if n == 10000 : print i
see the first observation.
Solution in Mathematica:
In[1]:= R[n_] := FromDigits@Reverse@IntegerDigits@n;
Select[Range[1000000], ((R[#] + #)~Mod~13 == 0) &][[10000]]
Out[2]= 130241
The APL way:
APL
i←⍳1000000 ⋄ n←0=13∣i+10⊥¨⌽¨((⌈¨10⍟i+1)⎕repl ¨10)⊤¨i ⋄ 10000⊃n/i
130241
Better APL way:
APL
∇ ret←Reverse rarg
[1] ret←10⊥⌽((⌈10⍟rarg+1)/10)⊤rarg
∇
10000⊃Where 0= 13∣ (⍳200000)+Reverse¨ ⍳200000
130241
:-)
Problem Loading...
Note Loading...
Set Loading...
I did using the following java code: