LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2011, 01:58 AM   #1
vjramana
Member
 
Registered: Sep 2009
Posts: 89

Rep: Reputation: 0
generating series of numbers of sequence


I am trying to generate sequence of number in the order of :

1
2
3

4
5
6

7
8
9

10
11
12

13
14
15

Where by after three lines of numbers there must be a blank line.

I tried to write this using bash script as below:
Code:
for i in {1..5}; do #lipid
#  echo $i
	
		for j in {1..3}; do #atoms in lipid
		
			echo $[$j*$i ]
			
		done
		echo ""  
	

done
but I get a sequence as below
1
2
3

2
4
6

3
6
9

4
8
12

5
10
15

Can anyone help me?
Thanks in advance
 
Old 03-19-2011, 02:27 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well that's just a basic math problem, no scripting issue in the slightest, just logic. This sounds a lot like homework so I'm not going to give you the answer, but to get your approach to work you just need to think more about the maths you're doing. Using $i, $j and the maximum size of j each time you can do this easily. I would also say that the range for i is not ideal, and things might be clearer if you start it at a different number.
 
Old 03-19-2011, 02:35 AM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Go back to basics, and look at what you are doing:
Code:
for i in {1..5}; do #lipid
#  echo $i
	
		for j in {1..3}; do #atoms in lipid
		
			echo $[$j*$i ]
                      # Do you really want to multiply i by j?
			
		done
		echo ""  
	

done
What you want to do is:
  • create a sequence of numbers 1..15
  • check if iteration is an exact multiple of 3 and display a new line if it is

HTH
 
Old 03-19-2011, 02:38 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Just an aside note: the usage of the $[...] operator is deprecated and it has been replaced by the arithmetic operator ((...)).
 
Old 03-19-2011, 03:24 AM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Here's a Ruby(1.91+) one-liner for you
Code:
$ ruby -e '(1..15).to_a.each_slice(3){|x| puts x;puts}'
 
Old 03-19-2011, 10:03 AM   #6
vjramana
Member
 
Registered: Sep 2009
Posts: 89

Original Poster
Rep: Reputation: 0
Dear, acid_kewpie

Thanks for the hint. Anyway this in not my homework.
Well after some try I got it as I want. For sharing purpose I would like post mine here.

Quote:
#!/bin/bash

for k in {0..12..3}; do
for n in {1..3}; do
echo $(($k+$n))
done
echo " "
done
You can give suggestion if there is any. I can improve with other way of doing things.


For Mr Kurumi, Thanks for your suggestion as well. It works. But I am more on bash.
 
Old 03-19-2011, 10:03 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
In these cases, I tend to make one sequence, and on each iteration, test whether that special case is true. In this case, the 'special case' is 'evenly divisible by three', which suggests the use of a modulo operator. Does bash have one of those? If not, it shouldn't be hard to craft one as a function.
--- rod.
 
Old 03-19-2011, 10:34 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Quote:
Does bash have one of those?
Yep

Code:
#!/bin/bash

for (( i=1;i<=15;i++))
do
    if ((  i % 3 ))
    then
        echo $i
    else
        echo -e "$i\n"
    fi
done
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
generating prime numbers in C j360 Programming 10 03-11-2011 03:43 AM
generating random numbers in C abk4523 Programming 20 01-10-2011 04:08 PM
generating random numbers for 10 minutes lucky.shubham Programming 4 10-21-2009 08:16 AM
sequence of numbers, how to extract which numbers are missing jonlake Programming 13 06-26-2006 03:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration