Number Pyramid like Egyptian with decreasing numbers - java

Im working on a pyramid on Java. I did it with stars. But i want to do it with decreasing numbers. I'm using an input. Assume input is 5;
5
545
54345
5432345
543212345
My code is;
int size = 11;
for (int i = 1; i <= size; i=i+2) {
int spaceCount = (size - i)/2;
for(int j = 0; j< size; j++) {
if(j < spaceCount || j >= (size - spaceCount)) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
I'm very glad to for your attention. Thanks a lot.
int size = 11;
for (int i = 1; i <= size; i=i+2) {
int spaceCount = (size - i)/2;
for(int j = 0; j< size; j++) {
if(j < spaceCount || j >= (size - spaceCount)) {
System.out.print(" ");
} else {
System.out.print(n);
}
}
System.out.println();
}

Something like this ?
But this only works for numbers of 1 - 9.
int h = 2;
String spacing = h == 1 ? "" : String.format("%" + (h - 1) + "s", "");
StringBuilder s = new StringBuilder(String.valueOf(h));
System.out.printf("%s%s\n", spacing, s);
for(int i = h; i > 1; --i){
System.out.print(spacing.substring(0, i - 2));
s.insert(s.length() / 2 + 1, String.valueOf(i - 1) + String.valueOf(i));
System.out.println(s.toString());
}

Related

SPOJ Prime Generator, getting TLE, but approached with the best I could. (JAVA)

The problem is to generate prime in between two interval, detail problem is given in this link. SPOJ Prime Generator.
Let me explain the magic numbers and the algorithm I followed.
I have used modified Sieve Eratosthenes algorithm (modified in sense because I used the basic idea.) for implementation.
Starting number of interval, m and End number of the interval n are <= 10^9 and the difference is <=10^5 (1 <= m <= n <= 1000000000, n-m<=100000)
There is no even prime number except 2, so I considered max m and n (10^9)/2
and sqrt(max number) is around 32000 (considering both odd and even), finally 32000/2= 16,000 is the size of odd numbers list input_aray.
Finally total number range is divided into 3 regiions.
m and n both >= 32000 in this case the size of the input_aray is (n-m+1)/2 from 16001 index of array, numbers between m and n is stored (only odd numbers).
m and n <32000 in this case size of input_aray is upto n/2.
m <32000 and n>32000 in this case size of input_aray is (n-32000+1)/2.
Boolean array bol of same size as input_aray is kept to track which number is visited so that two number can't be considered twice.
for (int j = 1; j < 16001; j++) {
int flag = input_aray[j];
This loop choose n index from input_aray and check if there is any number in this array that is divisible, if so then same index of bol is initialized into false.
for (int k = j + flag; k <= 16000; k = k + flag)
This loop check for prime numbers upto 32000.
for (int k = 16001; k < input_aray.length; k++)
This one checks in between ** m and n** (when m&n >=32000)
*This is the fastest approach I could implement, but still get Time Limit Exceed. What could be the probable cause?
public static void main(String args[]){
Scanner take= new Scanner(System.in);
ArrayList<String> arrayList= new ArrayList<>();
int m,n;
int temp= take.nextInt();
take.nextLine();
if(temp>=0 && temp<=10){
for(int i=0;i<temp;i++) {
String temp1 = take.nextLine();
arrayList.add(temp1);
}
}
for(int i=0;i<arrayList.size();i++){
String[] temp_aray= arrayList.get(i).split(" ");
m= Integer.parseInt(temp_aray[0]);
n= Integer.parseInt(temp_aray[1]);
if(m>0 && n>0 && m<=10E8 && n<=10E8 && n-m<= 10E4 ) {
if (m >= 32000 && n >= 32000) {
//m & n > 32000
int start;
int[] input_aray = new int[16001 + ((n - m + 1) / 2) + 1];
boolean[] bol = new boolean[16001 + ((n - m + 1) / 2) + 1];
Arrays.fill(bol, true);
input_aray[0] = 2;
input_aray[1] = 3;
for (int j = 2; j < 16001; j++) {
input_aray[j] = input_aray[j - 1] + 2;
}
if (m % 2 == 0) {
start = m + 1;
} else {
start = m;
}
for (int j = 16001; j < input_aray.length; j++) {
input_aray[j] = start;
start += 2;
}
for (int j = 1; j < 16001; j++) {
int flag = input_aray[j];
for (int k = j + flag; k <= 16000; k = k + flag) {
if (input_aray[k] % flag == 0 && bol[k] == true) {
bol[k] = false;
}
}
for (int k = 16001; k < input_aray.length; k++) {
if (input_aray[k] % flag == 0) {
bol[k] = false;
}
}
}
int num = 1;
for (int j = 16001; j < bol.length; j++) {
if (bol[j] == true) {
System.out.println(input_aray[j]);
num++;
}
}
System.out.println();
}
if(m<32000 && n< 32000){
int[] input_aray = new int[(n/2)+1];
boolean[] bol = new boolean[(n/2)+1];
Arrays.fill(bol, true);
input_aray[0] = 2;
input_aray[1] = 3;
for (int j = 2; j < input_aray.length; j++) {
input_aray[j] = input_aray[j - 1] + 2;
}
for (int j = 1; j < Math.sqrt(n); j++) {
int flag = input_aray[j];
for (int k = j + flag; k<input_aray.length; k = k + flag) {
if (input_aray[k] % flag == 0 && bol[k] == true) {
bol[k] = false;
}
}
}
int num = 1;
for (int j = 0; j < bol.length; j++) {
if (bol[j] == true && input_aray[j] >=m && input_aray[j]<=n) {
System.out.println(input_aray[j]);
num++;
}
}
System.out.println();
}
if(m<32000 && n>32000){
int start;
int[] input_aray = new int[16001 + ((n - 32000 + 1) / 2) + 1];
boolean[] bol = new boolean[16001 + ((n - 32000 + 1) / 2) + 1];
Arrays.fill(bol, true);
input_aray[0] = 2;
input_aray[1] = 3;
for (int j = 2; j < 16001; j++) {
input_aray[j] = input_aray[j - 1] + 2;
}
start=32001;
for (int j = 16001; j < input_aray.length; j++) {
input_aray[j] = start;
start += 2;
}
for (int j = 1; j < 16001; j++) {
int flag = input_aray[j];
for (int k = j + flag; k <= 16000; k = k + flag) {
if (input_aray[k] % flag == 0 && bol[k] == true) {
bol[k] = false;
}
}
for (int k = 16001; k < input_aray.length; k++) {
if (input_aray[k] % flag == 0) {
bol[k] = false;
}
}
}
int num = 1;
for (int j = 0; j < bol.length; j++) {
if (bol[j] == true && input_aray[j]>=m && input_aray[j]<=n) {
System.out.println(input_aray[j]);
num++;
}
}
System.out.println();
}
}
}
}

String,which repeats ,but not starts from the beginning in Java (diamond pattern

This is the work that i done so far:I have to print diamond pattern which always starts with uppercase from string, which repeats,but not always starts from the beginning.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.next();
userInput = Character.toUpperCase(userInput.charAt(0)) + userInput.substring(1);
int i;
int j;
if (userInput.length() % 2 != 0) {
for(i = 1; i < userInput.length(); i += 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.println("");
}
for(i = userInput.length(); i > 0; i -= 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.print("\n");
}
} else {
for(i = 2; i < userInput.length(); i += 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.println("");
}
for(i = userInput.length(); i > 0; i -= 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.print("\n");
}
}
}
For example my input is "Peter".
So my output is:
P
Pet
Peter
Pet
P
but it must be:
P
Ete
Rpete
Rpe
T
I dont know what to change to make this work
Here's a shorter version of your code:
public static void main(String[] args) {
String userInput = "Peter";
int length = userInput.length();
int m, j, i, n = 0;
for (m = length % 2 > 0 ? 1 : 2; m < length * 2; m += 2) {
i = m < length ? m : length * 2 - m;
for (j = 0; j < length - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
char c = userInput.charAt(n++ % length);
c = j == 0 ? Character.toUpperCase(c) : Character.toLowerCase(c);
System.out.print(c);
}
System.out.println("");
}
}
You need some few changes:
Declare int n=0; after int j;
Always print userInput.charAt(n++ % userInput.length()) instead of charAt(j)
In order to get only the first character in line in uppercase:
char c = userInput.charAt(n++ % userInput.length());
c = j == 0 ? Character.toUpperCase(c) : Character.toLowerCase(c);
System.out.print(c);
Check the modulo operator.
With these changes, you'll get this output:
P
Ete
Rpete
Rpe
T
Given the fact that the input itself gets printed in a cylic manner, we can make use out of it. My proposal would be to concatenate the input string and print out the substrings which are determined by the structure of the diamond pattern.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.next();
String concatenated = userInput;
// build up the index array
int i, cumSum = 0;
ArrayList<Integer> helperIndex = new ArrayList<>();
for(i = 1; i < userInput.length(); i += 2) {
helperIndex.add(i);
cumSum += i;
}
for(i = userInput.length(); i > 0; i -= 2) {
helperIndex.add(i);
cumSum += i;
}
int numOfWordRepitition = cumSum / userInput.length() ;
for (i = 0; i < numOfWordRepitition; i++){
concatenated += userInput;
}
// print out diamond
String substr;
int prev = helperIndex.get(0);
int next = helperIndex.get(0);
substr = concatenated.substring(0 , helperIndex.get(0));
System.out.println(Character.toUpperCase(substr.charAt(0)) + substr.substring(1));
for(i = 1; i < userInput.length(); i++){
next += helperIndex.get(i);
substr = concatenated.substring(prev , next);
substr = Character.toUpperCase(substr.charAt(0)) + substr.substring(1);
System.out.println(substr);
prev = next;
}
}

Java - Printing an unfilled diamond using nested for loops

I am new to learning Java and I am trying to print an unfilled diamond using asterisks. The height of the diamond has to be based on user input. My code currently prints out a filled diamond based on user input but I cannot figure out how to print an unfilled one. Every time I change one of the loops it messes something else up. All help is appreciated!
public static void diamond(){
int i = 0;
int j = 0;
int k = 0;
int height = 0;
Scanner in = new Scanner(System.in);
System.out.println("How tall do you want the diamond to be: ");
height = in.nextInt();
for (k = 1; k <= (height + 1) / 2; k++) {
for (i = 0; i < height - k; i++) {
System.out.print(" ");
}
for (j = 0; j < k; j++) {
System.out.print("* ");
}
System.out.println("");
}
for (k = ((height + 1) / 2); k < height; k++) {
for (i = 1; i < k; i++) {
System.out.print(" ");
}
for (j = 0; j < height - k; j++) {
System.out.print(" *");
}
System.out.println("");
}
}
Here is the method modified for printing an unfilled diamond. An if condition has been added to the inner loops.
public static void diamond() {
int i = 0;
int j = 0;
int k = 0;
int height = 0;
Scanner in = new Scanner(System.in);
System.out.println("How tall do you want the diamond to be: ");
height = in.nextInt();
for (k = 1; k <= (height + 1) / 2; k++) {
for (i = 0; i < height - k; i++) {
System.out.print(" ");
}
for (j = 0; j < k; j++) {
if (j == 0 || j == (k - 1)) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
for (k = ((height + 1) / 2); k < height; k++) {
for (i = 1; i < k; i++) {
System.out.print(" ");
}
for (j = 0; j < height - k; j++) {
if (j == 0 || j == (height - k - 1)) {
System.out.print(" *");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
}

Traversing a 2D array matrix diagonally from bottom left to upper right

I have a 3x4 matrix represented by a 2D array:
. 0 1 2 3
0 a c f i
1 b e h k
2 d g j l
and my approach to traverse the diagonal slice was to treat each slice as a sum, like this:
a = (0+0) = 0
b,c = (0+1),(1+0) = 1
d,e,f = (0+2),(1+1),(2+0) = 2
g,h,i = (1+2),(2+1),(3+0) = 3
j, k = (2+2),(3+1) = 4
l = (3+2) = 5
However, my code right now prints it in the opposite way that I want it to, which is from upper right to bottom left.
Current Output is:
acbfedihgkjl
Desired Output is:
abcdefghijkl
for (int sum = 0; sum <= numRows + numColumns - 2; sum++) {
for (int i = 0; i < numRows; i++) {
int j = sum - i;
if ((i >= 0 && i < numRows) && (j >= 0 && j < numColumns)) {
System.out.print(array[i][j]);
}
}
}
Can somebody point me in the right direction on how to fix my code to get the output that I want?
While it isn't very pretty, I think this will do it:
int i = 0;
int j = 0;
while (true) {
System.out.println("" + array[i][j]);
--i;
++j;
if (i < 0) {
if (j == numCols)
break;
i = Math.min(j, numRows - 1);
j = Math.max(j - numCols + 2, 0);
} else if (j >= numCols) {
if (i == numRows - 2)
break;
i = numRows - 1;
j = Math.max(j + 2 - numCols + i, 0);
}
}
int i = 0;
int j = 0;
int n = 0;
int x = 3;
int y = 4;
int newSize = Math.max(x,y) * Math.max(x,y);
while(n < newSize){
if(i <= x && j <= y)
System.out.println(array[i][j]);
n++;
if(i == 0) {
i = n:
j = 0;
} else {
--i;
++j;
}
}

Looping Algorithm

How do I make this:
*******
-*****-
--***--
---*---
--***--
-*****-
*******
The following is my code that I have written to try to accomplish the above, but it is not working as expected:
public static void stars(/*int jmlBaris*/) {
for ( int i = 7; i >= 1; i-=2) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
for (int i = 1; i <= 7; i+=2) {
for (int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println("");
}
}
public static void main(String[] args) {
stars();
}
}
This is how I might write it.
// three loops
public static void stars(int size) {
for (int y = 0; y < size; y++) {
for (int i = 0; i < y && i < size - y - 1; i++)
System.out.print(' ');
for (int i = Math.min(y, size - y - 1); i < Math.max(y + 1, size - y); i++)
System.out.print('*');
System.out.println();
}
}
or
// two loops
public static void stars(int size) {
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++)
System.out.print(
(x >= y && x < size - y) ||
(x >= size - y - 1 && x <= y) ? '*' : ' ');
System.out.println();
}
}
or
// one loop
public static void stars(int size) {
for (int i = 0; i < size * size; i++) {
int y = i / size, x = i % size;
System.out.print(
(x >= y && x < size - y) ||
(x >= size - y - 1 && x <= y) ? '*' : ' ');
if (x == size - 1)
System.out.println();
}
}
Note: Whether this uses one, two or three loops, the time complexity is O(N^2). A simple way to determine this is the number of stars produced is O(N^2) no matter how it is done.
I would do something like this with substrings.
String a = "*******"; //7 stars
String blank = " "; //7 spaces
int j = 7;
for (int i = 0; i < 7; i++) {
if (i > j){
System.out.print(blank.substring(0,i));
System.out.println(a.substring(i,j));
}
else{
System.out.print(blank.substring(0,j));
System.out.println(a.substring(j,i));
}
j--;
}
System.out.println(a);
**Previous edit wouldn't have worked. Changes made.
This works.
Try something like this code I compiled on IDEOne (it seems to work, though):
http://ideone.com/9xZ1YB
class Main
{
public static void main(String[] args)
{
stars();
}
static void stars()
{
final int MAX_WIDTH = 7;
for (int i = 0; i < 7; ++i)
{
int width;
if (i < 3) width = MAX_WIDTH - i * 2;
else if (i > 3) width = (i - 3) * 2 + 1;
else width = 1;
// Before spaces
for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j)
{
System.out.print(" ");
}
// Stars
for (int j = 0; j < width; ++j)
{
System.out.print("*");
}
// After spaces
for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j)
{
System.out.print(" ");
}
System.out.println();
}
}
}
For a beginner in algorithms I would recommend you to break down the structure in sub-parts and then try to solve the pattern.
For this specific pattern it could be broken down into several triangles. Each triangle is then solved by different for loops as shown in the image below.
public static void printPattern(int num) {
// this loop generates first 4 lines
for (int i = 0; i < num / 2 + 1; i++) {
// draws the red triangle of '-'
for (int j = 0; j < i; j++) {
System.out.print("-");
}
// draws the green triangle of '*'
for (int j = i; j < num / 2 + 1; j++) {
System.out.print("*");
}
// draws the blue triangle of '*'
for (int j = i + 1; j < num / 2 + 1; j++) {
System.out.print("*");
}
// draws the orange triangle of '-'
for (int j = 0; j < i; j++) {
System.out.print("-");
}
System.out.println();
}
/* this loop generates last 3 lines */
for (int i = 0; i < num / 2; i++) {
// draws the green triangle of '-'
for (int j = i + 1; j < num / 2; j++) {
System.out.print("-");
}
// draws the red triangle of '*'
for (int j = 0; j < i + 2; j++) {
System.out.print("*");
}
// draws the orange triangle of '*'
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
// draws the blue triangle of '-'
for (int j = i + 1; j < num / 2; j++) {
System.out.print("-");
}
System.out.println();
}
}
Using similar technique you could generate any pattern.
If I understood you right, your problem is to print indent in lines 2-7.
Imagine same problem with asterisk symbol replaced by 'x' and whitespace replaced by '-'. Then you need to draw
xxxxxxx
-xxxxx-
--xxx--
---x---
--xxx--
-xxxxx-
xxxxxxx
That means you should output 0, 1, 2 space(s) before asterisks in first, second, thrid strings respectively. I let details for you to figure them out.
public static void stars(/*int jmlBaris*/){
String starstr = "*";
String blank = "_";
int spaceBlank;;
for(int i=7; i>=1;i-=2){
spaceBlank = (7-i)*.5;
String starrep = StringUtils.repeat(starstr, i);
String blankrep = StrinUtils.repeat(blank, spacesBlank);
system.out.println(blankrep + starrep + blankrep);
}
for(int j=3 j<=7; j+=2){
spaceBlank = (7-j)*.5;
starrep = StringUtils.repeat(starstr, j);
String blankrep = StrinUtils.repeat(blank, spacesBlank);
system.out.println(blankrep + starrep + blankrep);
}
}
public static void main(String[] args){
stars();
}
You have little missing to put space on your code. I don't care about right space, who can see that? But left space is very important!!
Try this:
public static void stars(/*int jmlBaris*/) {
for ( int i = 7; i >= 1; i-=2) {
for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */
System.out.print(" "); /* Missing Here */
} /* Missing Here */
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
for (int i = 1; i <= 7; i+=2) {
for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */
System.out.print(" "); /* Missing Here */
} /* Missing Here */
for (int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println("");
}
}
int N = 7;
for (int y=0; y<N; y++)
{
for (int x=0; x<N; x++)
System.out.print( (y-x)*(N-y-x-1)<=0 ? '*' : '-');
System.out.println();
}
or, more symmetrically,
int n = 3;
for (int y=-n; y<=n; y++)
{
for (int x=-n; x<=n; x++)
System.out.print( y*y>=x*x ? '*' : '-');
System.out.println();
}

Categories

Resources