I tested the below code with all ASCII values from 64 - 90 inclusive (All uppercase letters) and adjusted accordingly so instead of:
for(int i = 0 ; i < c.length(); i++){
info[i] = ((int)c.charAt(i) - 32);
}
I would replace the 32 with 64 (so the ASCII value of A would save in the array as 0). Furthermore, in my encryption and decryption functions I would replace 95 with 26 (26 letters).
However, if I apply this to all values between 32-126 inclusive (95 characters) and adjust the values accordingly, the values become incorrect and I don't know why. Here is my whole main function below (note that the formula used in encryption and decryption is just an example one I used and I plan on changing the values later on):
public static void main(String[] args) {
String c = "sd344rf"; // could be any set of characters within the range
int[] e = new int[c.length()]; // encrypted set
int[] d = new int[c.length()]; // decrypted set
int[] info = new int[c.length()];
for(int i = 0 ; i < c.length(); i++){
info[i] = ((int)c.charAt(i) - 32);
}
for(int i = 0; i < c.length(); i++){
e[i] = encryption(info[i]);
}
for(int i = 0; i < c.length(); i++){
d[i] = decryption(e[i]);
}
display(info);
System.out.println();
display(e);
System.out.println();
display(d);
}
public static int encryption(int x){
return mod(3*x + 9,95);
}
public static int decryption(int x){
return mod(9*x - 3,95);
}
public static void display(int[] arr){
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
Obviously you are trying to implement an affine cipher. For an affine cipher the encryption is
y = mod(n * x + s, m)
and the decryption
x = mod(ni * (y - s), m)
with
x: Value of the character to encrypt
y: Value of the encrypted character
m: Number of characters in the underlying alphabet
n, s: Key of the encryption
n and s must be chosen so that they are between 0 and m - 1, inclusive. In addition, n has to be chosen so that n and m are coprime. ni is the modular multiplicative inverse of n modulo m and is determined by n*ni mod m = 1.
This is in more detail explained at https://en.wikipedia.org/wiki/Affine_cipher.
If the values u, v associated with the characters don't start at 0 the values have to be shifted by an offset equal to the value of the first character (provided that there are no gaps) and the formulas become
x = u - offset
y = v - offset
v = mod(n * (u - offset) + s, m) + offset
u = mod(ni * ((v - offset) - s), m) + offset
Thus, you've to replace in the main-method
info[i] = ((int)c.charAt(i) - 32);
with
info[i] = (int)c.charAt(i);
The encryption-method becomes:
public static int encryption(int u) {
return mod(n * (u - offset) + s, m) + offset;
}
and the decryption-method
public static int decryption(int v) {
return mod(ni * ((v - offset) - s), m) + offset;
}
with the fields
private static int m = <Number of the characters in the alphabet>;
private static int n = <Key (factor)>; // n between 0 and m-1 and moreover, n and m have te be coprime
private static int s = <Key (summand)>; // s between 0 and m-1
private static int offset = <Value of the first character of the alphabet>;
private static int ni = <Modular multiplicative inverse of n modulo m>;
Moreover, for the mod-operation the following method is used (see Encryption/decryption program not working properly):
private static int mod(int a, int b) {
return ((a % b) + b) % b;
}
Example 1: Uppercase letters A - Z:
private static int m = 'Z' - 'A' + 1; // 26
private static int n = 3; // Choose e.g. n = 3: n = 3 < 26 - 1 = 25 and moreover, 3 and 26 are coprime
private static int s = 9; // Choose e.g. s = 9: s = 9 < 26 - 1 = 25
private static int offset = 'A'; // 65
private static int ni = 9; // 3*9 mod 26 = 1
Test:
String c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Output (with characters instead of their values):
Plain text: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted text: JMPSVYBEHKNQTWZCFILORUXADG
Decrypted text: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Example 2: All characters between 32 (Space) and 126 (~), inclusive:
private static int m = '~' - ' ' + 1; // 95
private static int n = 3; // Choose e.g. n = 3: n = 3 < 95 - 1 = 94 and moreover, 3 and 95 are coprime
private static int s = 9; // Choose e.g. s = 9: s = 9 < 95 - 1 = 94
private static int offset = ' '; // 32
private static int ni = 32; // 3*32 mod 95 = 1
Test:
String c = " !\"#$%&'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
Output (with characters instead of their values):
Plain text: !"#$%&'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
Encrypted text: ),/258;>ADGJMPSVY\_behknqtwz}!$'*-0369<?BEHKNQTWZ]`cfilorux{~"%(+.147:=#CFILORUX[^adgjmpsvy| #&
Decrypted text: !"#$%&'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
Related
In a recent job interview, I had to solve a programmatic question. The question was to convert a given excel alphabet column name to a column number. My solution worked for the most part until they (interviewer) gave me "AUHS" as a column name. The solution broke. The expected answer "31999" but I got "18999". I want to learn where I went wrong and what I could do to improve the solution.
PS: I am new to Java
My solution,
public class test {
public static void main(String[] args) {
String argChar = "AUHS";
char a = 'A';
int asciiA = (int) a;
int col = 0;
col += Math.pow(26, 0) + (int)argChar.charAt(argChar.length()-1) - asciiA;
for(int i=1; i<argChar.length(); i++) {
col += (int)(Math.pow(26, i) + ((int)argChar.charAt(argChar.length()-(i+1)) - asciiA) * 26);
}
System.out.println(col);
}
}
The formula for spreadsheets's alphabetical column labels
The sequence of A-Z encoded (base-26) column-labels is:
A .. 1 (= 26^0*1)
B .. 2 (= 26^0*2)
H .. 2 (= 26^0*8)
S .. 2 (= 26^0*19)
U .. 2 (= 26^0*21)
Z .. 26 (= 26^0*26)
AA .. 27 (= 26^1*1 +1)
AZ .. 52 (= 26^1*1 +26)
BA .. 53 (= 26^1*2 +1)
ZZ .. 702 (= 26^1*26 +26)
AAA .. 703 (= 26^2*1 + 26^1*1 + 26^0*1)
UHS .. 14423 (= 26^2*21 + 26^1*8 + 26^0*19)
AUHS .. 31999 (= 26^3*1 + 26^2*21 + 26^1*8 + 26^0*19)
So we can deduct the formula (in pseudo-mathematical-notation) as:
sum-of ( 26^index * letterAsNumber ) over each letter
where index is the position of the letter from the end starting with 0
where letterAsNumber is the letter mapped to a number {1..26}
where letter must be in range {A..Z}
See also Positional notation.
The parts of this formula in Java
Given following inputs:
column-label String columnLabel
base int radix = 26
ASCII-code offset int asciiOffset = 65 - 1 (A has the decimal ASCII code 65)
We need to calculate the following working variables:
backwards-index int backwardsIndex, which is initially 0 and is incremented by 1 when iterating through each character; starting at the end of the column-label (hence: backwards!)
the running column-number long columnNumber, where a column-letters numerical-character-representation will be added during each iteration
The formula in Java
final int asciiOffset = (int) 'A' - 1; // 64 = 65 - 1, so that (int) 'B' - asciiOffset == 2
long columnNumber = 0;
for (int backwardsIndex=0; backwardsIndex < columnLabel.length(); backwardsIndex++) {
char letter = columnLabel.charAt(columnLabel.length() - backwardsIndex -1);
int letterAsNumber = (int) letter - asciiOffset;
columnNumber += Math.pow(26, backwardsIndex) * letterAsNumber;
}
return columnNumber;
}
See working demo on IDEone.
Issues in your code
What I did to show them:
split some statements of yours into meaningful and testable parts
commented the issues (actual values and expected)
added an assert below some lines (to verify expected values)
added a fixing statement below (with some explanation)
String argChar = "AUHS";
// int asciiA = (int) 'A'; // actual: 65; expected: 64
int asciiA = (int) 'A' - 1; // should be an offset to have: (int) `A` - offset == 1
assert asciiA == 64;
int col = 0;
char letter = argChar.charAt(argChar.length()-1); // actual == expected: 'S'
int letterAsNumber = (int) letter - asciiA; // actual: 83 - 65: expected: 83 - 64;
assert letterAsNumber == 19;
// col += Math.pow(26, 0) + letterAsNumber; // actual: 20 = 1 + 19; expected: 19 = 1 * 19
col = (int) Math.pow(26, 0) * letterAsNumber; // multiply instead add
assert col == 19;
for(int i=1; i<argChar.length(); i++) {
letter = argChar.charAt(argChar.length() - (i + 1));
// letterAsNumber = ((int) letter - asciiA) * 26;
letterAsNumber = ((int) letter - asciiA); // should not add 26 at the end
// col += Math.pow(26, i) + letterAsNumber;
col += Math.pow(26, i) * letterAsNumber; // multiply instead add
}
assert col == 31999;
As counting starts from 1, and columns are counted from A, a correction argChar.charAt(i) - 'A' + 1 is needed to properly convert chars to digits:
String argChar = "AUHS";
int pow = 1;
int n = 0;
for (int i = argChar.length() - 1; i >= 0; i--) {
n += (argChar.charAt(i) - 'A' + 1) * pow;
pow *= 26;
}
System.out.println("n = " + n);
Output:
n = 31999
I have two given Strings: String a = "111" and String b = "132", for these two String I want to achieve this count order:
111
112
121
122
131
132
Other example is if I have two given String like this: String a = "1111" and String b = "1223", I expect this result:
1111
1112
1113
1121
1122
1123
1211
1212
1213
1221
1222
1223
These can be also longer strings like String a = "0100110" and String b = "01101120".
I'm waiting these Strings from user in condition that every character in String a should be lower or equal than the same character position in String b (String a = "11" and String b = "00" <= not allowed)
This is a recursive method till now but it doesn't work very well because it generates number twice or more depending on the input:
public void expand(String l,String h){
for(int i=l.length()-1; i>=0; i--)
{
sb = new StringBuffer(l);
if(charToDigit(l.charAt(i)) < charToDigit(h.charAt(i))) {
sb.replace(i, i+1, inc(sb.charAt(i)));
expand(sb.toString(),h);
System.out.println(sb.toString());
}
}
}
Call the smaller number x and the larger number y. If you calculate y mod 10 (y % 10), you will find the value of the least significant digit, call this n. Similarly, calculate the least significant digit of x, call it m Then, create a temporary variable i which is equal to x initially. Loop until that number is equal to y.
In the body of the loop, first, print i. Then, if the least significant digit of i (again, calculated by i % 10), call it o, is less than n, increment i by one. Otherwise, if o == n, increase i by 10 - n + m. Naturally, if it is ever the case that o > n, something went wrong (i.e. invalid input from the user), since the guarantee was that all digits of x are less than or equal to the corresponding digits in y.
So, in pseudocode:
x = smaller number
y = larger number
n = y % 10
m = x % 10
i = x
while (i <= y):
print i
o = i % 10
if (o < n):
i += 1
else if (o == n):
i += 10 - n + m
Here is my solution
static String l="000";
static String h="232";
static ArrayList<String> combinations = new ArrayList<String>();
static int stringLength= l.length();
for(int i=0; i<rulelength; i++)
{
combinations.add((charToDigit(h.charAt(i)) - charToDigit(l.charAt(i))+1)+"");
}
int number = 1;
for(int i=0; i<combinations.size(); i++)
{
number*=Integer.parseInt(combinations.get(i));
}
int change = Integer.parseInt(combinations.get(combinations.size()-1));
expand(l, h, change, number);
public static void expand(String l, String h, int change, int comb)
{
StringBuffer sb = new StringBuffer(l);
int pos = stringLength-1;
int tmpPos = pos;
for(int i=1; i<=comb; i++)
{
System.out.println(sb.toString());
sb.replace(pos, pos+1, inc(sb.charAt(pos)));
if((i % change)==0) {
for(int j=stringLength-1; j>0; j--)
{
if(charToDigit(sb.charAt(j)) >= (Integer.parseInt(combinations.get(j))-1))
tmpPos = j-1;
else
break;
}
sb.replace(tmpPos, tmpPos+1, inc(sb.charAt(tmpPos)));
for(int j=stringLength-1; j>tmpPos; j--)
{
sb.replace(j, j+1, l.charAt(j)+"");
}
}
}
}
I have a homework assignment where I have to covert any base to base 10. I have some given numbers, which are the "basen". I have to convert those bases to base 10. The only part that I am stuck in is this part of the code:
answer = ; // Not sure what I have to put in here
I have seen some other posts about converting to base ten, but I am just not sure how to how to incorporate them into my code.
public class BaseN {
public static final int BASEN_ERRNO = -1;
public static int digit = 0;
public static void main(String[] argv) {
basen(512, 6);
basen(314, 8);
basen(49, 5);
basen(10101, 2);
}
public static void basen(int n, int b) {
int ans = basen(n, b, 1, 0);
if (ans == BASEN_ERRNO)
System.out.println(n + " is not a valid base-" + b + " number");
else
System.out.println(n + " base-" + b + " = " + ans + " base-10");
}
public static int basen(int number, int base, int placevalue, int answer) {
if (number == 0) return answer;
digit = number % 10;
if (digit >= base) return BASEN_ERRNO;
answer = 1;// not sure what to put here
number = 0;
placevalue = 0;
return basen(number, base, placevalue, answer);
}
}
You could look at a k length number of base n like this:
x(0)*n^(k-1) + x(1)*n^(k-2) + ... + x(k-1)*n^1 + x(k)*n^0
Where x(0), x(1), ..., x(k) is the digit at position k from the left.
So, if you are trying to convert, say, 101 base 2 to base 10 you would do the following :
1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 4 + 0 + 1 = 5 base 10
say you want to convert the number 352 from base 6:
3 * 6^2 + 5 * 6^1 + 2 * 6^0 = 108 + 30 + 2 = 145 base 10
What you're looking for code wise is something like this :
int[] digits = {3, 5, 2};
int base = 6;
int answer = 0;
for(int i = digits.length - 1; i >= 0; i--)
{
answer += digits[i] * Math.pow(base,digits.length-i-1);
}
return answer;
which will return 145.
Hopefully even though my implementation is iterative you should be able to apply it to your recursive implementation as well.
You can implement the following algorithm. Lets say you are given String number which represents the number you want to convert to decimal form and int base which represents the base of given number. You can implement function int convertToNumber(char c); which accepts one character representing one digit from your number and will map characters to numbers like this:
0 -> 0,
1 -> 1,
... ,
A-> 10,
B -> 11,
... ,
F -> 15,
...
Then you just iterate through your given string and multiply this functions output with base to the power of iteration. For example, convert number A32(hexadecimal):
A32 = convertToNumber(A) * b ^ 2 + convertToNumber(3) * b ^ 1 + convertToNumber(2) * b ^ 0 = 10 * 16 ^ 2 + 3 * 16 ^ 1 + 2 * 16 ^ 0 = 10 * 16 * 16 + 3 * 16 + 2 = 2610 (decimal).
public class BaseConvert {
public static int convertDigitToNumber(char c) throws Exception {
if(c >= '0' && c <= '9') return c - '0';
if(c >= 'A' && c <= 'Z') return c - 55;
if(c >= 'a' && c <= 'z') return c - 97;
throw new Exception("Invalid digit!");
}
public static int convertToBase(String number, int base) throws Exception {
int result = 0;
for(int i = 0; i < number.length(); i++){
result += convertDigitToNumber(number.charAt(i)) * (int)Math.pow(base, number.length() - i - 1);
}
return result;
}
public static void main(String[] args) {
try{
System.out.println(convertToBase("732", 8));
System.out.println(convertToBase("A32", 16));
System.out.println(convertToBase("1010", 2));
}catch (Exception e) {
System.out.print(e);
}
}
}
Given an integer N, i am trying to find the nth binary palindrome.I have written the following code but it is not efficient.is there a more efficient way in terms of time complexity.
I was trying it out as a problem online and i was supposed to output in 1 sec or less but for every input it takes 2 seconds.
public static Boolean Palind(String n){
String reverse = "";
int length = n.length();
for(int i = length - 1; i >=0;i--){
reverse = reverse + n.charAt(i);
}
if(n.equals(reverse)){
return true;
}
else{
return false;
}
}
public static int Magical(int n){
ArrayList<Integer> res = new ArrayList<Integer>();
for(int i = 1; i < Math.pow(2, n);i++){
if(Palind(Integer.toBinaryString(i))){
res.add(i);
}
}
return res.get(n-1);
}
The relevant OEIS entry (A006995) has a lot of nice tips if you read through it. For example, a(2^n-1)=2^(2n-2)-1 lets you skip right to the (2n - 1)th palindrome really quickly.
It also gives several implementations. For example, the Smalltalk implementation works like this (note that the input value, n, starts with 1 for the first palindrome, 0):
public static final int nthBooleanPalindrome(int n) {
if (n == 1) return 0;
if (n == 2) return 1;
int m = 31 - Integer.numberOfLeadingZeros(n);
int c = 1 << (m - 1);
int b;
if (n >= 3*c) {
int a = n - 3*c;
int d = 2*c*c;
b = d + 1;
int k2 = 1;
for (int i = 1; i < m; i++) {
k2 <<= 1;
b += a*k2/c%2*(k2 + d/k2);
}
}
else {
int a = n - 2*c;
int d = c*c;
b = d + 1 + (n%2*c);
int k2 = 1;
for (int i = 1; i < m - 1; i++) {
k2 <<= 1;
b += a*k2/c%2*(k2 + d/k2);
}
}
return b;
}
Try something like this maybe?
public static void main(String[] args) {
for (int i = 1; i < 65535; i++) {
System.out.println(
i + ": " + getBinaryPalindrom(i) + " = " + Integer.toBinaryString(getBinaryPalindrom(i)));
}
}
public static int getBinaryPalindrom(int N) {
if (N < 4) {
switch (N) {
case 1:
return 0;
case 2:
return 1;
case 3:
return 3;
}
throw new IndexOutOfBoundsException("You need to supply N >= 1");
}
// second highest to keep the right length (highest is always 1)
final int bitAfterHighest = (N >>> (Integer.SIZE - Integer.numberOfLeadingZeros(N) - 2)) & 1;
// now remove the second highest bit to get the left half of our palindrom
final int leftHalf = (((N >>> (Integer.SIZE - Integer.numberOfLeadingZeros(N) - 1)) & 1) << (Integer.SIZE -
Integer.numberOfLeadingZeros(N) - 2)) | ((N << (Integer.numberOfLeadingZeros(N) + 2)) >>> (Integer.numberOfLeadingZeros(N) + 2));
// right half is just the left reversed
final int rightHalf = Integer.reverse(leftHalf);
if (Integer.numberOfLeadingZeros(leftHalf) < Integer.SIZE / 2) {
throw new IndexOutOfBoundsException("To big to fit N=" + N + " into an int");
}
if (bitAfterHighest == 0) {
// First uneven-length palindromes
return (leftHalf << (Integer.SIZE - Integer.numberOfLeadingZeros(leftHalf)) - 1) | (rightHalf
>>> Integer.numberOfTrailingZeros(rightHalf));
} else {
// Then even-length palindromes
return (leftHalf << (Integer.SIZE - Integer.numberOfLeadingZeros(leftHalf))) | (rightHalf
>>> Integer.numberOfTrailingZeros(rightHalf));
}
}
The idea is that each number will become a palindrome once it reverse is added. To have the halves correctly aligned the halves just need to be shifted in place.
The problem why this has gotten a bit complex is that all uneven-length palindromes of a given leftHalf length come before all even-length palindromes of a given leftHalf length. Feel free to provide a better solution.
As int has 32 bit in Java there is a limit on N.
int-Version on ideone.com
And a BigInteger-version to support big values. It is not as fast as the int-version as the byte[]-arrays which store the value of the BigInteger create some overhead.
public static void main(String[] args) {
for (BigInteger i = BigInteger.valueOf(12345678); i.compareTo(BigInteger.valueOf(12345778)) < 0; i = i
.add(BigInteger
.ONE)) {
final BigInteger curr = getBinaryPalindrom(i);
System.out.println(i + ": " + curr + " = " + curr.toString(2));
}
}
public static BigInteger getBinaryPalindrom(BigInteger n) {
if (n.compareTo(BigInteger.ZERO) <= 0) {
throw new IndexOutOfBoundsException("You need to supply N >= 1");
} else if (n.equals(BigInteger.valueOf(1))) {
return BigInteger.valueOf(0);
} else if (n.equals(BigInteger.valueOf(2))) {
return BigInteger.valueOf(1);
} else if (n.equals(BigInteger.valueOf(3))) {
return BigInteger.valueOf(3);
}
final int bitLength = n.bitLength() - 1;
// second highest to keep the right length (highest is always 1)
final boolean bitAfterHighest = n.testBit(bitLength - 1);
// now remove the second highest bit to get the left half of our palindrom
final BigInteger leftHalf = n.clearBit(bitLength).setBit(bitLength - 1);
// right half is just the left reversed
final BigInteger rightHalf;
{
byte[] inArray = leftHalf.toByteArray();
byte[] outArray = new byte[inArray.length];
final int shiftOffset = Integer.SIZE - Byte.SIZE;
for (int i = 0; i < inArray.length; i++) {
outArray[inArray.length - 1 - i] = (byte) (Integer.reverse(inArray[i]) >>> shiftOffset);
}
rightHalf = new BigInteger(1, outArray).shiftRight(outArray.length * Byte.SIZE - bitLength);
}
if (!bitAfterHighest) {
// First uneven-length palindromes
return leftHalf.shiftLeft(bitLength - 1).or(rightHalf);
} else {
// Then even-length palindromes
return leftHalf.shiftLeft(bitLength).or(rightHalf);
}
}
I have the same idea with #Kiran Kumar: you should not count number one by one to find if it is a binary palindrome which is too slow, but rather find the internal pattern that number has.
List the number in binary string one by one, you can find the pattern:
0
1
11
101
1001
1111
...
1......1
And the following is some math problem:
We have 2^round_up((L-2)/2) palindrome of number with length L in binary format.
Sum up every shorter length number, we get following len to sum mapping:
for (int i = 1; i < mapping.length; i++) {
mapping[i] = (long) (mapping[i - 1] + Math.pow(2, Math.ceil((i - 1) * 1.0 / 2)));
}
If we find N range in [count(L), count(L+1)), we can concat it with remaining number:
public static long magical(long n) {
if (n == 0 || n == 1) {
return n;
}
long N = n - 2;
return Long.parseLong(concat(N), 2);
}
private static String concat(long N) {
int midLen = Arrays.binarySearch(indexRange, N);
if (midLen < 0) {
midLen = -midLen - 1;
}
long remaining = N - indexRange[midLen];
String mid = mirror(remaining, midLen);
return '1' + mid + '1';
}
private static String mirror(long n, int midLen) {
int halfLen = (int) Math.ceil(midLen * 1.0 / 2);
// produce fixed length binary string
final String half = Long.toBinaryString(n | (1 << halfLen)).substring(1);
if (midLen % 2 == 0) {
return half + new StringBuilder(half).reverse().toString();
} else {
return half + new StringBuilder(half).reverse().toString().substring(1);
}
}
Full code with test for produce large possible long can be found in my git repo.
Idea to optimize,
Let's look at the palindrome sequence 0, 1, 11, 101, 111, 1001 etc...
All numbers must begin and end with 1, So the middle bits only changes and midle substring should be palindrome for full string to become palindrome,
So let's take a 2 digit binary number - one palindrome is possible.
The binary of the decimal 3 is a palindrome. 11
For a 3 digit binary number 2 palindromes are possible, 2*(no of 1 digit palindrome)
The binary of the decimal 5 is a palindrome. 101
The binary of the decimal 7 is a palindrome. 111
For 5 digit binary number 4 palindromes are possible 2*(no of 3 digit palindrome)
10001,10101, 11011, 11111
and so on,
So it will be 2 + 20 + 21 + 22 +...... +2i-N ,
we solve for i and find out the palindrome number.
So by analysing this sequence we get an equation like 2(i/2)+1 -1 = N
where N is the No of palindrome,
and i is the number of bits in the nth palindrome string,
using this we can find the length of the String, from this we can find the string early.
This might be complex, but helps in solving higher values of N quickly....
import java.util.Scanner;
public class NeumannsRandomGenerator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter cases:");
int cases = in.nextInt();
int iterations = 0;
for (int i = 0; i <= cases; i++) {
int a = in.nextInt();
int res = ((a * a) / 100) % 10000;
if(res == a){
iterations++;
}
do {
int b = ((res * res) / 100) % 10000;
iterations++;
b = res;
} while (a != res);
System.out.println(iterations);
}
}
}
I am trying to figure Neumans random generator
For Example:
5761 - let it be the first number
5761 * 5761 = 33189121 - raised to power 2
33(1891)21 => 1891 - truncate to get the middle
1891 - it is the second number in the sequence
1891 * 1891 = 3575881 - raised to power 2 (add leading zero to get 8 digits)
03(5758)81 => 5758 - truncate to get the middle
5758 - it is the third number in the sequence (and so on...)
Please help why I am not getting any results:(
I don't know the rules for an iteration where your number is padded like 0315. If the padded value would be 00099225, and the resulting number is 992, then this would be a good start. I also don't see your rule for re-generating, so it just recurses.
I'm doing this quickly, so there could be some unnecessary code here
public int getNeumansRandomNumber(int starting) {
int nsquared = (int) Math.pow(starting, 2);
int length = String.valueOf(nsquared).length();
if (length < 8) {
String zeroPad = "00000000";
String padded = zeroPad.substring(length) + nsquared;
System.out.println("padded="+padded);
nsquared = Integer.valueOf(padded);
}
int middle = (nsquared % 1000000) / 100;
System.out.println(middle);
return getNeumansRandomNumber(middle);
}
References: 1, 2, and 3