Generating a sequence of numbers
Hi there, I'm trying to generate a number sequence. The description is as follows:
a)1 is in the sequence
b) If number X is in the sequence then so are the numbers 3*X, 5*X, and 7*X.
c) No number appears more than once in the sequence, and
d) No other number is in the sequence.
Thus, first few numbers in the sequence are:
1,3,5,7,9,15,21,25,27,35,45,49,63,75,81,105...
I need to print out the first 1000 elements of this sequence which is fairly large.
How can i compute this sequence and print the first 1000 elements of this sequence? I think that we need 3 separate methods, 1 for 3*X, 5*X and 7*X. Can anyone out there help me with this?
The following code is in ML (because that's what I did this in for an exercise in my first year). datatype stream = Item of int * (unit->stream);
fun cons (x,xs) = Item (x,xs);
fun head (Item(i,xf))=i;
fun tail (Item(i,xf))=xf();
fun maps f xs = cons(f (head xs), fn()=> maps f (tail xs));
fun merge (xs,ys)=
let val x = head (xs);
val y = head (ys)
in if x=y then cons(x, fn()=>merge((tail(xs)), (tail (ys))) )
else if x<y then cons(x, fn()=>merge((tail(xs)), ys) )
else cons(y, fn()=>merge(xs,(tail(ys))) ) end; (* y<x *)
fun merge3(xs,ys,zs) = merge(xs,merge(ys,zs));
fun threefiveseven()=
cons(1, fn()=>
merge3( maps (fn x=> 3*x) (threefiveseven()),
maps (fn x=> 5*x) (threefiveseven()),
maps (fn x=> 7*x) (threefiveseven()) )); Translating into Java elegantly isn't easy, because Java doesn't have function pointers. It took me quite a while, because it's difficult to avoid infinite recursion. (That's what the unit->stream and fn()=> is all about). Here you go: abstract class Function
abstract int f(int i);
class Maps extends Stream
Function f;
Maps(Function f, Stream xs)
this.head = f.f(xs.head());
this.tail = xs;
this.f = f;
Stream tail()
return new Maps(f, tail.tail());
class Merge extends Stream
Stream xs;
boolean tailxs;
Stream ys;
boolean tailys;
Merge(Stream xs, Stream ys)
int x = xs.head();
int y = ys.head();
this.xs = xs;
this.ys = ys;
if (x == y)
head = x;
tailxs = true;
tailys = true;
else if (x < y)
head = x;
tailxs = true;
tailys = false;
else
head = y;
tailxs = false;
tailys = true;
Stream tail()
Stream left = (tailxs) ? xs.tail() : xs;
Stream right = (tailys) ? ys.tail() : ys;
return new Merge(left, right);
abstract class Stream
int head;
int head() { return head; }
Stream tail;
abstract Stream tail();
public class Test
public static void main(String[] args)
printFirst(new ThreeFiveSeven(), 10);
static void printFirst(Stream stream, int n)
while(n-- > 0)
System.out.println(stream.head());
stream = stream.tail();
class ThreeFiveSeven extends Stream
ThreeFiveSeven()
head = 1;
Stream tail()
Stream three = new Maps(new Times3(), new ThreeFiveSeven());
Stream five = new Maps(new Times5(), new ThreeFiveSeven());
Stream seven = new Maps(new Times7(), new ThreeFiveSeven());
return new Merge(three, new Merge(five, seven));
class Times3 extends Function
int f(int i) { return 3 * i; }
class Times5 extends Function
int f(int i) { return 5 * i; }
class Times7 extends Function
int f(int i) { return 7 * i; }
}
Similar Messages
-
How to generate random sequence numbers
Hello experts
Iu2019m writing a custom program and pulling data from VBAK & VBAP. As per requirement I also need to generate a sequence number randomly and finally store all data in text file and upload at server. The hiccup is I donu2019t know how to generate sequence numbers. Can somebody please show me how I can accomplish it?
Thanks a lot in advanceFind the below code,
data: lv_range type datatype-char0128.
call function 'RANDOM_C'
exporting
len_min = 20
len_max = 20
char_min = 1
char_max = 20
importing
rnd_value = lv_range.
LV_RANGE param will have a random value..
If you need number you can use the FM "RANDOM_I4"
Thanks,
Prathap -
Select from table in group of columns and generate a sequence number
I have to select data from a table in group of columns and generate a sequence for every group resetting the sequence to start from 1 onwards.
For example:
Data:
Col1 Col2 Col3 Col4
A NA KA 2009-08-13
B NA KA 2009-08-13
C NA KA 2009-08-13
A NA KA 2009-08-13
B NA KA 2009-08-13
A NA KA 2009-08-13
Expected output from Select Statement:
Col1 Col2 Col3 Col4 Seq_No
A NA KA 2009-08-13 1
A NA KA 2009-08-13 2
A NA KA 2009-08-13 3
B NA KA 2009-08-13 1
B NA KA 2009-08-13 2
C NA KA 2009-08-13 1
How can this be possible with a SELECT statement? Is it possible to assign seq numbers for a group of columns and reset it when it changes? In the above example, all columns form the key to generate the seq number
I know it can be done using Stored procedures and that is how I am doing it now by introducing a temporary table.
Can anyone help me in this regard? Please let me know if the question is vague to understand!
Thanks,
Nachiwith t as(select 'A' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
union all
select 'B' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
union all
select 'C' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
union all
select 'A' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
union all
select 'B' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual
union all
select 'A' col1,'NA' col2 ,'KA' col3,'2009-08-13' col4 from dual)
select t.*,row_number() over (partition by col1,col2,col3,col4 order by col1,col2,col3,col4) from tYou can replace partition by col1,col2,col3,col4 with only columns that you need for grouping condition
and also order by you can just do on the column you need. -
Help in generating the same random numbers at every time of executuion
dear friends
i am facing a problem that the random numbers generated at time of each exectuion of the program are not the same, i want to generate the same random numbers every time time i run the program. i need your help. i am giving the code in c++ if anybody can help in providing the solution so that i get the same random numbers at every run of the program. waiting for your help
wit regards
jaya shankar
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
class density
int s[150][150],parent[150][150],i,j,l,n,loop;
public:
void bd(void);
void css(void);
void density :: bd(void)
cout<<"ENTER THE POPULATION SIZE = ";
cin>>n;
cout<<"\nENTER THE NO.OF LOOPS = ";
cin>>loop;
for(i=0;i<n;i++)
for(j=0;j<80;j++)
s[j]=rand()%2;
cout<<"s:"<<s[j];
void density :: css(void)
int a,b;
float c;
for(i=0;i<n;i=i+2)
b=rand()%100;
cout<<"random b="<<b;
main()
density d;
d.bd();
d.css();You have to read the documentation for the java.util.Random class.
"If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random"
Here is a peace of code that will do wat you need:
Random rand = new Random(100);
for(int i=0; i<10; i++)
System.out.println("Random="+ rand.nextInt(10)); -
Need SQL statement to generate a sequence number in the output rows
Hi folks. I need to create an SQL statement that generates a sequence number column in the output rows (records) such that the first returned row has this column set to 1, second returned row has the column set to 2, etc.
For example, consider the query:
SELECT income from employees WHERE income != 20000 ORDER BY income;
If employees.income contains 60,000, 20,000, 35,000, and 19,000 for respective rows, the output would be this:
19,000
35,000
60,000
I would like the SQL to also return a sequence number that is computed across the returned rows, resulting in two output columns:
1 19,000
2 35,000
3 60,000
Is there a simple SQL function that generates the sequence number, in order, and only for the returned rows? Or is there another way?
I'm stumped. Any help is appreciated! Thanks!
- Jack CochraneHi,
Welcome to the forum!
Use ROWNUM, like (example):
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr
SQL> select rownum, first_name from (select e.first_name from employees e where e.first_name like 'J%' order by e.first_name);
ROWNUM FIRST_NAME
1 Jack
2 James
3 James
4 Janette
5 Jason
6 Jean
7 Jennifer
8 Jennifer
9 John
10 John
11 John
12 Jonathon
13 Jose Manuel
14 Joshua
15 Julia
16 Julia
16 rows selected
SQL> But rememeber if you want to be sure of unique numbers in certain field is better to use sequences and use seq_name.nextval each time you need.
Regards, -
Help in generating the same random numbers each time of the program executi
dear friends
i am facing a problem that the random numbers generated at time of each exectuion of the program are not the same, i want to generate the same random numbers every time time i run the program. i need your help. i am giving the code in c++ if anybody can help in providing the solution so that i get the same random numbers at every run of the program. waiting for your help
wit regards
jaya shankar
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
class density
int s[150][150],parent[150][150],i,j,l,n,loop;
public:
void bd(void);
void css(void);
void density :: bd(void)
cout<<"ENTER THE POPULATION SIZE = ";
cin>>n;
cout<<"\nENTER THE NO.OF LOOPS = ";
cin>>loop;
for(i=0;i<n;i++)
for(j=0;j<80;j++)
s[i][j]=rand()%2;
cout<<"s:"<<s[i][j];
void density :: css(void)
int a,b;
float c;
for(i=0;i<n;i=i+2)
b=rand()%100;
cout<<"random b="<<b;
main()
density d;
d.bd();
d.css();To generate the same random,it's impossible!
why do you want to do that? -
Hi i am using
select rownum rn from user_objects where rownum <= 20;
to generate a sequence of number....
is there any other better way...???try
select level
from dual
connect by level <= 20;
illustrated at -
http://nimishgarg.blogspot.com/2010/01/oracle-sql-get-all-month-names-jan-to.html -
Generating a sequence of waveforms with NI 5640R card
Hello,
I have a question regarding generating a sequence of waveforms. I want to use the example "NI 5640R analog input and output" (I know this example is capable of generating a QAM or single tone signal) and make changes such that the when i run the VI i want the output to be switching between single tone and QAM and i want this to happen until i stop the VI.
And in the future i want to add another waveform (sawtooth) and want my output to be switching between the 3 waveforms. Any help regarding this will be grately appreciated.
Thanks,
Sandeep.
Sandeep Palreddy, Graduate Research Assistance
The Microwave Remote Sensing Laboratory (MIRSL)
University of Massachusetts
151 Holdsworth Way
Amherst MA 01003-9284Hello,
I have a question regarding generating a sequence of waveforms. I want to use the example "NI 5640R analog input and output" (I know this example is capable of generating a QAM or single tone signal) and make changes such that the when i run the VI i want the output to be switching between single tone and QAM and i want this to happen until i stop the VI.
And in the future i want to add another waveform (sawtooth) and want my output to be switching between the 3 waveforms. Any help regarding this will be grately appreciated.
Thanks,
Sandeep.
Sandeep Palreddy, Graduate Research Assistance
The Microwave Remote Sensing Laboratory (MIRSL)
University of Massachusetts
151 Holdsworth Way
Amherst MA 01003-9284 -
Calculate date differences in consecutive rows and generate a sequence
Hi Guys,
I am trying to implement one scenario where in i need to compare the date differences for consecutive rows and generate a sequence against that.
this is the table schema:
create table temp
id int identity(1,1),
emp_id int,
time datetime
insert into temp
values
(1, '2011-02-20 12:30:00.000'),
(1, '2011-02-20 12:32:34.172'),
(1, '2011-02-20 12:32:34.172'),
(1, '2011-02-20 12:43:21.004'),
(1, '2011-02-20 12:46:39.745'),
(2, '2011-02-20 12:48:06.004'),
(2, '2011-02-20 12:48:06.004'),
(2, '2011-02-20 12:53:07.733'),
(2, '2011-02-20 12:55:30.295');
now, I want to compare the first date-time with the second and so on. now if the date-time difference is same for two consecutive rows then the sequence should increment by 1 otherwise the sequence again will start from '00' for any unique date-time.
This sequence number should start from '00' from each employee.
I want the output to be like this
ID emp_id
time
sequence
1 1 2011-02-20 12:30:00.000
00
2 1 2011-02-20 12:32:34.172
00
3 1 2011-02-20 12:32:34.172
01
4 1 2011-02-20 12:32:34.172
02
5 1 2011-02-20 12:46:39.745
00
6 2
2011-02-20 12:48:06.004 00
7 2
2011-02-20 12:48:07.003 00
8 2
2011-02-20 12:48:07.003 01
9 2
2011-02-20 12:46:39.745 00
Please revert as soon as possible as this is a bit urgent.
Thank You in Advance. :)create table temp
(id int identity(1,1),
emp_id int,
time datetime
insert into temp values
(1, '20110220 12:30:00.000'),
(1, '20110220 12:32:34.172'),
(1, '20110220 12:32:34.172'),
(1, '20110220 12:43:21.004'),
(1, '20110220 12:46:39.745'),
(2, '20110220 12:48:06.004'),
(2, '20110220 12:48:06.004'),
(2, '20110220 12:53:07.733'),
(2, '20110220 12:55:30.295');
go
SELECT id, emp_id, time,
dateadd(mcs, (row_number() OVER(PARTITION BY emp_id, time ORDER BY
id) - 1) * 10,
convert(datetime2(5), time)) AS [Sequence]
FROM temp
ORDER BY id
go
DROP TABLE temp
Erland Sommarskog, SQL Server MVP, [email protected] -
Strange display of a long sequence of numbers - Should I press OK ?
From type to time my iphone displays a long sequence of numbers and waits for me to press a green "OK" button. For example last sequence starts with 1259752976 1 199.664277......
Anybody knows what's going on ?Yolanda,
Yolanda Arroyo wrote:
Hi folks,
I have problems with my Tuxedo cofiguration using domains.
I have Tuxedo8 installed in Linux 7.1. It connects to an Oracle 8.1.7 database,
and has 2 domains cofigured to connect to a remote domain. (this domain is in
fact WebLogic Server 6.1 running in the same machine)
If I run this configuration, the servers in LDOM2 (for example, server1) are not
able to connect to the database (this group don't include a TMS because is
associated with the domain, and I cannot put an OPENINFO information in that
group, for the same reason)
BUT!!!! the most amazing thing is that, if I make no changes in the bdmconfig
file, and in the UBBCONFIG I change the group WTC2 for a group that contains a
TMS, in this way:
I think that you are confusing the purpose of the 2 config files, ubbconfig and
dmconfig.
You should configure you application servers in the ubbconfig file. They
should be in server groups configured with the correct TMS servers, OPENINFO, etc.
One you have done this, you have a single domain.
To extend this to talk to external domains, now you need to configure the gateways.
Add a new server group to your ubbconfig and configure the domain gateway
processes (GWADM, DMADM, GWTDOMAIN) in it. This group does not need a TMS (TMS
services are provided by the domain gateway) or any OPENINFO.
Now, write a dmconfig file. This does not reference any servers it does
reference services that you want to import from other domains, or export to them.
I hope this explains how you can successfully get your configuration working...
Regards,
Peter. -
Cryptic sequence of numbers as sender name for invitations
When I want to send an calendar invitation to my business address, my sender name is a cryptic sequence of numbers instead of "first name last name". These are the steps:
1. Receiving Email form my iPad with the invitation
2. Download the attached ics to my desktop of the windows PC
3. Drag&Drop the ics to the Lotus Notes Calendar
4. The invitation is visible, the sender name is i.e. 2fjfu3oodsjdf90499fj993efj0ßßejfefjß39eß939hgsdlfnasdäfa9e9
I know that this invitation is from my private account. But what about friends I wanted to invite? Or should I invite only friends with apple equipment?!?! ;-)Well,
Nokia MfE doesn't reply to my mails. I guess that only poeple using MfE v 2.09.158 who are not registered to the MfE Service (I guess you have to pay a monthly fee as it is with the Blackberry service) have the sender name problem. Of course you can use MfE with other mail providers than Nokia MfE. But in this case "Mail for Exchange" will appear as sender name.
I now switched over to MfE's competitor "RS" and don't have this annoying problem anymore. The alternative to RS would be using the old MfE version where I didn't have that sender name prob. But as my battery was empty after 15 hrs using the old MfE version (with RS my battery lasts 3 days !!!) there is no real alternative to RS!
This sender name problem seems to be part of Nokia's business strategy. As they want to compete with Blackberry they want you to register (and pay the MfE mail service monthly). Probably that's the reason why you get MfE for free (RS costs 50 US$).
Any comments on my theory?!
BR, Johnny -
Unsupported generator type: SEQUENCE
Is the primary key generation strategy "SEQUENCE" supported? I have an entity, based on a table in Oracle database, whose primary key is generated by a sequence in Oracle database. When I deploy the entity I get the following error...
ORPersistence Model Builder: Exception occurred: com.sap.engine.services.orpersistence.model.ormappingmodel.ORMException: Unsupported generator type: SEQUENCE at com.sap.engine.services.orpersistence.model.ormappingmodel.impl.ORMappingModelCreatorImpl.parseId(ORMappingModelCreatorImpl.java:2347), file: EntityTest.jar, column 0, line 0, severity: errorHi Christopher,
The generation types SEQUENCE and IDENTITY are currently not supported by the JPA implementation in this release. You can however use the TABLE and AUTO generation types. In the case of AUTO you have to provide a table TMP_SEQUENCE created like this:
CREATE TABLE "TMP_SEQUENCE"
"GEN_KEY" VARCHAR(256) UNICODE NOT NULL,
"GEN_VALUE" INTEGER,
PRIMARY KEY ("GEN_KEY")
HTH!
-Vladimir -
"Hello everyone, I hope someone can answer the following question. How do I generate a sequence of different waveforms, with different attributes?
Like, i want to generate a sine wave for some time, then a square pulse of some width and then again a sine wave of some other time. Finally I want to repeat this sequence. I'll really appreciated it if I can get an answer soon. Thank you!From the previous comments I am assuming that your waveforms are arrays, rather than the waveform data type. I am also assuming that the sine waves on both sides of the square wave are the same signal.
If so then I suggest a slightly different approach. 1. Generate your sine wave, rather than the empty array. The length should be the length of the entire end signal.
2. Generate the square waveform. You can either generate it at the length you need, or you can generate a much larger square wave, and then use the Array Subset VI to get the portion you need. Which method you use depends how you want to implement the whole method.
3. Use the Replace Array Subset VI to place the portion of the square wave at the location (index) of the original sine wave you ge
nerated.
The result of this method will be an array that starts with your sine wave, and then at the index you choose it will place the square wave of the length you want right into the signal, and then finish up with the sine wave for the rest of the signal.
Evan Collier
Application Engineering
National Instruments -
Generate alphanumeric sequence using oracle sequence
Hi,
Can we generate alphanumeric sequence from Oracle sequence.
Sequence would be something like this. Please advice
TEMP-0001
TEMP-0002
TEMP-0010
Thanks,
Lakhi,
You can try below procedure...
SQL> create sequence sec
2 minvalue 0
3 start with 001
4 increment by 1
5 nocache;
Sequence created.
SQL> select 'temp-'||to_char(sec.nextval) from dual;
'TEMP-'||TO_CHAR(SEC.NEXTVAL)
temp-1 -
@SequenceGenerator generating wrong sequence numbers on an Oracle database
Hello,
I have a JPA entity with the following annotation:
@SequenceGenerator(name = "PERS_GEN", sequenceName = "PERS_SEQ", initialValue = 1, allocationSize = 1)
But the ids created by this sequence are not incrementing one by one;they increment 20 by 20. What is the cause of this?And how can it be sold?Hello,
You will need to reference the database docs on what the database cache setting means or how it behaves, or post on the JDBC forums as I don't really know. My testing by calling SELECT seq.NEXTVAL FROM dual repeatedly caused it to return numbers sequentially based on the increment by value. I suspect this was because I was using the same session/connect etc and might be different if using different connections, timing etc. Have you tried creating the sequence with the nocache option? The cache 20 is not something that would be set by EclipseLink and is likely a database default, and I do not see why START WITH 21 would be used if the annotation specified initialValue = 1 - how are you checking the sequence that gets created? You can turn on EclipseLink logging to Finest to see the SQL it generates, but I would suggest you create the sequence object yourself - or have JPA create a DDL file and use it to create the tables. See http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#Using_EclipseLink_JPA_Extensions_for_Schema_Generation for ddl generation options.
I found this http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:369390500346406705 discussion on database sequence cache sizes that might help.
Regards,
Chris
Maybe you are looking for
-
I've got a simple layout but I need to get it looking more like this picture: http://members.lycos.nl/ghanswebsite/Layout.JPG Now I got a simple GridBag. I tried a few other layouts but they got me errors so until now I evaded them .. :-(. // Fig. 12
-
I am getting ORA-01008:not all variables bound error
Hi all, i am getting ORA-01008:not all variables bound error while executing the following code... Could any one help me to find out the problem...i am very new to ODP.net and Oracle .. I am using VC++.net for run this code. Here is the code..... #in
-
Bulk Data - Insert / Update from text file - Urgent
hi, Consider there is a Table A. There are around 1.7 Million Records. i have a situation that i will be getting the Data, Every two Weeks, around 100 - 200 thousand Records. I have to do some calculations. If any Old Data Exists, then update the Dat
-
New mini with new samsung 2494Hm display *****
Hi folks, I connected my new mini to my samsung 2494HM monitor via HDMI and the picture *****, I have tried all the various adjustments and I can't make it good. Any advice? Thanks, Bart
-
Stop pre from trying to force update to 1.3.1?
So like most other posts after 1.3.1 was dumped on the unsuspecting populace, I updated, saw none of my major bugs fixed, introduced only worse ones, and I flattened my phone back to restore back to 1.2.1 until palm gets their act together (hopefully